@Override final public <K extends Exception> boolean batchAccept(int batchSize, AbortingVisitor<? super List<T>, K> v) throws K { Preconditions.checkArgument(batchSize > 0); if (v instanceof ConsistentVisitor) { @SuppressWarnings("unchecked") AbortingVisitor<List<T>, K> v2 = (AbortingVisitor<List<T>, K>) v; ConsistentVisitor<T, K> consistentVisitor = (ConsistentVisitor<T, K>) v2; Preconditions.checkState(consistentVisitor.visitorAlwaysReturnedTrue, "passed a visitor that has already said stop"); batchAcceptSizeHint(batchSize, consistentVisitor); return consistentVisitor.visitorAlwaysReturnedTrue; } AbstractBatchingVisitable.ConsistentVisitor<T, K> consistentVisitor = AbstractBatchingVisitable.ConsistentVisitor.create(batchSize, v); batchAcceptSizeHint(batchSize, consistentVisitor); if (consistentVisitor.visitorAlwaysReturnedTrue && !consistentVisitor.buffer.isEmpty()) { Preconditions.checkState(consistentVisitor.buffer.size() < batchSize); return v.visit(Collections.unmodifiableList(consistentVisitor.buffer)); } else { return consistentVisitor.visitorAlwaysReturnedTrue; } }
@Test public void testImmutableCopyWithDefaultBatchSize() throws Exception { AbstractBatchingVisitable bv = mock(AbstractBatchingVisitable.class); BatchingVisitableView bvv = BatchingVisitableView.of(bv); bvv.immutableCopy(); verify(bv).batchAcceptSizeHint(eq(BatchingVisitables.DEFAULT_BATCH_SIZE), any()); }
@Test public void testImmutableCopyRespectsCustomBatchSize() throws Exception { AbstractBatchingVisitable bv = mock(AbstractBatchingVisitable.class); BatchingVisitableView bvv = BatchingVisitableView.of(bv); bvv.hintBatchSize(2).immutableCopy(); verify(bv).batchAcceptSizeHint(eq(2), any()); }
@Override final public <K extends Exception> boolean batchAccept(int batchSize, AbortingVisitor<? super List<T>, K> v) throws K { Preconditions.checkArgument(batchSize > 0); if (v instanceof ConsistentVisitor) { @SuppressWarnings("unchecked") AbortingVisitor<List<T>, K> v2 = (AbortingVisitor<List<T>, K>) v; ConsistentVisitor<T, K> consistentVisitor = (ConsistentVisitor<T, K>) v2; Preconditions.checkState(consistentVisitor.visitorAlwaysReturnedTrue, "passed a visitor that has already said stop"); batchAcceptSizeHint(batchSize, consistentVisitor); return consistentVisitor.visitorAlwaysReturnedTrue; } AbstractBatchingVisitable.ConsistentVisitor<T, K> consistentVisitor = AbstractBatchingVisitable.ConsistentVisitor.create(batchSize, v); batchAcceptSizeHint(batchSize, consistentVisitor); if (consistentVisitor.visitorAlwaysReturnedTrue && !consistentVisitor.buffer.isEmpty()) { Preconditions.checkState(consistentVisitor.buffer.size() < batchSize); return v.visit(Collections.unmodifiableList(consistentVisitor.buffer)); } else { return consistentVisitor.visitorAlwaysReturnedTrue; } }