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.kiji.schema.impl.cassandra.CassandraKijiResultScanner.java

/**
 * Get an iterator of the entity IDs in a list of Cassandra Kiji tables that correspond to a
 * subset of cassandra tables in a Kiji table.
 *
 * @param tables The Cassandra tables to get Entity IDs from.
 * @param options The scan options. May specify start and stop tokens.
 * @param table The Kiji Cassandra table which the Cassandra tables belong to.
 * @param layout The layout of the Kiji Cassandra table.
 * @return An iterator of Entity IDs./* ww w  . j a  v a  2  s  .c o m*/
 */
public static Iterator<EntityId> getEntityIDs(final List<CassandraTableName> tables,
        final CassandraKijiScannerOptions options, final CassandraKijiTable table,
        final KijiTableLayout layout) {

    final List<ResultSetFuture> localityGroupFutures = FluentIterable.from(tables)
            .transform(new Function<CassandraTableName, Statement>() {
                /** {@inheritDoc} */
                @Override
                public Statement apply(final CassandraTableName tableName) {
                    return CQLUtils.getEntityIDScanStatement(layout, tableName, options);
                }
            }).transform(new Function<Statement, ResultSetFuture>() {
                /** {@inheritDoc} */
                @Override
                public ResultSetFuture apply(final Statement statement) {
                    return table.getAdmin().executeAsync(statement);
                }
            })
            // Force futures to execute by sending results to a list
            .toList();

    // We can use the DISTINCT optimization iff the entity ID contains only hashed components
    RowKeyFormat2 keyFormat = (RowKeyFormat2) layout.getDesc().getKeysFormat();
    final boolean deduplicateComponents = keyFormat.getRangeScanStartIndex() != keyFormat.getComponents()
            .size();

    if (deduplicateComponents) {
        LOG.warn(
                "Scanning a Cassandra Kiji table with non-hashed entity ID components is"
                        + " inefficient.  Consider hashing all entity ID components. Table: {}.",
                table.getURI());
    }

    final List<Iterator<TokenRowKeyComponents>> tokenRowKeyStreams = FluentIterable.from(localityGroupFutures)
            .transform(new Function<ResultSetFuture, Iterator<Row>>() {
                /** {@inheritDoc} */
                @Override
                public Iterator<Row> apply(final ResultSetFuture future) {
                    return CassandraKijiResult.unwrapFuture(future).iterator();
                }
            }).transform(new Function<Iterator<Row>, Iterator<TokenRowKeyComponents>>() {
                /** {@inheritDoc} */
                @Override
                public Iterator<TokenRowKeyComponents> apply(final Iterator<Row> rows) {
                    return Iterators.transform(rows, RowDecoders.getRowKeyDecoderFunction(layout));
                }
            }).transform(new Function<Iterator<TokenRowKeyComponents>, Iterator<TokenRowKeyComponents>>() {
                /** {@inheritDoc} */
                @Override
                public Iterator<TokenRowKeyComponents> apply(final Iterator<TokenRowKeyComponents> components) {
                    if (deduplicateComponents) {
                        return IteratorUtils.deduplicatingIterator(components);
                    } else {
                        return components;
                    }
                }
            }).toList();

    return Iterators.transform(
            IteratorUtils.deduplicatingIterator(
                    Iterators.mergeSorted(tokenRowKeyStreams, TokenRowKeyComponentsComparator.getInstance())),
            RowDecoders.getEntityIdFunction(table));
}

From source file:org.hashtrees.manager.HashTreesManager.java

