Example usage for com.google.common.collect Iterators size

List of usage examples for com.google.common.collect Iterators size

Introduction

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

Prototype

public static int size(Iterator<?> iterator) 

Source Link

Document

Returns the number of elements remaining in iterator .

Usage

From source file:org.polymap.model2.engine.UnitOfWorkImpl.java

@Override
public <T extends Entity> Query<T> query(final Class<T> entityClass) {
    checkOpen();/*  www.j ava2s  .  c om*/
    return new Query(entityClass) {
        @Override
        public ResultSet<T> execute() {
            // the preloaded entity from the CompositeStateReference is used to build the
            // entity; but we are not keeping a strong ref to it in order to allow the cache to
            // evict the entity state; 

            // we are either not keeping a strong ref to the CompositeStateReferences as they
            // may contain refs to the states which would kept in memory for the lifetime of
            // the ResultSet otherwise

            // unmodified
            final StoreResultSet rs = storeUow.executeQuery(this);
            IteratorBuilder<T> unmodifiedResults = IteratorBuilder.on(rs)
                    .map(ref -> entity(entityClass, ref.id(), ref)).filter(entity -> {
                        EntityStatus status = entity.status();
                        assert status != EntityStatus.CREATED;
                        return status == EntityStatus.LOADED;
                    });

            // modified
            // XXX not cached, done for every call to iterator()
            IteratorBuilder<T> modifiedResults = (IteratorBuilder<T>) IteratorBuilder.on(modified.values())
                    .filter(entity -> {
                        if (entity.getClass().equals(entityClass)
                                && (entity.status() == CREATED || entity.status() == MODIFIED)) {
                            if (expression == null) {
                                return true;
                            } else if (expression instanceof BooleanExpression) {
                                return expression.evaluate(entity);
                            } else {
                                return storeUow.evaluate(entity.state(), expression);
                            }
                        }
                        return false;
                    });

            // ResultSet, caching the ids for subsequent runs
            return new ResultSet<T>() {

                /** null after one full run */
                private Iterator<T> results = unmodifiedResults.concat(modifiedResults);
                private List<Object> cachedIds = new ArrayList(1024);
                /** The cached cachedSize; not synchronized */
                private int cachedSize = -1;

                @Override
                public Iterator<T> iterator() {
                    return new Iterator<T>() {
                        int index = -1;

                        @Override
                        public boolean hasNext() {
                            if (index + 1 < cachedIds.size() || (results != null && results.hasNext())) {
                                return true;
                            } else {
                                rs.close();
                                results = null;
                                return false;
                            }
                        }

                        @Override
                        public T next() {
                            if (++index < cachedIds.size()) {
                                return entity(entityClass, cachedIds.get(index), null);
                            } else {
                                assert index == cachedIds.size() : "index == cachedIds.size(): " + index + ", "
                                        + cachedIds.size();
                                T result = results.next();
                                cachedIds.add(result.id());
                                return result;
                            }
                        }
                    };
                }

                @Override
                public int size() {
                    if (cachedSize == -1) {
                        cachedSize = results == null ? cachedIds.size()
                                : modified.isEmpty() ? rs.size() : Iterators.size(iterator());
                    }
                    return cachedSize;
                }

                @Override
                public Stream<T> stream() {
                    // XXX use cachedIds.size() if available
                    return StreamSupport.stream(spliterator(), false);
                }

                @Override
                public void close() {
                    rs.close();
                    results = null;
                    cachedIds = null;
                }
            };
        }
    };
}

From source file:org.eclipse.emf.compare.EMFCompare.java

/**
 * Log useful informations at the end of the comparison process.
 * /*from www. j  av  a  2s  . c  om*/
 * @param comparison
 *            The comparison
 * @param start
 *            The time when the method start
 */
