private static <T> long countInternal(BatchingVisitable<T> visitable, int batchSize) { final long[] count = new long[1]; visitable.batchAccept( batchSize, AbortingVisitors.batching( new AbortingVisitor<T, RuntimeException>() { @Override public boolean visit(Object item) { count[0]++; return true; } })); return count[0]; }
@Nullable public static <T> T getLast(BatchingVisitable<T> visitable, @Nullable T defaultElement) { final Mutable<T> ret = Mutables.newMutable(defaultElement); visitable.batchAccept( DEFAULT_BATCH_SIZE, AbortingVisitors.batching( new AbortingVisitor<T, RuntimeException>() { @Override public boolean visit(T item) { ret.set(item); return true; } })); return ret.get(); }
public static <T> boolean isEqual(BatchingVisitable<T> v, final Iterator<T> it) { boolean ret = v.batchAccept( DEFAULT_BATCH_SIZE, new AbortingVisitor<List<T>, RuntimeException>() { @Override public boolean visit(List<T> batch) { Iterator<T> toMatch = Iterators.limit(it, batch.size()); return Iterators.elementsEqual(toMatch, batch.iterator()); } }); if (it.hasNext()) { return false; } return ret; }
/** * This will return the first maximal element in the visitable. This method takes a default * element and will return that if the visitable is empty. If the visitable is non-empty it will * return the largest value in the visitable. * * <p>A common way to use this would be to pass <code>null</code> as the defaultElement and have * the ordering throw on null elements so you know the visitable doesn't have any nulls. */ public static <T> T getMin( BatchingVisitable<T> v, final Ordering<? super T> o, @Nullable T defaultElement) { final Mutable<T> ret = Mutables.newMutable(defaultElement); v.batchAccept( DEFAULT_BATCH_SIZE, AbortingVisitors.batching( new AbortingVisitor<T, RuntimeException>() { boolean hasSeenFirst = false; @Override public boolean visit(T item) throws RuntimeException { if (hasSeenFirst) { ret.set(o.min(ret.get(), item)); } else { // Call o.max here so it will throw if item is null and this // ordering hates on nulls. ret.set(o.min(item, item)); hasSeenFirst = true; } return true; } })); return ret.get(); }
public static <T> boolean isEmpty(BatchingVisitable<T> v) { return v.batchAccept(1, AbortingVisitors.batching(AbortingVisitors.<T>alwaysFalse())); }