private void rebuildAllLocalTrees() {
    Iterator<Callable<Void>> rebuildTasks = Iterators.transform(treeIdProvider.getAllPrimaryTreeIds(),
            new Function<Long, Callable<Void>>() {

                @Override/* www  .  jav  a 2 s  .  c o  m*/
                public Callable<Void> apply(final Long treeId) {
                    return new Callable<Void>() {

                        @Override
                        public Void call() throws IOException {
                            sendRebuildRequestToRemoteTrees(treeId);
                            rebuildHashTree(treeId, fullRebuildPeriod);
                            return null;
                        }
                    };
                }
            });

    LOG.info("Building locally managed trees.");
    TaskQueue<Void> taskQueue = new TaskQueue<Void>(threadPool, rebuildTasks, noOfThreads);
    while (taskQueue.hasNext()) {
        try {
            taskQueue.next().get();
        } catch (ExecutionException | InterruptedException e) {
            LOG.info("Failure occurred in build task.", e);
        }
        if (hasStopRequested()) {
            taskQueue.stopAsync();
        }
    }
    LOG.info("No of successful/failed rebuild tasks : " + taskQueue.getPasseTasksCount() + "/"
            + taskQueue.getFailedTasksCount());
    LOG.info("Building locally managed trees - Done");
}

From source file:kn.uni.gis.dataimport.util.GeoUtil.java

public static CPolygon polygonAt(ResultSet object, int index) throws SQLException {
    PGgeometry geo = (PGgeometry) object.getObject(index);
    CPolygon cPolygon = new CPolygon(ImmutableList
            .copyOf((Iterators.transform(pointIterator(geo.getGeometry()), new Function<Point, CPoint>() {
                @Override/*  www . java2s  . com*/
                public CPoint apply(Point input) {
                    return new CPoint(input.getX(), input.getY());
                }
            }))));
    return cPolygon;
}

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

public Iterator iterator() {
    return Iterators.transform(simpleIterator(), new Function() {

        public Object apply(Object e) {
            return graph.frameElement((Element) e, FramedVertex.class);
        }//from  w ww .  jav a2 s  . co  m
    });
}

From source file:com.google.cloud.genomics.localrepo.vcf.VCFReader.java

/**
 * Read the VCF data and pass the data to the provided {@code Callback}.
 *//*w  ww  . ja v a  2s .  c  om*/
public <X> X read(Callback<X> callback) throws Exception {
    final PeekingIterator<String> iterator = Iterators.peekingIterator(new AbstractIterator<String>() {
        @Override
        protected String computeNext() {
            try {
                String line = in.readLine();
                return null == line ? endOfData() : line;
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
        }
    });
    return callback.readVcf(parseMetaInfo(iterator), parseHeader(iterator), new FluentIterable<VCFRecord>() {
        @Override
        public Iterator<VCFRecord> iterator() {
            return Iterators.transform(Iterators.filter(iterator, IS_NOT_EMPTY), PARSE_RECORD);
        }
    });
}

From source file:org.locationtech.geogig.porcelain.RemoveOp.java

private void stageDeletes(Iterator<NodeRef> trees, Iterator<String> features) {
    final StagingArea index = stagingArea();

    Iterator<DiffEntry> treeDeletes = Iterators.transform(trees, (treeRef) -> new DiffEntry(treeRef, null));

    Iterator<DiffEntry> featureDeletes = Iterators.transform(features, (featurePath) -> {
        Node node = Node.create(NodeRef.nodeFromPath(featurePath), ObjectId.NULL, ObjectId.NULL, TYPE.FEATURE,
                null);/*from   www.ja  v  a2  s.  c o m*/
        String parentPath = NodeRef.parentPath(featurePath);
        NodeRef oldFeature = new NodeRef(node, parentPath, ObjectId.NULL);
        return new DiffEntry(oldFeature, null);
    });

    ProgressListener progress = DefaultProgressListener.NULL;
    index.stage(progress, Iterators.concat(treeDeletes, featureDeletes), -1);
}

From source file:org.geogig.osm.cli.commands.OSMExport.java

private Iterator<EntityContainer> getFeatures(String ref) {
    Optional<ObjectId> id = geogig.command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Collections.emptyIterator();
    }//  w ww . j a  va2  s .c  om
    LsTreeOp op = geogig.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES)
            .setReference(ref);
    if (bbox != null) {
        final Envelope env;
        try {
            env = new Envelope(Double.parseDouble(bbox.get(0)), Double.parseDouble(bbox.get(2)),
                    Double.parseDouble(bbox.get(1)), Double.parseDouble(bbox.get(3)));
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Wrong bbox definition");
        }
        Predicate<Bounded> filter = new Predicate<Bounded>() {
            @Override
            public boolean apply(final Bounded bounded) {
                boolean intersects = bounded.intersects(env);
                return intersects;
            }
        };
        op.setBoundsFilter(filter);
    }
    Iterator<NodeRef> iterator = op.call();
    final EntityConverter converter = new EntityConverter();
    final Function<NodeRef, EntityContainer> function = (nr) -> {
        RevFeature revFeature = geogig.command(RevObjectParse.class).setObjectId(nr.getObjectId())
                .call(RevFeature.class).get();
        SimpleFeatureType featureType;
        if (nr.path().startsWith(OSMUtils.NODE_TYPE_NAME)) {
            featureType = OSMUtils.nodeType();
        } else {
            featureType = OSMUtils.wayType();
        }
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
        RevFeatureType revFeatureType = RevFeatureTypeBuilder.build(featureType);
        List<PropertyDescriptor> descriptors = revFeatureType.descriptors();
        for (int i = 0; i < descriptors.size(); i++) {
            PropertyDescriptor descriptor = descriptors.get(i);
            Optional<Object> value = revFeature.get(i);
            featureBuilder.set(descriptor.getName(), value.orNull());
        }
        SimpleFeature feature = featureBuilder.buildFeature(nr.name());
        Entity entity = converter.toEntity(feature, null);
        EntityContainer container;
        if (entity instanceof Node) {
            container = new NodeContainer((Node) entity);
        } else {
            container = new WayContainer((Way) entity);
        }

        return container;
    };
    return Iterators.transform(iterator, function);
}

