下面列出了怎么用com.google.common.collect.AbstractSequentialIterator的API类实例代码及写法,或者点击链接到github查看源代码。
private static Iterable<Double> doubleSequence(double start, double step, int items)
{
return () -> new AbstractSequentialIterator<Double>(start)
{
private int item;
@Override
protected Double computeNext(Double previous)
{
if (item >= items) {
return null;
}
item++;
return previous + step;
}
};
}
private Iterator<LocatedFileStatus> listPrefix(Path path)
{
String key = keyFromPath(path);
if (!key.isEmpty()) {
key += PATH_SEPARATOR;
}
ListObjectsV2Request request = new ListObjectsV2Request()
.withBucketName(getBucketName(uri))
.withPrefix(key)
.withDelimiter(PATH_SEPARATOR)
.withRequesterPays(requesterPaysEnabled);
STATS.newListObjectsCall();
Iterator<ListObjectsV2Result> listings = new AbstractSequentialIterator<ListObjectsV2Result>(s3.listObjectsV2(request))
{
@Override
protected ListObjectsV2Result computeNext(ListObjectsV2Result previous)
{
if (!previous.isTruncated()) {
return null;
}
request.setContinuationToken(previous.getNextContinuationToken());
return s3.listObjectsV2(request);
}
};
return Iterators.concat(Iterators.transform(listings, this::statusFromListing));
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInWriteQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInAccessQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInWriteQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInAccessQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInWriteQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInAccessQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInWriteQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInAccessQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInWriteQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInAccessQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInWriteQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInAccessQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInWriteQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<ReferenceEntry<K, V>> iterator() {
return new AbstractSequentialIterator<ReferenceEntry<K, V>>(peek()) {
@Override
protected ReferenceEntry<K, V> computeNext(ReferenceEntry<K, V> previous) {
ReferenceEntry<K, V> next = previous.getNextInAccessQueue();
return (next == head) ? null : next;
}
};
}
@Override
public Iterator<InetAddress> iterator() {
return new AbstractSequentialIterator<InetAddress>(ip) {
@Override
protected InetAddress computeNext(InetAddress previous) {
if (InetAddresses.isMaximum(previous)) {
return null;
}
InetAddress next = InetAddresses.increment(previous);
return contains(next) ? next : null;
}
};
}
/**
* Return each method in the inheritance hierarchy of method in the order described by
* {@link AuthorizingParam}.
*
* @see org.apache.aurora.scheduler.http.api.security.AuthorizingParam
*/
private static Iterable<Method> getCandidateMethods(final Method method) {
return () -> new AbstractSequentialIterator<Method>(method) {
@Override
protected Method computeNext(Method previous) {
String name = previous.getName();
Class<?>[] parameterTypes = previous.getParameterTypes();
Class<?> declaringClass = previous.getDeclaringClass();
if (declaringClass.isInterface()) {
return null;
}
Iterable<Class<?>> searchOrder = ImmutableList.<Class<?>>builder()
.addAll(Optional.ofNullable(declaringClass.getSuperclass())
.map(ImmutableSet::of)
.orElse(ImmutableSet.of()))
.addAll(ImmutableList.copyOf(declaringClass.getInterfaces()))
.build();
for (Class<?> klazz : searchOrder) {
try {
return klazz.getMethod(name, parameterTypes);
} catch (NoSuchMethodException ignored) {
// Expected.
}
}
return null;
}
};
}
private Iterator<LocatedFileStatus> listPrefix(Path path)
{
String key = keyFromPath(path);
if (!key.isEmpty()) {
key += PATH_SEPARATOR;
}
ListObjectsRequest request = new ListObjectsRequest()
.withBucketName(uri.getHost())
.withPrefix(key)
.withDelimiter(PATH_SEPARATOR);
STATS.newListObjectsCall();
Iterator<ObjectListing> listings = new AbstractSequentialIterator<ObjectListing>(s3.listObjects(request))
{
@Override
protected ObjectListing computeNext(ObjectListing previous)
{
if (!previous.isTruncated()) {
return null;
}
return s3.listNextBatchOfObjects(previous);
}
};
return Iterators.concat(Iterators.transform(listings, this::statusFromListing));
}
/**
* Constructor that initializes the temp ID generator based on the ID of the root node.
*
* @param adGroupId the ID of the ad group
* @param biddingStrategyConfig the bidding strategy configuration of the ad group
* @param rootNode the root node of the tree
*/
ProductPartitionTreeImpl(long adGroupId, BiddingStrategyConfiguration biddingStrategyConfig,
ProductPartitionNode rootNode) {
this.adGroupId = adGroupId;
this.biddingStrategyConfig =
Preconditions.checkNotNull(biddingStrategyConfig, "Null bidding strategy configuration");
this.root = Preconditions.checkNotNull(rootNode, "Null root node");
long startingTempId;
this.dimensionComparator = new ProductDimensionComparator();
if (this.root.getProductPartitionId() < 0L) {
// The root has a temporary ID, so all changes made to this tree should result in ADD
// operations.
originalRoot = null;
startingTempId = -1L;
} else {
// Set originalRoot to a deep copy of the root node.
originalRoot =
new ProductPartitionNode(null, root.getDimension(), root.getProductPartitionId(),
this.dimensionComparator);
long minimumId = cloneChildrenToNewParent(originalRoot, root.getChildren(),
originalRoot.getProductPartitionId());
// The starting temp ID should be -1 if all nodes are non-temporary (have positive IDs),
// else start at one less than the lowest ID found in the tree.
startingTempId = minimumId >= 0L ? -1L : minimumId - 1L;
}
this.idGenerator = new AbstractSequentialIterator<Long>(startingTempId) {
@Override
protected Long computeNext(Long previous) {
return Long.MIN_VALUE == previous.longValue() ? null : previous - 1;
}
};
}