/** * Provides an efficient summary of the changes, without doing too much unnecessary work. * - Will only emit changes of a single type (from a single delegate change set) * - Will return no more than the specified maximum of number of changes */ public Iterator<TaskStateChange> iterator() { return new AbstractIterator<TaskStateChange>() { Iterator<TaskStateChange> changes; int count; @Override protected TaskStateChange computeNext() { if (changes == null) { changes = firstDirtyIterator(); } if (count < maxReportedChanges && changes != null && changes.hasNext()) { count++; return changes.next(); } return endOfData(); } }; }
/** * Iterator over all present inhabitants of ordinal domain. * @return snapshot iterator of elements in ordinal domain. */ @Override public Iterator<E> iterator() { return new AbstractIterator<E>() { private final int length = length(); private int index = 0; @Override protected E computeNext() { int p = index++; if (p < length) { return get(p); } return endOfData(); } }; }
@Override public Iterable<AbstractMetric> metrics() { return new Iterable<AbstractMetric>() { final Iterator<AbstractMetric> it = delegate.metrics().iterator(); @Override public Iterator<AbstractMetric> iterator() { return new AbstractIterator<AbstractMetric>() { @Override public AbstractMetric computeNext() { while (it.hasNext()) { AbstractMetric next = it.next(); if (filter.accepts(next.name())) { return next; } } return endOfData(); } }; } }; }
@Override public UnmodifiableIterator<E> iterator() { final Iterator<? extends Entry<E, ?>> entries = outEdgeToNode.entrySet().iterator(); return new AbstractIterator<E>() { @Override protected E computeNext() { while (entries.hasNext()) { Entry<E, ?> entry = entries.next(); if (targetNode.equals(entry.getValue())) { return entry.getKey(); } } return endOfData(); } }; }
/** * Returns an iterator over the lines in the string. If the string ends in a newline, a final * empty string is not included, to match the behavior of BufferedReader/LineReader.readLine(). */ private Iterator<String> linesIterator() { return new AbstractIterator<String>() { Iterator<String> lines = LINE_SPLITTER.split(seq).iterator(); @Override protected String computeNext() { if (lines.hasNext()) { String next = lines.next(); // skip last line if it's empty if (lines.hasNext() || !next.isEmpty()) { return next; } } return endOfData(); } }; }
@Override public Iterator<C> iterator() { return new AbstractIterator<C>() { Iterator<Object[]> it = map.values().iterator(); Object[] counters = it.hasNext() ? it.next() : null; int i = 0; @Override protected C computeNext() { while (counters != null) { while (i < counters.length) { @SuppressWarnings("unchecked") C counter = (C) counters[i++]; if (counter != null) return counter; } i = 0; counters = it.hasNext() ? it.next() : null; } return endOfData(); } }; }
public static <T extends Object> Iterator<T> getWrappingIterator(final int startIdx, final List<T> list) { return new AbstractIterator<T>() { private int idx = Math.max(0, Math.min(startIdx, list.size() - 1)); @Override protected T computeNext() { T o = list.get(idx); idx++; if(idx >= list.size()) { idx = 0; } return o; } }; }
/** * Returns an iterable over the lines in the string. If the string ends in a newline, a final * empty string is not included to match the behavior of BufferedReader/LineReader.readLine(). */ private Iterable<String> lines() { return new Iterable<String>() { @Override public Iterator<String> iterator() { return new AbstractIterator<String>() { Iterator<String> lines = LINE_SPLITTER.split(seq).iterator(); @Override protected String computeNext() { if (lines.hasNext()) { String next = lines.next(); // skip last line if it's empty if (lines.hasNext() || !next.isEmpty()) { return next; } } return endOfData(); } }; } }; }
public Iterable<Entry<String, T>> entry() { return () -> new AbstractIterator<Entry<String, T>>() { NodeHolder holder; Iterator<String> ite; { holder = new NodeHolder(); ite = keys(holder); } @Override protected Entry<String, T> computeNext() { if (!ite.hasNext()) { return endOfData(); } String key = ite.next(); if (key != null) { return new AbstractMap.SimpleEntry<>(key, (T) holder.node.value); } return endOfData(); } }; }
@Override public Iterator<Vertex> iterator() { return new AbstractIterator<Vertex>() { Vertex point = first; @Override protected Vertex computeNext() { if (point == null) { return endOfData(); } else { Vertex old = point; point = old.next; return old; } } }; }
@Override public Iterator<ObjectEntry<T, R>> iterator() { abstract class __SeparatorRegions_1 extends AbstractIterator<ObjectEntry<T, R>> { ObjectEntry<T, R> next; } return new __SeparatorRegions_1() { { next = SeparatorRegions.this.first; } @Override protected ObjectEntry<T, R> computeNext() { ObjectEntry<T, R> _xblockexpression = null; { if ((this.next == null)) { return this.endOfData(); } final ObjectEntry<T, R> current = this.next; this.next = this.next.getTrailingObject(); _xblockexpression = current; } return _xblockexpression; } }; }
/** * Returns a view on this iterator that provides at most the first <code>count</code> entries. * * @param iterator * the iterator. May not be <code>null</code>. * @param count * the number of elements that should be returned at most. * @return an iterator with <code>count</code> elements. Never <code>null</code>. * @throws IllegalArgumentException * if <code>count</code> is negative. */ @Pure public static <T> Iterator<T> take(final Iterator<T> iterator, final int count) { if (iterator == null) throw new NullPointerException("iterator"); if (count < 0) throw new IllegalArgumentException("Cannot take a negative number of elements. Argument 'count' was: " + count); if (count == 0) return ImmutableSet.<T>of().iterator(); return new AbstractIterator<T>() { private int remaining = count; @Override protected T computeNext() { if (remaining <= 0) return endOfData(); if (!iterator.hasNext()) return endOfData(); remaining--; return iterator.next(); } }; }
/** * Returns a view on this iterator that provides all elements except the first <code>count</code> entries. * * @param iterator * the iterator. May not be <code>null</code>. * @param count * the number of elements that should be dropped. * @return an iterator without the first <code>count</code> elements. Never <code>null</code>. * @throws IllegalArgumentException * if <code>count</code> is negative. */ public static <T> Iterator<T> drop(final Iterator<T> iterator, final int count) { if (iterator == null) throw new NullPointerException("iterator"); if (count == 0) return iterator; if (count < 0) throw new IllegalArgumentException("Cannot drop a negative number of elements. Argument 'count' was: " + count); return new AbstractIterator<T>() { { int i = count; while (i > 0 && iterator.hasNext()) { iterator.next(); i--; } } @Override protected T computeNext() { if (!iterator.hasNext()) return endOfData(); return iterator.next(); } }; }
/** * Returns an Iterator containing all elements starting from the head of the source up to and excluding the first * element that violates the predicate. The resulting Iterator is a lazily computed view, so any modifications to the * underlying Iterators will be reflected on iteration. The result does not support {@link Iterator#remove()} * * @param iterator * the elements from which to take. May not be <code>null</code>. * @param predicate * the predicate which decides whether to keep taking elements. May not be <code>null</code>. * @return the taken elements * @since 2.7 */ public static <T> Iterator<T> takeWhile(final Iterator<? extends T> iterator, final Function1<? super T, Boolean> predicate) { if (iterator == null) throw new NullPointerException("iterator"); if (predicate == null) throw new NullPointerException("predicate"); return new AbstractIterator<T>() { @Override protected T computeNext() { if (!iterator.hasNext()) return endOfData(); T next = iterator.next(); if (predicate.apply(next)) { return next; } else { return endOfData(); } } }; }
/** * Returns an Iterator of Pairs where the nth pair is created by taking the nth element of the source as the value * and its 0-based index as the key. E.g. * <code>zipWitIndex(#["a", "b", "c"]) == #[(0, "a"), (1, "b"), (2, "c")]</code> * * If the index would overflow, {@link Integer#MAX_VALUE} is returned for all subsequent elements. * * The resulting Iterator is a lazily computed view, so any modifications to the underlying Iterator will be * reflected on iteration. The result does not support {@link Iterator#remove()} * * @param iterator * the elements. May not be <code>null</code>. * @return the zipped result * @since 2.7 */ public static <A> Iterator<Pair<Integer, A>> indexed(final Iterator<? extends A> iterator) { if (iterator == null) throw new NullPointerException("iterator"); return new AbstractIterator<Pair<Integer, A>>() { int i = 0; @Override protected Pair<Integer, A> computeNext() { if (iterator.hasNext()) { Pair<Integer, A> next = new Pair<Integer, A>(i, iterator.next()); if (i != Integer.MAX_VALUE) i++; return next; } else { return endOfData(); } } }; }
/** * Creates an Iterator of FetchedMessage based on the given message set. The iterator would also updates * the offset while iterating. */ private Iterator<FetchedMessage> createFetchedMessages(ByteBufferMessageSet messageSet, final AtomicLong offset) { final Iterator<MessageAndOffset> messages = messageSet.iterator(); return new AbstractIterator<FetchedMessage>() { @Override protected FetchedMessage computeNext() { while (messages.hasNext()) { MessageAndOffset message = messages.next(); long msgOffset = message.offset(); if (msgOffset < offset.get()) { LOG.trace("Received old offset {}, expecting {} on {}. Message Ignored.", msgOffset, offset.get(), topicPart); continue; } fetchedMessage.setPayload(message.message().payload()); fetchedMessage.setOffset(message.offset()); fetchedMessage.setNextOffset(message.nextOffset()); return fetchedMessage; } return endOfData(); } }; }
@Test public void accessIterable() { final Iterable<Integer> iterable = () -> new AbstractIterator<Integer>() { private boolean done = false; @Override protected Integer computeNext() { if (done) { return endOfData(); } done = true; return 23; } }; final IndexedAccessExpression idxExpr = new IndexedAccessExpression(START, obj(iterable), num(0)); final Object evaluate = idxExpr.evaluateUnsafe(context); assertThat(evaluate).isOfAnyClassIn(Integer.class); assertThat(evaluate).isEqualTo(23); }
public static Iterable<String> toDumpLines(final String code) { return new Iterable<String>() { @Override public Iterator<String> iterator() { return new AbstractIterator<String>() { StringTokenizer tok = new StringTokenizer(code, "\n"); int line = 0; @Override protected String computeNext() { if (tok.hasMoreTokens()) { return String.format("%3d: %s", ++line, tok.nextToken()); } return endOfData(); } }; } }; }
private Iterator<Map.Entry<String, StorageSummary>> decodeMetadataRows( final Iterator<Row<ByteBuffer, Composite>> rowIter, final AstyanaxTable table) { return new AbstractIterator<Map.Entry<String, StorageSummary>>() { @Override protected Map.Entry<String, StorageSummary> computeNext() { while (rowIter.hasNext()) { Row<ByteBuffer, Composite> row = rowIter.next(); ByteBuffer key = row.getKey(); ColumnList<Composite> columns = row.getColumns(); String blobId = AstyanaxStorage.getContentKey(key); StorageSummary summary = toStorageSummary(columns); if (summary == null) { continue; // Partial blob, parts may still be replicating. } // Cleanup older versions of the blob, if any (unlikely). deleteOldColumns(table, blobId, columns, summary.getTimestamp()); return Maps.immutableEntry(blobId, summary); } return endOfData(); } }; }
@Override public Iterator<com.bazaarvoice.emodb.blob.api.Table> listTables(@Nullable String fromTableExclusive, long limit) { checkArgument(limit > 0, "Limit must be >0"); LimitCounter remaining = new LimitCounter(limit); final Iterator<Table> tableIter = _tableDao.list(fromTableExclusive, remaining); return remaining.limit(new AbstractIterator<com.bazaarvoice.emodb.blob.api.Table>() { @Override protected com.bazaarvoice.emodb.blob.api.Table computeNext() { while (tableIter.hasNext()) { Table table = tableIter.next(); if (!table.isInternal()) { return toDefaultTable(table); } } return endOfData(); } }); }
@Override public Iterator<String> listQueues() { final Iterator<Row<String, UUID>> rowIter = execute( _keyspace.prepareQuery(CF_DEDUP_MD, ConsistencyLevel.CL_LOCAL_QUORUM) .getAllRows() .setRowLimit(100) .withColumnRange(new RangeBuilder().setLimit(1).build())) .iterator(); return new AbstractIterator<String>() { @Override protected String computeNext() { while (rowIter.hasNext()) { Row<String, UUID> row = rowIter.next(); if (!row.getColumns().isEmpty()) { return row.getKey(); } } return endOfData(); } }; }
@Override public Iterator<ByteBuffer> scanRecords(UUID dataId, @Nullable ByteBuffer from, @Nullable final ByteBuffer to, int batchSize, int limit) { final Iterator<Column<ByteBuffer>> iter = executePaginated( _keyspace.prepareQuery(CF_DEDUP_DATA, ConsistencyLevel.CL_LOCAL_QUORUM) .getKey(dataId) .withColumnRange(new RangeBuilder() .setStart(Objects.firstNonNull(from, EMPTY_BUFFER)) .setEnd(Objects.firstNonNull(to, EMPTY_BUFFER)) .setLimit(batchSize) .build()) .autoPaginate(true)); return Iterators.limit(new AbstractIterator<ByteBuffer>() { @Override protected ByteBuffer computeNext() { while (iter.hasNext()) { ByteBuffer record = iter.next().getName(); if (!record.equals(to)) { // To is exclusive return record; } } return endOfData(); } }, limit); }
@Override public Iterator<String> listChannels() { final Iterator<Row<String, ByteBuffer>> rowIter = execute( _keyspace.prepareQuery(ColumnFamilies.MANIFEST, ConsistencyLevel.CL_LOCAL_QUORUM) .getAllRows() .setRowLimit(1000) .withColumnRange(new RangeBuilder().setLimit(1).build())) .iterator(); return new AbstractIterator<String>() { @Override protected String computeNext() { while (rowIter.hasNext()) { Row<String, ByteBuffer> row = rowIter.next(); if (!row.getColumns().isEmpty()) { return row.getKey(); } } return endOfData(); } }; }
@Override public Iterator<Map.Entry<String, MaintenanceOp>> listMaintenanceOps() { final Iterator<Map<String, Object>> tableIter = _backingStore.scan(_systemTable, null, LimitCounter.max(), ReadConsistency.STRONG); return new AbstractIterator<Map.Entry<String, MaintenanceOp>>() { @Override protected Map.Entry<String, MaintenanceOp> computeNext() { while (tableIter.hasNext()) { TableJson json = new TableJson(tableIter.next()); MaintenanceOp op = getNextMaintenanceOp(json, false/*don't expose task outside this class*/); if (op != null) { return Maps.immutableEntry(json.getTable(), op); } } return endOfData(); } }; }
@Timed(name = "bv.emodb.table.AstyanaxTableDAO.list", absolute = true) @Override public Iterator<Table> list(@Nullable String fromNameExclusive, LimitCounter limit) { checkArgument(limit.remaining() > 0, "Limit must be >0"); final Iterator<Map<String, Object>> tableIter = _backingStore.scan(_systemTable, fromNameExclusive, limit, ReadConsistency.STRONG); // Filter out dropped tables. return new AbstractIterator<Table>() { @Override protected Table computeNext() { while (tableIter.hasNext()) { Table table = tableFromJson(new TableJson(tableIter.next())); if (table != null) { return table; } } return endOfData(); } }; }
@Override public Iterator<String> scanKeys(AstyanaxStorage storage, final ReadConsistency consistency) { checkNotNull(storage, "storage"); checkNotNull(consistency, "consistency"); final DeltaPlacement placement = (DeltaPlacement) storage.getPlacement(); // We just want row keys, but get at least one column so we can ignore range ghosts. final ByteBufferRange columnRange = new RangeBuilder().setLimit(1).build(); final LimitCounter unlimited = LimitCounter.max(); // Loop over all the range prefixes (2^shardsLog2 of them) and, for each, execute Cassandra queries to // page through the records with that prefix. final Iterator<ByteBufferRange> scanIter = storage.scanIterator(null); return touch(Iterators.concat(new AbstractIterator<Iterator<String>>() { @Override protected Iterator<String> computeNext() { if (scanIter.hasNext()) { ByteBufferRange keyRange = scanIter.next(); return decodeKeys(rowScan(placement, keyRange, columnRange, unlimited, consistency)); } return endOfData(); } })); }
@Override public Iterator<com.bazaarvoice.emodb.sor.api.Table> listTables(@Nullable String fromTableExclusive, long limit) { checkArgument(limit > 0, "Limit must be >0"); LimitCounter remaining = new LimitCounter(limit); final Iterator<Table> tableIter = _tableDao.list(fromTableExclusive, remaining); return remaining.limit(new AbstractIterator<com.bazaarvoice.emodb.sor.api.Table>() { @Override protected com.bazaarvoice.emodb.sor.api.Table computeNext() { while (tableIter.hasNext()) { Table table = tableIter.next(); if (!table.isInternal()) { return toDefaultTable(table); } } return endOfData(); } }); }
private Iterator<Map<String, Object>> resolveScanResults(final Iterator<Record> records, final ReadConsistency consistency, final boolean includeDeletes) { return new AbstractIterator<Map<String, Object>>() { @Override protected Map<String, Object> computeNext() { while (records.hasNext()) { Record record = records.next(); // Collapse the deltas into a Resolved object. Resolved resolved = resolve(record, consistency); // Skip deleted objects, if not desired if (!includeDeletes && !resolved.matches(Conditions.isDefined())) { continue; } // Convert to the final JSON format including intrinsic fields return toContent(resolved, consistency); } return endOfData(); } }; }