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

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

Introduction

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

Prototype

public static <F, T> Iterator<T> transform(final Iterator<F> fromIterator,
        final Function<? super F, ? extends T> function) 

Source Link

Document

Returns an iterator that applies function to each element of fromIterator .

Usage

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

@Override
public Iterator<GTRecord> iterator() {
    Iterator<PartitionResultIterator> iterators = Iterators.transform(blocks,
            new Function<byte[], PartitionResultIterator>() {
                public PartitionResultIterator apply(byte[] input) {
                    return new PartitionResultIterator(input, info, columns);
                }//w  ww  .  ja  va  2s  .  co m
            });

    if (!needSorted) {
        logger.debug("Using Iterators.concat to pipeline partition results");
        return Iterators.concat(iterators);
    }

    List<PartitionResultIterator> partitionResults = Lists.newArrayList(iterators);
    if (partitionResults.size() == 1) {
        return partitionResults.get(0);
    }
    logger.debug("Using SortMergedPartitionResultIterator to merge {} partition results",
            partitionResults.size());
    return new SortMergedPartitionResultIterator(partitionResults, info, GTRecord.getComparator(groupByDims));
}

From source file:com.yammer.collections.transforming.TransformingCollection.java

@SuppressWarnings("NullableProblems")
@Override
public Iterator<F> iterator() {
    return Iterators.transform(backingCollection.iterator(), fromFunction);
}

From source file:org.apache.gobblin.util.request_allocation.PriorityIterableBasedRequestAllocator.java

@Override
public AllocatedRequestsIterator<T> allocateRequests(Iterator<? extends Requestor<T>> requestors,
        ResourcePool resourcePool) {//  w w  w . jav a 2 s. com
    final ConcurrentBoundedPriorityIterable<T> iterable = new ConcurrentBoundedPriorityIterable<>(
            this.configuration.getPrioritizer(), this.configuration.getResourceEstimator(),
            this.configuration.getStoreRejectedRequestsSetting(), resourcePool);

    final Iterator<T> joinIterator = getJoinIterator(requestors, iterable);

    if (this.configuration.getAllowedThreads() <= 1) {
        while (joinIterator.hasNext()) {
            iterable.add(joinIterator.next());
        }
    } else {

        IteratorExecutor<Void> executor = new IteratorExecutor<>(
                Iterators.transform(joinIterator, new Function<T, Callable<Void>>() {
                    @Override
                    public Callable<Void> apply(final T input) {
                        return new Callable<Void>() {
                            @Override
                            public Void call() throws Exception {
                                iterable.add(input);
                                return null;
                            }
                        };
                    }
                }), this.configuration.getAllowedThreads(),
                ExecutorsUtils.newThreadFactory(Optional.of(log), Optional.of("request-allocator-%d")));

        try {
            List<Either<Void, ExecutionException>> results = executor.executeAndGetResults();
            IteratorExecutor.logFailures(results, log, 10);
        } catch (InterruptedException ie) {
            log.error("Request allocation was interrupted.");
            return new AllocatedRequestsIteratorBase<>(
                    Iterators.<AllocatedRequestsIteratorBase.RequestWithResourceRequirement<T>>emptyIterator(),
                    resourcePool);
        }
    }

    iterable.logStatistics(Optional.of(this.log));

    //Get all requests rejected/dropped
    getRejectedAndDroppedRequests(iterable);

    return new AllocatedRequestsIteratorBase<>(iterable.iterator(), resourcePool);
}

From source file:de.cosmocode.palava.ipc.xml.rpc.adapters.MapAdapter.java

@Override
public Map<String, Object> decode(Value input) {
    Preconditions.checkNotNull(input, "Input");
    @SuppressWarnings("unchecked")
    final JAXBElement<Struct> element = JAXBElement.class.cast(input.getContent().get(0));
    final List<Member> members = element.getValue().getMember();
    return new AbstractMap<String, Object>() {

        @Override/*from w w  w .  j  ava2  s.co  m*/
        public Set<Entry<String, Object>> entrySet() {
            return new AbstractSet<Entry<String, Object>>() {

                @Override
                public Iterator<Entry<String, Object>> iterator() {
                    return Iterators
                            .unmodifiableIterator(Iterators.transform(members.iterator(), entryDecoder));
                }

                @Override
                public int size() {
                    return members.size();
                }

            };
        }

    };
}

From source file:com.davidbracewell.math.linear.VectorMap.java