private void logEndOfComparison(Comparison comparison, long start) {
    long duration = System.currentTimeMillis() - start;
    int diffQuantity = comparison.getDifferences().size();
    int conflictQuantity = comparison.getConflicts().size();
    int matchQuantity = 0;
    EList<Match> matches = comparison.getMatches();
    for (Match match : matches) {
        matchQuantity++;
        matchQuantity += Iterators.size(match.getAllSubmatches().iterator());
    }
    LOGGER.info("compare() - FINISH - " + matchQuantity + " matches, " + diffQuantity + " diffs and " //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
            + conflictQuantity + " conflicts found in " + duration + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
}

From source file:org.apache.abdera2.common.misc.MapRed.java

public static <K, V> Reducer<K, V, K, Integer> countingReducer() {
    return new Reducer<K, V, K, Integer>() {
        public void reduce(K key, Iterator<V> vals, Collector<K, Integer> context) {
            context.collect(key, Iterators.size(vals));
        }/*  w  w  w . j  a  v  a 2 s.  co  m*/
    };
}

From source file:nl.knaw.huygens.timbuctoo.storage.graph.tinkerpop.TinkerPopStorage.java

@Override
public long countEntities(Class<? extends Entity> type) {
    Class<? extends Entity> baseClass = TypeRegistry.getBaseClass(type);
    Iterator<Vertex> vertices = lowLevelAPI.getLatestVerticesOf(baseClass);

    return Iterators.size(vertices);
}

From source file:nl.knaw.huygens.timbuctoo.storage.graph.tinkerpop.TinkerPopStorage.java

@Override
public long countRelations(Class<? extends Relation> relationType) {
    @SuppressWarnings("unchecked")
    Class<? extends Relation> baseClass = (Class<? extends Relation>) TypeRegistry
            .toBaseDomainEntity(relationType);

    Iterator<Edge> edges = lowLevelAPI.getLatestEdgesOf(baseClass);

    return Iterators.size(edges);
}

From source file:org.locationtech.geogig.remotes.pack.PushOp.java

private int depthTo(ObjectId tip, ObjectId ca) {
    Iterator<RevCommit> commits = command(LogOp.class)//
            .setSince(ca)//
            .setUntil(tip)//
            .call();//from   w  ww . j  a v  a2s  . com
    int depth = Iterators.size(commits);
    return depth;
}

From source file:org.apache.hadoop.hive.metastore.hbase.HBaseReadWrite.java

int getDatabaseCount() throws IOException {
    Filter fil = new FirstKeyOnlyFilter();
    Iterator<Result> iter = scan(DB_TABLE, fil);
    return Iterators.size(iter);
}

From source file:org.jglue.totorom.GlobalVertexTraversal.java

public long count() {
    return Iterators.size(simpleIterator());
}

From source file:org.eclipse.xtext.xbase.lib.IteratorExtensions.java

/**
 * Returns the number of elements in {@code iterator}.
 * The given iterator is left exhausted.
 * //  w ww.j ava 2  s . co m
 * @param iterator
 *            the iterator. May not be <code>null</code>.
 * @return the number of elements in {@code iterator}.
 */
public static int size(Iterator<?> iterator) {
    return Iterators.size(iterator);
}

From source file:com.netxforge.editing.base.impl.ScreenFormService.java

/**
 * Runs through the list of screens on the stack and adds an entry on the
 * screenpath composite./*from  ww w. jav  a 2  s .co m*/
 */
private void updateScreenPath() {

    Iterator<Composite> elements = screenStack.iterator();
    int count = Iterators.size(elements);
    elements = screenStack.iterator();
    this.screenBody.clearScreenPath();
    if (count > 0) {
        this.screenBody.setScreenPathOn();
        while (elements.hasNext()) {
            Composite composite = elements.next();
            if (ScreenUtil.isScreen(composite)) {
                createPathEntry(ScreenUtil.screenFor(composite));
            }
            if (elements.hasNext()) {
                formToolkit.createLabel(this.screenBody.getScreenPath(), "->");
            }
        }

        formToolkit.createLabel(this.screenBody.getScreenPath(), "->");

        this.createPathEntry(this.getActiveScreen());

        this.screenBody.getScreenPath().layout();
    } else {
        this.screenBody.setScreenPathOff();
    }
}