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

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

Introduction

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

Prototype

protected UnmodifiableIterator() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:org.sosy_lab.cpachecker.util.CFAUtils.java

/**
 * Return an {@link Iterable} that contains the leaving edges of a given CFANode,
 * excluding the summary edge./*from   ww  w . ja va 2s  . c  o  m*/
 */
public static FluentIterable<CFAEdge> leavingEdges(final CFANode node) {
    checkNotNull(node);
    return new FluentIterable<CFAEdge>() {

        @Override
        public Iterator<CFAEdge> iterator() {
            return new UnmodifiableIterator<CFAEdge>() {

                // the index of the next edge
                private int i = 0;

                @Override
                public boolean hasNext() {
                    return i < node.getNumLeavingEdges();
                }

                @Override
                public CFAEdge next() {
                    return node.getLeavingEdge(i++);
                }
            };
        }
    };
}

From source file:org.elasticsearch.common.collect.ImmutableOpenIntMap.java

/**
 * Returns a direct iterator over the keys.
 *//*  w w w . ja  v a  2 s .  co  m*/
public UnmodifiableIterator<VType> valuesIt() {
    final Iterator<ObjectCursor<VType>> iterator = map.values().iterator();
    return new UnmodifiableIterator<VType>() {
        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public VType next() {
            return iterator.next().value;
        }
    };
}

From source file:com.github.rconner.anansi.Elements.java

private static Iterable<PersistentList<Step<Object, String>>> mapWalker(
        final PersistentList<Step<Object, String>> walk) {
    @SuppressWarnings("unchecked")
    final Map<String, Object> map = (Map<String, Object>) walk.first().getTo();
    return new Iterable<PersistentList<Step<Object, String>>>() {
        @Override//from   w  w  w  . j av  a 2  s. co  m
        public Iterator<PersistentList<Step<Object, String>>> iterator() {
            return new UnmodifiableIterator<PersistentList<Step<Object, String>>>() {
                private final Iterator<Map.Entry<String, Object>> delegate = map.entrySet().iterator();

                @Override
                public boolean hasNext() {
                    return delegate.hasNext();
                }

                @Override
                public PersistentList<Step<Object, String>> next() {
                    final Map.Entry<String, ?> entry = delegate.next();
                    final String over = DELIMITER_PATTERN.matcher(entry.getKey()).replaceAll("\\\\$0");
                    return walk.add(Step.newInstance(entry.getValue(), '.' + over));
                }
            };
        }
    };
}

From source file:org.apache.kylin.storage.gtrecord.SegmentCubeTupleIterator.java

private Iterator<Object[]> getGTValuesIterator(final Iterator<GTRecord> records,
        final GTScanRequest scanRequest, final int[] gtDimsIdx, final int[] gtMetricsIdx) {

    boolean hasMultiplePartitions = records instanceof SortMergedPartitionResultIterator;
    if (hasMultiplePartitions && context.isStreamAggregateEnabled()) {
        // input records are ordered, leverage stream aggregator to produce possibly fewer records
        IGTScanner inputScanner = new IGTScanner() {
            public GTInfo getInfo() {
                return scanRequest.getInfo();
            }/*from   ww  w  . j  av  a2  s  .c o m*/

            public void close() throws IOException {
            }

            public Iterator<GTRecord> iterator() {
                return records;
            }
        };
        GTStreamAggregateScanner aggregator = new GTStreamAggregateScanner(inputScanner, scanRequest);
        return aggregator.valuesIterator(gtDimsIdx, gtMetricsIdx);
    }

    // simply decode records
    return new UnmodifiableIterator<Object[]>() {
        Object[] result = new Object[gtDimsIdx.length + gtMetricsIdx.length];

        public boolean hasNext() {
            return records.hasNext();
        }

        public Object[] next() {
            GTRecord record = records.next();
            for (int i = 0; i < gtDimsIdx.length; i++) {
                result[i] = record.decodeValue(gtDimsIdx[i]);
            }
            for (int i = 0; i < gtMetricsIdx.length; i++) {
                result[gtDimsIdx.length + i] = record.decodeValue(gtMetricsIdx[i]);
            }
            return result;
        }
    };
}

