Search in sources :

Example 91 with Function

use of com.google.common.base.Function in project google-cloud-java by GoogleCloudPlatform.

the class HttpBigQueryRpc method listTables.

@Override
public Tuple<String, Iterable<Table>> listTables(String projectId, String datasetId, Map<Option, ?> options) {
    try {
        TableList tableList = bigquery.tables().list(projectId, datasetId).setMaxResults(Option.MAX_RESULTS.getLong(options)).setPageToken(Option.PAGE_TOKEN.getString(options)).execute();
        Iterable<TableList.Tables> tables = tableList.getTables();
        return Tuple.of(tableList.getNextPageToken(), Iterables.transform(tables != null ? tables : ImmutableList.<TableList.Tables>of(), new Function<TableList.Tables, Table>() {

            @Override
            public Table apply(TableList.Tables tablePb) {
                return new Table().setFriendlyName(tablePb.getFriendlyName()).setId(tablePb.getId()).setKind(tablePb.getKind()).setTableReference(tablePb.getTableReference()).setType(tablePb.getType());
            }
        }));
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : Function(com.google.common.base.Function) Table(com.google.api.services.bigquery.model.Table) TableList(com.google.api.services.bigquery.model.TableList) IOException(java.io.IOException)

Example 92 with Function

use of com.google.common.base.Function in project GeoGig by boundlessgeo.

the class DeepMove method moveObjects.

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

        final Function<Node, ObjectId> asId = new Function<Node, ObjectId>() {

            @Override
            public ObjectId apply(Node input) {
                Optional<ObjectId> metadataId = input.getMetadataId();
                if (metadataId.isPresent()) {
                    metadataIds.add(input.getMetadataId().get());
                }
                ObjectId id = input.getObjectId();
                return id;
            }
        };

        @Override
        public Iterator<ObjectId> iterator() {
            Iterator<Node> iterator = nodesToMove.get();
            Iterator<ObjectId> ids = Iterators.transform(iterator, asId);
            return ids;
        }
    };
    final ExecutorService deletingService = Executors.newSingleThreadExecutor();
    try {
        final DeletingListener deletingListener = new DeletingListener(deletingService, from);
        // store objects into the target db and remove them from the origin db in one shot
        to.putAll(from.getAll(ids), deletingListener);
        // in case there are some deletes pending cause the iterator finished and the listener
        // didn't fill its buffer
        deletingListener.deleteInserted();
    } finally {
        deletingService.shutdown();
        while (!deletingService.isTerminated()) {
            try {
                deletingService.awaitTermination(100, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
            // ok, still awaiting for delete tasks to finish
            }
        }
    }
}
Also used : Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) Node(org.locationtech.geogig.api.Node) Function(com.google.common.base.Function) ExecutorService(java.util.concurrent.ExecutorService)

Example 93 with Function

use of com.google.common.base.Function in project GeoGig by boundlessgeo.

the class OSMMapOp method getFeatures.

private Iterator<Feature> getFeatures(String ref) {
    Optional<ObjectId> id = command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Iterators.emptyIterator();
    }
    LsTreeOp op = command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
    Iterator<NodeRef> iterator = op.call();
    Function<NodeRef, Feature> nodeRefToFeature = new Function<NodeRef, Feature>() {

        private final //
        Map<String, FeatureBuilder> builders = //
        ImmutableMap.<//
        String, //
        FeatureBuilder>of(//
        OSMUtils.NODE_TYPE_NAME, //
        new FeatureBuilder(RevFeatureTypeImpl.build(OSMUtils.nodeType())), //
        OSMUtils.WAY_TYPE_NAME, new FeatureBuilder(RevFeatureTypeImpl.build(OSMUtils.wayType())));

        private final RevObjectParse parseCommand = command(RevObjectParse.class);

        @Override
        @Nullable
        public Feature apply(@Nullable NodeRef ref) {
            RevFeature revFeature = parseCommand.setObjectId(ref.objectId()).call(RevFeature.class).get();
            final String parentPath = ref.getParentPath();
            FeatureBuilder featureBuilder = builders.get(parentPath);
            String fid = ref.name();
            Feature feature = featureBuilder.build(fid, revFeature);
            return feature;
        }
    };
    return Iterators.transform(iterator, nodeRefToFeature);
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) RevFeature(org.locationtech.geogig.api.RevFeature) Feature(org.opengis.feature.Feature) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Example 94 with Function

use of com.google.common.base.Function in project GeoGig by boundlessgeo.

the class MongoGraphDatabase method getChildren.

@Override
public ImmutableList<ObjectId> getChildren(ObjectId id) {
    DBObject query = new BasicDBObject();
    query.put("_label", Relationship.PARENT.name());
    query.put("_out", id.toString());
    DBCursor cursor = collection.find(query);
    Function<DBObject, ObjectId> idMapper = new Function<DBObject, ObjectId>() {

        @Override
        public ObjectId apply(DBObject o) {
            return ObjectId.valueOf((String) o.get("_in"));
        }
    };
    return ImmutableList.copyOf(Iterators.transform(cursor.iterator(), idMapper));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Function(com.google.common.base.Function) DBCursor(com.mongodb.DBCursor) ObjectId(org.locationtech.geogig.api.ObjectId) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 95 with Function

use of com.google.common.base.Function in project GeoGig by boundlessgeo.

the class OSMExport method getFeatures.

private Iterator<EntityContainer> getFeatures(String ref) {
    Optional<ObjectId> id = geogig.command(RevParse.class).setRefSpec(ref).call();
    if (!id.isPresent()) {
        return Iterators.emptyIterator();
    }
    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();
    Function<NodeRef, EntityContainer> function = new Function<NodeRef, EntityContainer>() {

        @Override
        @Nullable
        public EntityContainer apply(@Nullable NodeRef ref) {
            RevFeature revFeature = geogig.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 = RevFeatureTypeImpl.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, 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);
}
Also used : EntityConverter(org.locationtech.geogig.osm.internal.EntityConverter) Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) WayContainer(org.openstreetmap.osmosis.core.container.v0_6.WayContainer) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) NodeContainer(org.openstreetmap.osmosis.core.container.v0_6.NodeContainer) Envelope(com.vividsolutions.jts.geom.Envelope) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) Predicate(com.google.common.base.Predicate) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) Bounded(org.locationtech.geogig.api.Bounded) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

Function (com.google.common.base.Function)616 List (java.util.List)138 ArrayList (java.util.ArrayList)114 Nullable (javax.annotation.Nullable)103 Map (java.util.Map)97 IOException (java.io.IOException)89 HashMap (java.util.HashMap)78 Test (org.junit.Test)73 ImmutableList (com.google.common.collect.ImmutableList)49 File (java.io.File)46 Set (java.util.Set)46 Collection (java.util.Collection)35 ImmutableMap (com.google.common.collect.ImmutableMap)34 DateTime (org.joda.time.DateTime)33 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)29 HashSet (java.util.HashSet)27 Iterator (java.util.Iterator)27 LinkedList (java.util.LinkedList)26 ExecutionException (java.util.concurrent.ExecutionException)24 Collectors (java.util.stream.Collectors)15