Example usage for com.google.common.collect AbstractIterator AbstractIterator

List of usage examples for com.google.common.collect AbstractIterator AbstractIterator

Introduction

In this page you can find the example usage for com.google.common.collect AbstractIterator AbstractIterator.

Prototype

protected AbstractIterator() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:com.facebook.presto.util.PrestoIterators.java

public static <T> Iterator<T> closeWhenExhausted(Iterator<T> iterator, AutoCloseable resource) {
    requireNonNull(iterator, "iterator is null");
    requireNonNull(resource, "resource is null");

    return new AbstractIterator<T>() {
        @Override//from  ww w.j a v a2 s.c  om
        protected T computeNext() {
            if (iterator.hasNext()) {
                return iterator.next();
            } else {
                try {
                    resource.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return endOfData();
            }
        }
    };
}

From source file:org.calrissian.mango.collect.Iterators2.java

/**
 * If we can assume the iterator is sorted, return the distinct elements. This only works
 * if the data provided is sorted.//from  w  w w . j a va2  s  .  co  m
 */
public static <T> Iterator<T> distinct(final Iterator<T> iterator) {
    checkNotNull(iterator);
    return new AbstractIterator<T>() {
        T current = null;

        @Override
        protected T computeNext() {
            if (iterator.hasNext()) {
                if (current == null) {
                    current = iterator.next();
                    return current;
                } else {
                    T next = iterator.next();
                    while (current.equals(next)) {
                        if (iterator.hasNext()) {
                            next = iterator.next();
                        } else {
                            return endOfData();
                        }
                    }
                    current = next;
                    return current;
                }
            } else
                return endOfData();
        }
    };
}

From source file:com.nitayjoffe.util.iterators.IteratorUtils.java

public static <E> Iterator<E> interleave(final Iterable<Iterator<E>> iterators) {
    return new AbstractIterator<E>() {
        private Queue<Iterator<E>> queue = Lists.newLinkedList(iterators);

        @Override/*from ww w. j av a 2s  .co m*/
        protected E computeNext() {
            while (!queue.isEmpty()) {
                Iterator<E> topIter = queue.poll();
                if (topIter.hasNext()) {
                    E result = topIter.next();
                    queue.offer(topIter);
                    return result;
                }
            }
            return endOfData();
        }
    };
}

From source file:org.asoem.greyfish.utils.collect.Chains.java

public static <T> Iterable<T> of(@Nullable final T root,
        final Function<? super T, ? extends T> nextElementFunction) {
    checkNotNull(nextElementFunction);//from   ww w  .j av a 2  s .c o m

    if (root == null) {
        return ImmutableList.of();
    } else {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                return new AbstractIterator<T>() {
                    T next = root;

                    @Override
                    protected T computeNext() {
                        if (next == null) {
                            return endOfData();
                        } else {
                            final T ret = next;
                            next = nextElementFunction.apply(next);
                            return ret;
                        }
                    }
                };
            }
        };
    }
}

From source file:org.locationtech.geogig.storage.StagingDbCompositionHelper.java

public static Iterator<RevObject> getAll(final ObjectDatabase objectDb, final ObjectDatabase stagingDb,
        final Iterable<ObjectId> ids, final BulkOpListener listener) {

    final List<ObjectId> missingInStaging = Lists.newLinkedList();

    final int limit = 10000;

    final BulkOpListener stagingListener = new BulkOpListener.ForwardingListener(listener) {
        @Override/* ww w . j av  a  2s.  c o m*/
        public void notFound(ObjectId id) {
            missingInStaging.add(id);
        }
    };

    final Iterator<RevObject> foundInStaging = stagingDb.getAll(ids, stagingListener);

    Iterator<RevObject> compositeIterator = new AbstractIterator<RevObject>() {

        Iterator<RevObject> forwardedToObjectDb = Iterators.emptyIterator();

        @Override
        protected RevObject computeNext() {
            if (forwardedToObjectDb.hasNext()) {
                return forwardedToObjectDb.next();
            }
            if (missingInStaging.size() >= limit) {
                List<ObjectId> missing = new ArrayList<ObjectId>(missingInStaging);
                missingInStaging.clear();

                forwardedToObjectDb = objectDb.getAll(missing, listener);
                return computeNext();
            }
            if (foundInStaging.hasNext()) {
                return foundInStaging.next();
            } else if (forwardedToObjectDb.hasNext()) {
                return forwardedToObjectDb.next();
            } else if (!missingInStaging.isEmpty()) {
                List<ObjectId> missing = new ArrayList<ObjectId>(missingInStaging);
                missingInStaging.clear();
                forwardedToObjectDb = objectDb.getAll(missing, listener);
                return computeNext();
            }
            return endOfData();
        }
    };

    return compositeIterator;
}