From source file:org.apache.giraph.edge.IdAndValueArrayEdges.java

@Override
public Iterator<Edge<I, E>> iterator() {
    // Returns an iterator that reuses objects.
    return new UnmodifiableIterator<Edge<I, E>>() {
        private int index;

        /** Representative edge object. */
        private final Edge<I, E> representativeEdge = EdgeFactory.create(
                neighborIds.getElementTypeOps().create(), neighborEdgeValues.getElementTypeOps().create());

        @Override//from  w w  w . ja  va 2s  .  co m
        public boolean hasNext() {
            return index < neighborIds.size();
        }

        @Override
        public Edge<I, E> next() {
            neighborIds.getIntoW(index, representativeEdge.getTargetVertexId());
            neighborEdgeValues.getIntoW(index, representativeEdge.getValue());
            index++;
            return representativeEdge;
        }
    };
}

From source file:org.apache.giraph.graph.DefaultVertex.java

@Override
public Iterable<E> getAllEdgeValues(final I targetVertexId) {
    // If the OutEdges implementation has a specialized random-access
    // method, we use that; otherwise, we scan the edges.
    if (edges instanceof MultiRandomAccessOutEdges) {
        return ((MultiRandomAccessOutEdges<I, E>) edges).getAllEdgeValues(targetVertexId);
    } else {//w  ww .jav a 2 s  .c  o  m
        return new Iterable<E>() {
            @Override
            public Iterator<E> iterator() {
                return new UnmodifiableIterator<E>() {
                    /** Iterator over all edges. */
                    private Iterator<Edge<I, E>> edgeIterator = edges.iterator();
                    /** Last matching edge found. */
                    private Edge<I, E> currentEdge;

                    @Override
                    public boolean hasNext() {
                        while (edgeIterator.hasNext()) {
                            currentEdge = edgeIterator.next();
                            if (currentEdge.getTargetVertexId().equals(targetVertexId)) {
                                return true;
                            }
                        }
                        return false;
                    }

                    @Override
                    public E next() {
                        return currentEdge.getValue();
                    }
                };
            }
        };
    }
}

From source file:org.elasticsearch.search.aggregations.AggregatorFactories.java

/**
 * Create all aggregators so that they can be consumed with multiple buckets.
 *///from   ww w  . j a va  2  s  . c  om
