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

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

Introduction

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

Prototype

public static <T> Iterator<T> concat(Iterator<? extends T> a, Iterator<? extends T> b) 

Source Link

Document

Combines two iterators into a single iterator.

Usage

From source file:org.locationtech.geogig.storage.internal.ObjectStoreDiffObjectIterator.java

private @Nullable DiffObjectInfo<T> computeNext() {
    if (nextBatch != null && nextBatch.hasNext()) {
        return nextBatch.next();
    }//w  ww  . j av  a  2 s  . c o m
    if (!nodes.hasNext()) {
        return null;
    }

    final int queryBatchSize = this.getAllBatchSize;

    List<DiffEntry> nextEntries = Iterators.partition(this.nodes, queryBatchSize).next();
    Set<ObjectId> leftEntriesIds = new HashSet<>();
    Set<ObjectId> rightEntriesIds = this.leftStore == this.rightStore ? leftEntriesIds : new HashSet<>();

    nextEntries.forEach((e) -> {
        ObjectId oldId = e.oldObjectId();
        ObjectId newId = e.newObjectId();
        if (!oldId.isNull()) {
            leftEntriesIds.add(oldId);
        }
        if (!newId.isNull()) {
            rightEntriesIds.add(newId);
        }
    });

    Iterator<T> objects = leftStore.getAll(leftEntriesIds, NOOP_LISTENER, this.type);
    if (rightEntriesIds != leftEntriesIds && !rightEntriesIds.isEmpty()) {
        objects = Iterators.concat(objects, rightStore.getAll(rightEntriesIds, NOOP_LISTENER, this.type));
    }
    Map<ObjectId, T> objectsById = new HashMap<>();
    objects.forEachRemaining((o) -> objectsById.putIfAbsent(o.getId(), o));
    nextBatch = createBatch(nextEntries, objectsById);
    return computeNext();
}

From source file:org.eclipse.sirius.ext.emf.AllContents.java

/**
 * {@inheritDoc}/*from ww  w  .  j  av  a  2  s .  c o  m*/
 */
public Iterator<EObject> iterator() {
    final Iterator<EObject> contentsIterator;
    if (root == null) {
        contentsIterator = Iterators.<EObject>emptyIterator();
    } else if (klass == null) {
        contentsIterator = root.eAllContents();
    } else {
        contentsIterator = Iterators.filter(root.eAllContents(), new Predicate<EObject>() {
            public boolean apply(EObject input) {
                return klass.isInstance(input);
            }
        });
    }
    if (includeRoot) {
        return Iterators.concat(Iterators.singletonIterator(root), contentsIterator);
    } else {
        return contentsIterator;
    }
}

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

/**
 * Executes the export command using the provided options.
 *///from  ww w.jav  a 2  s . com
@Override
protected void runInternal(GeogitCLI cli) throws IOException {
    if (args.size() < 1 || args.size() > 2) {
        printUsage();
        throw new CommandFailedException();
    }

    checkParameter(bbox == null || bbox.size() == 4, "The specified bounding box is not correct");

    geogit = cli.getGeogit();

    String osmfile = args.get(0);

    String ref = "WORK_HEAD";
    if (args.size() == 2) {
        ref = args.get(1);
        Optional<ObjectId> tree = geogit.command(ResolveTreeish.class).setTreeish(ref).call();
        checkParameter(tree.isPresent(), "Invalid commit or reference: %s", ref);
    }

    File file = new File(osmfile);
    checkParameter(!file.exists() || overwrite, "The selected file already exists. Use -o to overwrite");

    Iterator<EntityContainer> nodes = getFeatures(ref + ":node");
    Iterator<EntityContainer> ways = getFeatures(ref + ":way");
    Iterator<EntityContainer> iterator = Iterators.concat(nodes, ways);
    if (file.getName().endsWith(".pbf")) {
        BlockOutputStream output = new BlockOutputStream(new FileOutputStream(file));
        OsmosisSerializer serializer = new OsmosisSerializer(output);
        while (iterator.hasNext()) {
            EntityContainer entity = iterator.next();
            serializer.process(entity);
        }
        serializer.complete();
    } else {
        XmlWriter writer = new XmlWriter(file, CompressionMethod.None);
        while (iterator.hasNext()) {
            EntityContainer entity = iterator.next();
            writer.process(entity);
        }
        writer.complete();
    }

}