From source file:qdg.StaticMixedIdGraph.java

@Override
public Iterator<Edge> getIncidentArcIterator(Node node) {
    return Iterators.transform(new ConcatIterator<Integer>(arcLace.getOutArcIterator(((N) node).getId()),
            arcLace.getInArcIterator(((N) node).getId())), constructArc);
}

From source file:org.geogit.osm.out.cli.OSMExport.java

private Iterator<EntityContainer> getFeatures(String ref) {
    Optional<ObjectId> id = geogit.command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Iterators.emptyIterator();
    }//from   w  w w  .j  ava  2s .  c om
    LsTreeOp op = geogit.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES)
            .setReference(ref);
    if (bbox != null) {
        final Envelope env;
        try {
            env = new Envelope(Double.parseDouble(bbox.get(0)), Double.parseDouble(bbox.get(2)),
                    Double.parseDouble(bbox.get(1)), Double.parseDouble(bbox.get(3)));
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Wrong bbox definition");
        }
        Predicate<Bounded> filter = new Predicate<Bounded>() {
            @Override
            public boolean apply(final Bounded bounded) {
                boolean intersects = bounded.intersects(env);
                return intersects;
            }
        };
        op.setBoundsFilter(filter);
    }
    Iterator<NodeRef> iterator = op.call();
    final EntityConverter converter = new EntityConverter();
    Function<NodeRef, EntityContainer> function = new Function<NodeRef, EntityContainer>() {

        @Override
        @Nullable
        public EntityContainer apply(@Nullable NodeRef ref) {
            RevFeature revFeature = geogit.command(RevObjectParse.class).setObjectId(ref.objectId())
                    .call(RevFeature.class).get();
            SimpleFeatureType featureType;
            if (ref.path().startsWith(OSMUtils.NODE_TYPE_NAME)) {
                featureType = OSMUtils.nodeType();
            } else {
                featureType = OSMUtils.wayType();
            }
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
            RevFeatureType revFeatureType = RevFeatureType.build(featureType);
            List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
            ImmutableList<Optional<Object>> values = revFeature.getValues();
            for (int i = 0; i < descriptors.size(); i++) {
                PropertyDescriptor descriptor = descriptors.get(i);
                Optional<Object> value = values.get(i);
                featureBuilder.set(descriptor.getName(), value.orNull());
            }
            SimpleFeature feature = featureBuilder.buildFeature(ref.name());
            Entity entity = converter.toEntity(feature);
            EntityContainer container;
            if (entity instanceof Node) {
                container = new NodeContainer((Node) entity);
            } else {
                container = new WayContainer((Way) entity);
            }

            return container;

        }

    };
    return Iterators.transform(iterator, function);
}