public Aggregator[] createSubAggregators(Aggregator parent, final long estimatedBucketsCount) {
    Aggregator[] aggregators = new Aggregator[count()];
    for (int i = 0; i < factories.length; ++i) {
        final AggregatorFactory factory = factories[i];
        final Aggregator first = createAndRegisterContextAware(parent.context(), factory, parent,
                estimatedBucketsCount);
        if (first.bucketAggregationMode() == BucketAggregationMode.MULTI_BUCKETS) {
            // This aggregator already supports multiple bucket ordinals, can be used directly
            aggregators[i] = first;
            continue;
        }
        // the aggregator doesn't support multiple ordinals, let's wrap it so that it does.
        aggregators[i] = new Aggregator(first.name(), BucketAggregationMode.MULTI_BUCKETS,
                AggregatorFactories.EMPTY, 1, first.context(), first.parent()) {

            ObjectArray<Aggregator> aggregators;

            {
                // if estimated count is zero, we at least create a single aggregator.
                // The estimated count is just an estimation and we can't rely on how it's estimated (that is, an
                // estimation of 0 should not imply that we'll end up without any buckets)
                long arraySize = estimatedBucketsCount > 0 ? estimatedBucketsCount : 1;
                aggregators = bigArrays.newObjectArray(arraySize);
                aggregators.set(0, first);
                for (long i = 1; i < arraySize; ++i) {
                    aggregators.set(i, createAndRegisterContextAware(parent.context(), factory, parent,
                            estimatedBucketsCount));
                }
            }

            @Override
            public boolean shouldCollect() {
                return first.shouldCollect();
            }

            @Override
            protected void doPostCollection() {
                for (long i = 0; i < aggregators.size(); ++i) {
                    final Aggregator aggregator = aggregators.get(i);
                    if (aggregator != null) {
                        aggregator.postCollection();
                    }
                }
            }

            @Override
            public void collect(int doc, long owningBucketOrdinal) throws IOException {
                aggregators = bigArrays.grow(aggregators, owningBucketOrdinal + 1);
                Aggregator aggregator = aggregators.get(owningBucketOrdinal);
                if (aggregator == null) {
                    aggregator = createAndRegisterContextAware(parent.context(), factory, parent,
                            estimatedBucketsCount);
                    aggregators.set(owningBucketOrdinal, aggregator);
                }
                aggregator.collect(doc, 0);
            }

            @Override
            public void setNextReader(AtomicReaderContext reader) {
            }

            @Override
            public InternalAggregation buildAggregation(long owningBucketOrdinal) {
                // The bucket ordinal may be out of range in case of eg. a terms/filter/terms where
                // the filter matches no document in the highest buckets of the first terms agg
                if (owningBucketOrdinal >= aggregators.size() || aggregators.get(owningBucketOrdinal) == null) {
                    return first.buildEmptyAggregation();
                } else {
                    return aggregators.get(owningBucketOrdinal).buildAggregation(0);
                }
            }

            @Override
            public InternalAggregation buildEmptyAggregation() {
                return first.buildEmptyAggregation();
            }

            @Override
            public void doRelease() {
                final Iterable<Aggregator> aggregatorsIter = new Iterable<Aggregator>() {

                    @Override
                    public Iterator<Aggregator> iterator() {
                        return new UnmodifiableIterator<Aggregator>() {

                            long i = 0;

                            @Override
                            public boolean hasNext() {
                                return i < aggregators.size();
                            }

                            @Override
                            public Aggregator next() {
                                return aggregators.get(i++);
                            }

                        };
                    }

                };
                Releasables.release(Iterables.concat(aggregatorsIter, Collections.singleton(aggregators)));
            }
        };
    }
    return aggregators;
}

From source file:org.elasticsearch.index.mapper.internal.FieldNamesFieldMapper.java

static Iterable<String> extractFieldNames(final String fullPath) {
    return new Iterable<String>() {
        @Override//from w w w  . ja va2s .  co m
        public Iterator<String> iterator() {
            return new UnmodifiableIterator<String>() {

                int endIndex = nextEndIndex(0);

                private int nextEndIndex(int index) {
                    while (index < fullPath.length() && fullPath.charAt(index) != '.') {
                        index += 1;
                    }
                    return index;
                }

                @Override
                public boolean hasNext() {
                    return endIndex <= fullPath.length();
                }

                @Override
                public String next() {
                    final String result = fullPath.substring(0, endIndex);
                    endIndex = nextEndIndex(endIndex + 1);
                    return result;
                }

            };
        }
    };
}

From source file:com.palantir.giraffe.file.UniformPath.java

@Override
public Iterator<UniformPath> iterator() {
    return new UnmodifiableIterator<UniformPath>() {
        private int i = 0;

        @Override//  w ww.  j a  va 2s.  c o  m
        public boolean hasNext() {
            return i < getNameCount();
        }

        @Override
        public UniformPath next() {
            if (i < getNameCount()) {
                UniformPath result = getName(i);
                i++;
                return result;
            } else {
                throw new NoSuchElementException();
            }
        }
    };
}

From source file:org.opendaylight.yangtools.yang.model.api.SchemaPath.java

/**
 * Returns the list of nodes which need to be traversed to get from this
 * node to the starting point (root for absolute SchemaPaths).
 *
 * @return list of <code>qname</code> instances which represents
 *         path from the schema node towards the root.
 *//*from  w  ww.j  av a  2 s. c  o m*/
public Iterable<QName> getPathTowardsRoot() {
    return () -> new UnmodifiableIterator<QName>() {
        private SchemaPath current = SchemaPath.this;

        @Override
        public boolean hasNext() {
            return current.parent != null;
        }

        @Override
        public QName next() {
            if (current.parent != null) {
                final QName ret = current.qname;
                current = current.parent;
                return ret;
            } else {
                throw new NoSuchElementException("No more elements available");
            }
        }
    };
}