From source file:es.usc.citius.composit.wsc08.data.WSCLazyServiceProvider.java

@Override
public Iterable<Operation<Concept>> getOperationsWithInput(final Concept input) {
    final Set<Instance> instances = kb.getInstances(input);
    return new Iterable<Operation<Concept>>() {
        @Override/*w ww . j  av  a 2s.  c om*/
        public Iterator<Operation<Concept>> iterator() {
            Iterator<Operation<String>> iteratorResult = Iterators.emptyIterator();
            for (Instance instance : instances) {
                iteratorResult = Iterators.concat(iteratorResult,
                        delegatedProvider.getOperationsWithInput(instance.getID()).iterator());
            }
            return Iterators.transform(iteratorResult, new Function<Operation<String>, Operation<Concept>>() {
                @Override
                public Operation<Concept> apply(Operation<String> op) {
                    return translate(op);
                }
            });
        }
    };

}

From source file:net.stargraph.core.EntityIterator.java

private Iterator<Node> createIterator() {
    Model model = core.getGraphModel(kbId.getId());
    Graph g = model.getGraph();/*  w  w w.  jav  a2s .c om*/
    ExtendedIterator<Triple> exIt = g.find(Node.ANY, null, null);
    ExtendedIterator<Node> subjIt = exIt.mapWith(Triple::getSubject);
    exIt = g.find(null, null, Node.ANY);
    ExtendedIterator<Node> objIt = exIt.mapWith(Triple::getObject);
    return Iterators.concat(subjIt, objIt);
}

From source file:org.apache.cassandra.cql3.Operations.java

/**
 * {@inheritDoc}
 */
@Override
public Iterator<Operation> iterator() {
    return Iterators.concat(staticOperations.iterator(), regularOperations.iterator());
}

From source file:com.thinkbiganalytics.metadata.modeshape.sla.JcrServiceLevelAgreement.java

@Override
public List<ObligationGroup> getObligationGroups() {
    try {/*from  w w  w  .  j  ava  2  s  .  co m*/
        @SuppressWarnings("unchecked")
        Iterator<Node> defItr = (Iterator<Node>) this.node.getNodes(DEFAULT_GROUP);
        @SuppressWarnings("unchecked")
        Iterator<Node> grpItr = (Iterator<Node>) this.node.getNodes(GROUPS);

        return Lists.newArrayList(Iterators.concat(Iterators.transform(defItr, (groupNode) -> {
            return JcrUtil.createJcrObject(groupNode, JcrObligationGroup.class, JcrServiceLevelAgreement.this);
        }), Iterators.transform(grpItr, (groupNode) -> {
            return JcrUtil.createJcrObject(groupNode, JcrObligationGroup.class, JcrServiceLevelAgreement.this);
        })));
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to retrieve the obligation nodes", e);
    }

}

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

/**
 * Executes the export command using the provided options.
 * /*from ww  w .  j ava 2 s  . c o m*/
 * @param cli
 */
@Override
protected void runInternal(GeogitCLI cli) throws Exception {
    if (cli.getGeogit() == null) {
        cli.getConsole().println("Not a geogit repository: " + cli.getPlatform().pwd());
        return;
    }

    if (args.size() < 1 || args.size() > 2) {
        printUsage();
        return;
    }

    checkArgument(bbox == null || bbox.size() == 4, "The specified bounding box is not correct");

    geogit = cli.getGeogit();

    String osmfile = args.get(0);

    String ref = "WORK_HEAD";
    if (args.size() == 2) {
        ref = args.get(1);
        Optional<ObjectId> tree = geogit.command(ResolveTreeish.class).setTreeish(ref).call();
        Preconditions.checkArgument(tree.isPresent(), "Invalid commit or reference: %s", ref);
    }

    File file = new File(osmfile);
    checkArgument(!file.exists() || overwrite, "The selected file already exists. Use -o to overwrite");

    Iterator<EntityContainer> nodes = getFeatures(ref + ":node");
    Iterator<EntityContainer> ways = getFeatures(ref + ":way");
    Iterator<EntityContainer> iterator = Iterators.concat(nodes, ways);
    if (file.getName().endsWith(".pbf")) {
        BlockOutputStream output = new BlockOutputStream(new FileOutputStream(file));
        OsmosisSerializer serializer = new OsmosisSerializer(output);
        while (iterator.hasNext()) {
            EntityContainer entity = iterator.next();
            serializer.process(entity);
        }
        serializer.complete();
    } else {
        XmlWriter writer = new XmlWriter(file, CompressionMethod.None);
        while (iterator.hasNext()) {
            EntityContainer entity = iterator.next();
            writer.process(entity);
        }
        writer.complete();
    }

}

