com.google.common.collect.AbstractIterator Java Examples

The following examples show how to use com.google.common.collect.AbstractIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OrcTester.java    From presto with Apache License 2.0 6 votes vote down vote up
private static <T> List<T> insertNullEvery(int n, List<T> iterable)
{
    return newArrayList(() -> new AbstractIterator<T>()
    {
        private final Iterator<T> delegate = iterable.iterator();
        private int position;

        @Override
        protected T computeNext()
        {
            position++;
            if (position > n) {
                position = 0;
                return null;
            }

            if (!delegate.hasNext()) {
                return endOfData();
            }

            return delegate.next();
        }
    });
}
 
Example #2
Source File: PartitionedConsumption.java    From presto with Apache License 2.0 6 votes vote down vote up
Iterator<Partition<T>> beginConsumption()
{
    Queue<Partition<T>> partitions = new ArrayDeque<>(requireNonNull(this.partitions, "partitions is already null"));
    if (consumed.incrementAndGet() >= consumersCount) {
        // Unreference futures to allow GC
        this.partitions = null;
    }
    return new AbstractIterator<Partition<T>>()
    {
        @Override
        protected Partition<T> computeNext()
        {
            Partition<T> next = partitions.poll();
            if (next != null) {
                return next;
            }
            else {
                return endOfData();
            }
        }
    };
}
 
Example #3
Source File: CharSource.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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();
        }
      };
    }
  };
}
 
Example #4
Source File: RcFileTester.java    From presto with Apache License 2.0 6 votes vote down vote up
private static <T> Iterable<T> insertNullEvery(int n, Iterable<T> iterable)
{
    return () -> new AbstractIterator<T>()
    {
        private final Iterator<T> delegate = iterable.iterator();
        private int position;

        @Override
        protected T computeNext()
        {
            position++;
            if (position > n) {
                position = 0;
                return null;
            }

            if (!delegate.hasNext()) {
                return endOfData();
            }

            return delegate.next();
        }
    };
}
 
Example #5
Source File: ArrayListTextSegmentSet.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Iterator<T> iteratorAfter(T segment) {
	int searchResult = 1 + Collections.binarySearch(contents, segment, new RegionComparator<T>(getRegionAccess()));
	if (searchResult < 1) {
		return Collections.emptyIterator();
	}
	return new AbstractIterator<T>() {
		private int index = searchResult;

		@Override
		protected T computeNext() {
			if (index >= contents.size())
				return endOfData();
			return contents.get(index++);
		}
	};
}
 
Example #6
Source File: RepoService.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
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;
		}
	};
}
 
Example #7
Source File: CharSource.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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();
        }
      };
    }
  };
        }
 
Example #8
Source File: SeparatorRegions.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Iterator<ObjectEntry<T, R>> iterator() {
	return new AbstractIterator<ObjectEntry<T, R>>() {
		ObjectEntry<T, R> next = first;

		@Override
		protected ObjectEntry<T, R> computeNext() {
			if (next == null) {
				return endOfData();
			}
			ObjectEntry<T, R> current = next;
			next = next.getTrailingObject();
			return current;
		}
	};
}
 
Example #9
Source File: SeparatorRegions.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public Iterable<SeparatorEntry<T, R>> separators() {
	return new Iterable<SeparatorEntry<T, R>>() {
		@Override
		public Iterator<SeparatorEntry<T, R>> iterator() {

			return new AbstractIterator<SeparatorEntry<T, R>>() {
				SeparatorEntry<T, R> next = first.getTrailingSeparator();

				@Override
				protected SeparatorEntry<T, R> computeNext() {
					if (next == null) {
						return endOfData();
					}
					SeparatorEntry<T, R> current = next;
					next = next.getTrailingSeparator();
					return current;
				}
			};
		}
	};
}
 
Example #10
Source File: IteratorExtensions.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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();
		}
	};
}
 
Example #11
Source File: IteratorExtensions.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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();
		}
	};
}
 
Example #12
Source File: IteratorExtensions.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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();
			}
		}
	};
}
 
Example #13
Source File: IteratorExtensions.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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();
			}
		}
	};
}
 
Example #14
Source File: SimpleKafkaConsumer.java    From twill with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
  };
}
 
Example #15
Source File: FileSystemCounterGroup.java    From big-c with Apache License 2.0 6 votes vote down vote up
@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();
    }
  };
}
 
Example #16
Source File: MetricsRecordFiltered.java    From big-c with Apache License 2.0 6 votes vote down vote up
@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();
        }
      };
    }
  };
}
 
Example #17
Source File: CodeFormatter.java    From yql-plus with Apache License 2.0 6 votes vote down vote up
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();
                }
            };
        }
    };
}
 
Example #18
Source File: DefaultBlobStore.java    From emodb with Apache License 2.0 6 votes vote down vote up
@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();
        }
    });
}
 
Example #19
Source File: AstyanaxQueueDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
@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();
        }
    };
}
 
Example #20
Source File: CompositeNodeTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected Iterable<ILeafNode> getReverseLeafNodes(final AbstractNode node) {
	return Iterables.filter(new Iterable<INode>() {
		@Override
		public Iterator<INode> iterator() {
			return new AbstractIterator<INode>() {

				private BidiTreeIterator<AbstractNode> delegate = node.basicIterator();
				
				@Override
				protected INode computeNext() {
					if (delegate.hasPrevious())
						return delegate.previous();
					return endOfData();
				}
			};
		}
	}, ILeafNode.class);
}
 
Example #21
Source File: AstyanaxEventReaderDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
@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();
        }
    };
}
 
Example #22
Source File: AstyanaxTableDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<Map.Entry<String, MaintenanceOp>> listMaintenanceOps() {
    final Iterator<Map<String, Object>> tableIter =
            _backingStore.scan(_systemTable, null, LimitCounter.max(), ReadConsistency.STRONG);
    final Supplier<List<TableEventDatacenter>> tableEventDatacenterSupplier = Suppliers.memoize(this::getTableEventDatacenters);
    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*/, tableEventDatacenterSupplier);
                if (op != null) {
                    return Maps.immutableEntry(json.getTable(), op);
                }
            }
            return endOfData();
        }
    };
}
 
Example #23
Source File: AstyanaxTableDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
@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();
        }
    };
}
 
Example #24
Source File: ItemHandlerWrapper.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Iterator<ItemStack> iterator()
{
    return new AbstractIterator<ItemStack>()
    {
        int current = 0;

        @Override
        protected ItemStack computeNext()
        {
            if (current >= getSizeInventory())
            {
                return endOfData();
            }
            ItemStack stack = getStackInSlot(current++);
            return stack;
        }
    };
}
 
Example #25
Source File: AstyanaxBlockedDataReaderDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
@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();
        }
    }));
}
 
Example #26
Source File: DefaultDataStore.java    From emodb with Apache License 2.0 6 votes vote down vote up
@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();
        }
    });
}
 
Example #27
Source File: DefaultDataStore.java    From emodb with Apache License 2.0 6 votes vote down vote up
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();
        }
    };
}
 
Example #28
Source File: CharSource.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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();
        }
      };
    }
  };
        }
 
Example #29
Source File: CharSource.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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();
        }
      };
    }
  };
        }
 
Example #30
Source File: CharSource.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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();
        }
      };
    }
  };
        }