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:com.jwsphere.accumulo.shell.ScanCompressionCommand.java

protected void printRecords(final CommandLine cl, Shell shellState, final Scanner scanner, PrintFile outFile)
        throws IOException {

    Iterator<List<KeyValue>> batched = Scans.toBatchIterator(scanner.iterator(), scanner.getBatchSize());

    final TransportCompression defaultCompression = new DefaultTransportCompression();
    final TransportCompression deflateCompression = new DeflateTransportCompression();
    final TransportCompression relativeKeyCompression = new RelativeKeyTransportCompression();
    final TransportCompression deflatedRelativeKeyCompression = new DeflatedRelativeKeyTransportCompression();

    Iterator<String> stats = Iterators.transform(batched, new Function<List<KeyValue>, String>() {
        public String apply(List<KeyValue> batch) {
            StringBuilder sb = new StringBuilder();
            sb.append("============ ScanResult compression stats =============").append('\n');
            sb.append(defaultCompression.evaluate(batch).toString()).append('\n');
            sb.append(deflateCompression.evaluate(batch).toString()).append('\n');
            sb.append(relativeKeyCompression.evaluate(batch).toString()).append('\n');
            sb.append(deflatedRelativeKeyCompression.evaluate(batch));
            sb.append("=======================================================").append("\n\n");
            return sb.toString();
        }//  w  ww .  j a  va  2s  . c om
    });

    shellState.printLines(stats, true);
}

From source file:org.apache.beam.runners.spark.translation.TranslationUtils.java

/** {@link KV} to pair flatmap function. */
public static <K, V> PairFlatMapFunction<Iterator<KV<K, V>>, K, V> toPairFlatMapFunction() {
    return new PairFlatMapFunction<Iterator<KV<K, V>>, K, V>() {
        @Override/*from  www  .j  a va  2s. com*/
        public Iterable<Tuple2<K, V>> call(final Iterator<KV<K, V>> itr) {
            final Iterator<Tuple2<K, V>> outputItr = Iterators.transform(itr,
                    new com.google.common.base.Function<KV<K, V>, Tuple2<K, V>>() {

                        @Override
                        public Tuple2<K, V> apply(KV<K, V> kv) {
                            return new Tuple2<>(kv.getKey(), kv.getValue());
                        }
                    });
            return new Iterable<Tuple2<K, V>>() {

                @Override
                public Iterator<Tuple2<K, V>> iterator() {
                    return outputItr;
                }
            };
        }
    };
}

From source file:edu.harvard.med.screensaver.ui.arch.util.AttachedFiles.java

public DataModel getAttachedFilesDataModel() {
    if (_entity == null) {
        return new ListDataModel();
    }//from  ww w .  j  a  va 2  s.c  o  m
    List<AttachedFile> attachedFilesUnfiltered = Lists
            .newArrayList(Iterables.filter(_entity.getAttachedFiles(), _attachedFileFilter));
    List<AttachedFile> attachedFiles = Lists.newArrayList(
            Iterators.transform(attachedFilesUnfiltered.iterator(), new Function<AttachedFile, AttachedFile>() {

                @Override
                public AttachedFile apply(AttachedFile from) {
                    return (AttachedFile) from.restrict();
                }
            }));
    Collections.sort(attachedFiles);
    return new ListDataModel(attachedFiles);
}

From source file:qdg.view.MixedGraphAsDiGraph.java

@Override
public Iterator<Edge> getOutArcIterator(Node node) {
    Iterator<Edge> arcs = Iterators.transform(g.getOutArcIterator(node), forwardEdge);
    Iterator<Edge> uEdges = new OutArcIterator(node);
    return new ConcatIterator<Edge>(arcs, uEdges);
}

From source file:org.apache.rya.export.accumulo.AccumuloRyaStatementStore.java

@Override
public Iterator<RyaStatement> fetchStatements() throws FetchStatementException {
    try {/*from   w w w  . ja v a2 s .  c o  m*/
        final RyaTripleContext ryaTripleContext = RyaTripleContext.getInstance(accumuloRyaDao.getConf());

        Scanner scanner = null;
        try {
            scanner = AccumuloRyaUtils.getScanner(tablePrefix + RdfCloudTripleStoreConstants.TBL_SPO_SUFFIX,
                    accumuloRyaDao.getConf());
            for (final IteratorSetting iteratorSetting : iteratorSettings) {
                scanner.addScanIterator(iteratorSetting);
            }
        } catch (final IOException e) {
            throw new FetchStatementException("Unable to get scanner to fetch Rya Statements", e);
        }
        // Convert Entry iterator to RyaStatement iterator
        final Iterator<Entry<Key, Value>> entryIter = scanner.iterator();
        final Iterator<RyaStatement> ryaStatementIter = Iterators.transform(entryIter,
                new Function<Entry<Key, Value>, RyaStatement>() {
                    @Override
                    public RyaStatement apply(final Entry<Key, Value> entry) {
                        final Key key = entry.getKey();
                        final Value value = entry.getValue();
                        RyaStatement ryaStatement = null;
                        try {
                            ryaStatement = AccumuloRyaUtils.createRyaStatement(key, value, ryaTripleContext);
                        } catch (final TripleRowResolverException e) {
                            log.error("Unable to convert the key/value pair into a Rya Statement", e);
                        }
                        return ryaStatement;
                    }
                });
        return ryaStatementIter;
    } catch (final Exception e) {
        throw new FetchStatementException("Failed to fetch statements.", e);
    }
}