From source file:org.dspace.app.bulkedit.MetadataExport.java

private Iterator<Item> addItemsToResult(Iterator<Item> result, Iterator<Item> items) {
    if (result == null) {
        result = items;/*from   ww w  .ja v a  2  s  . c  o m*/
    } else {
        result = Iterators.concat(result, items);
    }

    return result;
}

From source file:org.eclipse.papyrus.infra.table.controlmode.helpers.TableMoveHelper.java

/**
 * Creates an iterable containing all the Papyrus Tables that are descending from the context.
 * /*from   w  ww  .j av  a 2 s.  com*/
 * @author olivier melois (Atos)
 */
public static Iterable<EObject> createDescendantTablesIterable(EObject context) {

    Set<EObject> result = Sets.newHashSet();

    TreeIterator<EObject> eAllContents = EcoreUtil.getAllProperContents(context, true); // was context.eAllContents().
    Iterator<EObject> contextAndDescendants = Iterators.concat(eAllContents,
            Iterators.singletonIterator(context));

    final Predicate<Setting> keepPapyrusTableInstances = new Predicate<Setting>() {

        public boolean apply(Setting setting) {
            boolean result = true;
            if (setting != null) {
                EObject settingEObject = setting.getEObject();
                result &= settingEObject instanceof PapyrusTableInstance;
                result &= PapyrustableinstancePackage.Literals.PAPYRUS_TABLE_INSTANCE__TABLE == setting
                        .getEStructuralFeature();
            } else {
                result = false;
            }
            return result;
        }
    };

    /*
     * Predicate used to keep the usages which are PapyrusTableInstances
     */
    Predicate<Setting> keepTableInstances = new Predicate<Setting>() {

        public boolean apply(Setting setting) {
            boolean result = true;
            if (setting != null) {
                EObject settingEObject = setting.getEObject();
                result &= settingEObject instanceof TableInstance;
                result &= setting
                        .getEStructuralFeature() == TableinstancePackage.Literals.TABLE_INSTANCE__CONTEXT;

                Collection<Setting> references = PapyrusEcoreUtils.getUsages(settingEObject);
                Iterable<Setting> papyrusTableInstances = Iterables.filter(references,
                        keepPapyrusTableInstances);
                //Veryfing that there is at least one papyrusTableInstance
                result = result && !Iterables.isEmpty(papyrusTableInstances);

            } else {
                result = false;
            }
            return result;
        }
    };

    /*
     * Function to get the eObject from a setting
     */
    Function<Setting, EObject> getEObject = new Function<Setting, EObject>() {

        public EObject apply(Setting input) {
            EObject settingEObject = input.getEObject();
            Collection<Setting> references = PapyrusEcoreUtils.getUsages(settingEObject);
            Iterable<Setting> papyrusTableInstances = Iterables.filter(references, keepPapyrusTableInstances);
            //Getting the eobject of thie first element of this iterable.
            return Iterables.get(papyrusTableInstances, 0).getEObject();
        }

    };

    /*
     * For the context and his descendants :
     */
    while (contextAndDescendants.hasNext()) {
        EObject current = contextAndDescendants.next();
        //Usages
        Iterable<Setting> usages = PapyrusEcoreUtils.getUsages(current);
        //Filtering to keep only papyrus table instances.
        Iterable<Setting> tableInstanceSettings = Iterables.filter(usages, keepTableInstances);
        //Getting the eObjects 
        Iterable<EObject> papyrusTableInstances = Iterables.transform(tableInstanceSettings, getEObject);
        //Adding all the kept usages.
        Iterables.addAll(result, papyrusTableInstances);
    }

    return result;
}