From source file:com.facebook.presto.sql.planner.iterative.RuleStore.java

private static Iterator<Class<? extends PlanNode>> ancestors(Class<? extends PlanNode> planNodeClass) {
    return new AbstractIterator<Class<? extends PlanNode>>() {
        private Class<?> current = planNodeClass;

        @Override/*from   w ww.j  a v a2s .  c  o  m*/
        protected Class<? extends PlanNode> computeNext() {
            if (!PlanNode.class.isAssignableFrom(current)) {
                return endOfData();
            }

            Class<? extends PlanNode> result = (Class<? extends PlanNode>) current;
            current = current.getSuperclass();

            return result;
        }
    };
}

From source file:org.jclouds.googlecloud.internal.ListPages.java

public static <T> Iterable<T> concat(final Iterator<ListPage<T>> input) {
    return new Iterable<T>() {
        @Override/*  w  w  w . j  a  v  a 2  s .c  om*/
        public Iterator<T> iterator() {
            return Iterators.concat(new AbstractIterator<Iterator<T>>() {
                @Override
                protected Iterator<T> computeNext() {
                    return input.hasNext() ? input.next().iterator() : endOfData();
                }
            });
        }
    };
}

From source file:org.apache.cassandra.noTTL.NoTTLAbstractCell.java

public static Iterator<OnDiskAtom> onDiskIterator(final DataInput in, final ColumnSerializer.Flag flag,
        final int expireBefore, final Version version, final CellNameType type) {
    return new AbstractIterator<OnDiskAtom>() {
        protected OnDiskAtom computeNext() {
            OnDiskAtom atom;/*  www . ja  v a  2s . c o  m*/
            try {
                atom = new NoTTLSerializer(type).deserializeFromSSTable(in, flag, expireBefore, version);
            } catch (IOException e) {
                throw new IOError(e);
            }
            if (atom == null)
                return endOfData();

            return atom;
        }
    };
}

From source file:com.stackframe.xml.DOMUtilities.java

/**
 * Get a view of a NodeList as an Iterator.
 *
 * @param nodeList the NodeList to iterate over
 * @return an Iterator that iterates over nodeList
 *//*from ww w .  j  a va  2  s .  c  o m*/
public static Iterator<Node> iterator(final NodeList nodeList) {
    return new AbstractIterator<Node>() {
        private int index;

        @Override
        protected Node computeNext() {
            if (index == nodeList.getLength()) {
                return endOfData();
            }

            return nodeList.item(index++);
        }
    };
}

From source file:org.locationtech.geogig.storage.impl.StagingDbCompositionHelper.java

public static Iterator<RevObject> getAll(final ObjectStore objectDb, final ObjectStore stagingDb,
        final Iterable<ObjectId> ids, final BulkOpListener listener) {

    final List<ObjectId> missingInStaging = Lists.newLinkedList();

    final int limit = 10000;

    final BulkOpListener stagingListener = new BulkOpListener.ForwardingListener(listener) {
        @Override/*from   w w w  . j  a v a  2s . co m*/
        public void notFound(ObjectId id) {
            missingInStaging.add(id);
        }
    };

    final Iterator<RevObject> foundInStaging = stagingDb.getAll(ids, stagingListener);

    Iterator<RevObject> compositeIterator = new AbstractIterator<RevObject>() {

        Iterator<RevObject> forwardedToObjectDb = Collections.emptyIterator();

        @Override
        protected RevObject computeNext() {
            if (forwardedToObjectDb.hasNext()) {
                return forwardedToObjectDb.next();
            }
            if (missingInStaging.size() >= limit) {
                List<ObjectId> missing = new ArrayList<ObjectId>(missingInStaging);
                missingInStaging.clear();

                forwardedToObjectDb = objectDb.getAll(missing, listener);
                return computeNext();
            }
            if (foundInStaging.hasNext()) {
                return foundInStaging.next();
            } else if (forwardedToObjectDb.hasNext()) {
                return forwardedToObjectDb.next();
            } else if (!missingInStaging.isEmpty()) {
                List<ObjectId> missing = new ArrayList<ObjectId>(missingInStaging);
                missingInStaging.clear();
                forwardedToObjectDb = objectDb.getAll(missing, listener);
                return computeNext();
            }
            return endOfData();
        }
    };

    return compositeIterator;
}