From source file:org.apache.mahout.math.hadoop.DistributedRowMatrix.java

@Override
public Iterator<MatrixSlice> iterateAll() {
    try {/*from  w ww.java2s  .  c  om*/
        Path pathPattern = rowPath;
        if (FileSystem.get(conf).getFileStatus(rowPath).isDir()) {
            pathPattern = new Path(rowPath, "*");
        }
        return Iterators.transform(
                new SequenceFileDirIterator<IntWritable, VectorWritable>(pathPattern, PathType.GLOB,
                        PathFilters.logsCRCFilter(), null, true, conf),
                new Function<Pair<IntWritable, VectorWritable>, MatrixSlice>() {
                    @Override
                    public MatrixSlice apply(Pair<IntWritable, VectorWritable> from) {
                        return new MatrixSlice(from.getSecond().get(), from.getFirst().get());
                    }
                });
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
}

From source file:com.google.gcloud.datastore.DatastoreServiceImpl.java

@Override
public List<Key> allocateId(IncompleteKey... keys) {
    if (keys.length == 0) {
        return Collections.emptyList();
    }//www. j  a v a 2s  .  c  o  m
    DatastoreV1.AllocateIdsRequest.Builder requestPb = DatastoreV1.AllocateIdsRequest.newBuilder();
    for (IncompleteKey key : keys) {
        requestPb.addKey(trimNameOrId(key).toPb());
    }
    DatastoreV1.AllocateIdsResponse responsePb = allocateIds(requestPb.build());
    Iterator<DatastoreV1.Key> keyIterator = responsePb.getKeyList().iterator();
    ImmutableList.Builder<Key> builder = ImmutableList.<Key>builder()
            .addAll(Iterators.transform(keyIterator, new Function<DatastoreV1.Key, Key>() {
                @Override
                public Key apply(DatastoreV1.Key keyPb) {
                    return Key.fromPb(keyPb);
                }
            }));
    return builder.build();
}

From source file:org.geogit.api.plumbing.DeepMove.java

private void moveObjects(final ObjectDatabase from, final ObjectDatabase to,
        Supplier<Iterator<Node>> nodesToMove, final Set<ObjectId> metadataIds) {

    Function<Node, ObjectId> asIds = new Function<Node, ObjectId>() {
        @Override/*from www .  j av a  2  s  .co m*/
        public ObjectId apply(Node input) {
            return input.getObjectId();
        }
    };
    Function<Node, RevObject> asObjects = new Function<Node, RevObject>() {
        @Override
        public RevObject apply(Node input) {
            if (input.getMetadataId().isPresent()) {
                metadataIds.add(input.getMetadataId().get());
            }
            return from.get(input.getObjectId());
        }
    };

    Iterator<Node> iterator;
    iterator = nodesToMove.get();
    if (iterator.hasNext()) {
        to.putAll(Iterators.transform(iterator, asObjects));

        iterator = nodesToMove.get();
        from.deleteAll(Iterators.transform(iterator, asIds));
    }
}

From source file:org.kitesdk.data.spi.filesystem.FileSystemPartitionIterator.java

@SuppressWarnings("deprecation")
FileSystemPartitionIterator(FileSystem fs, Path root, PartitionStrategy strategy, final Constraints constraints)
        throws IOException {
    Preconditions.checkArgument(fs.isDirectory(root));
    this.fs = fs;
    this.rootDirectory = root;
    this.iterator = Iterators.filter(Iterators
            .transform(new FileSystemIterator(strategy.getFieldPartitioners().size()), new MakeKey(strategy)),
            new KeyPredicate(constraints.toKeyPredicate()));
}

From source file:org.apache.gobblin.runtime.local.LocalJobLauncher.java

@Override
protected void runWorkUnitStream(WorkUnitStream workUnitStream) throws Exception {
    String jobId = this.jobContext.getJobId();
    final JobState jobState = this.jobContext.getJobState();

    Iterator<WorkUnit> workUnitIterator = workUnitStream.getWorkUnits();
    if (!workUnitIterator.hasNext()) {
        LOG.warn("No work units to run");
        return;/*from w  w  w . j  av a  2  s. c  o m*/
    }

    TimingEvent workUnitsRunTimer = this.eventSubmitter
            .getTimingEvent(TimingEvent.RunJobTimings.WORK_UNITS_RUN);
    Iterator<WorkUnit> flattenedWorkUnits = new MultiWorkUnitUnpackingIterator(workUnitStream.getWorkUnits());
    Iterator<WorkUnit> workUnitsWithJobState = Iterators.transform(flattenedWorkUnits,
            new Function<WorkUnit, WorkUnit>() {
                @Override
                public WorkUnit apply(WorkUnit workUnit) {
                    workUnit.addAllIfNotExist(jobState);
                    return workUnit;
                }
            });

    GobblinMultiTaskAttempt.runWorkUnits(this.jobContext, workUnitsWithJobState, this.taskStateTracker,
            this.taskExecutor, GobblinMultiTaskAttempt.CommitPolicy.IMMEDIATE);

    if (this.cancellationRequested) {
        // Wait for the cancellation execution if it has been requested
        synchronized (this.cancellationExecution) {
            if (this.cancellationExecuted) {
                return;
            }
        }
    }
    workUnitsRunTimer.stop();

    LOG.info(String.format("All tasks of job %s have completed", jobId));

    if (jobState.getState() == JobState.RunningState.RUNNING) {
        jobState.setState(JobState.RunningState.SUCCESSFUL);
    }
}