@Override
public Set<Integer> keySet() {
    return new AbstractSet<Integer>() {

        @Override//from   w  w w.  j av  a2  s . co  m
        public Iterator<Integer> iterator() {
            return Iterators.transform(vector.nonZeroIterator(), new Function<DoubleEntry, Integer>() {
                @Nullable
                @Override
                public Integer apply(@Nullable DoubleEntry input) {
                    return input == null ? null : input.index;
                }
            });
        }

        @Override
        public int size() {
            return vector.size();
        }

    };
}

From source file:nz.ac.massey.cs.guery.impl.BreadthFirstPathFinder.java

private Iterator<Path<V, E>> findDirectLinks(final GraphAdapter<V, E> g, V start, int minLength,
        boolean outgoing, Predicate<E> filter, boolean computeAll) {
    Iterator<Path<V, E>> nullIter = null;
    if (minLength == 0) {
        Path<V, E> path = new LREmptyPath(start);
        nullIter = (Iterator<Path<V, E>>) Iterators.singletonIterator(path);
    }/*w  ww .ja v a 2 s.  com*/
    Iterator<E> edges = outgoing ? g.getOutEdges(start, filter) : g.getInEdges(start, filter);
    Iterator<Path<V, E>> paths = Iterators.transform(edges, new Function<E, Path<V, E>>() {
        @Override
        public Path<V, E> apply(E e) {
            return new SingletonPath<V, E>(e, g);
        }
    });

    return nullIter == null ? paths : Iterators.concat(nullIter, paths);

}

From source file:org.apache.accumulo.core.util.shell.commands.TablesCommand.java

@SuppressWarnings("unchecked")
@Override//from www  .  j a v  a 2  s  .  c  om
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState)
        throws AccumuloException, AccumuloSecurityException, IOException, NamespaceNotFoundException {

    final String namespace = cl.hasOption(OptUtil.namespaceOpt().getOpt())
            ? OptUtil.getNamespaceOpt(cl, shellState)
            : null;
    Map<String, String> tables = shellState.getConnector().tableOperations().tableIdMap();

    // filter only specified namespace
    tables = Maps.filterKeys(tables, new Predicate<String>() {
        @Override
        public boolean apply(String tableName) {
            return namespace == null || Tables.qualify(tableName).getFirst().equals(namespace);
        }
    });

    final boolean sortByTableId = cl.hasOption(sortByTableIdOption.getOpt());
    tables = new TreeMap<String, String>((sortByTableId ? MapUtils.invertMap(tables) : tables));

    Iterator<String> it = Iterators.transform(tables.entrySet().iterator(),
            new Function<Entry<String, String>, String>() {
                @Override
                public String apply(Map.Entry<String, String> entry) {
                    String tableName = String.valueOf(sortByTableId ? entry.getValue() : entry.getKey());
                    String tableId = String.valueOf(sortByTableId ? entry.getKey() : entry.getValue());
                    if (namespace != null)
                        tableName = Tables.qualify(tableName).getSecond();
                    if (cl.hasOption(tableIdOption.getOpt()))
                        return String.format(NAME_AND_ID_FORMAT, tableName, tableId);
                    else
                        return tableName;
                };
            });

    shellState.printLines(it, !cl.hasOption(disablePaginationOpt.getOpt()));
    return 0;
}

From source file:org.hashtrees.store.SimpleMemStore.java

public Iterator<Map.Entry<byte[], byte[]>> iterator() {
    return Iterators.transform(kvMap.entrySet().iterator(),
            new Function<Map.Entry<ByteBuffer, ByteBuffer>, Map.Entry<byte[], byte[]>>() {

                @Override//from   w w  w  .  ja va2 s  .c om
                public Entry<byte[], byte[]> apply(final Entry<ByteBuffer, ByteBuffer> input) {
                    return Maps.immutableEntry(input.getKey().array(), input.getValue().array());
                };
            });
}

From source file:de.cosmocode.collections.event.EventMap.java

@Override
public Set<K> keySet() {
    final Set<Entry<K, V>> entrySet = entrySet();
    // is ok, because the set returned by keySet() does not support add methods
    return new AbstractSet<K>() {

        @Override//from ww w.j av a 2s . c om
        public Iterator<K> iterator() {
            return Iterators.transform(entrySet.iterator(), getKey);
        }

        @Override
        public int size() {
            return entrySet.size();
        }

    };
}

From source file:org.fcrepo.kernel.impl.rdf.impl.VersionsRdfContext.java

private Iterator<Triple> versionTriples() throws RepositoryException {
    final Iterator<Version> allVersions = versionHistory.getAllVersions();
    return Iterators.concat(Iterators.transform(allVersions, version2triples));
}