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

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

Introduction

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

Prototype

public static int advance(Iterator<?> iterator, int numberToAdvance) 

Source Link

Document

Calls next() on iterator , either numberToAdvance times or until hasNext() returns false , whichever comes first.

Usage

From source file:org.apache.marmotta.commons.sesame.repository.ResourceUtils.java

/**
 * List resources with the given prefix/*w  w  w .j  a v a 2 s .co m*/
 *
 * @param prefix the prefix
 * @param offset
 * @param limit
 */
public static Iterable<URI> listResourcesByPrefix(final RepositoryConnection con, final String prefix,
        final int offset, final int limit) {
    final ResourceConnection rcon = getWrappedResourceConnection(con);

    if (rcon != null) {
        return new Iterable<URI>() {
            @Override
            public Iterator<URI> iterator() {
                try {
                    Iterator<URI> result = ResultUtils.unwrap(rcon.getResources(prefix));

                    Iterators.advance(result, offset);

                    if (limit > 0) {
                        return Iterators.limit(result, limit);
                    } else {
                        return result;
                    }
                } catch (RepositoryException e) {
                    ExceptionUtils.handleRepositoryException(e, ResourceUtils.class);
                    return Iterators.emptyIterator();
                }

            }
        };
    } else {
        // no direct prefix listing support, need to filter the listResources result
        return new Iterable<URI>() {
            @Override
            public Iterator<URI> iterator() {
                Iterator<URI> result = Iterators
                        .transform(Iterators.filter(listResources(con).iterator(), new Predicate<Resource>() {
                            @Override
                            public boolean apply(Resource input) {
                                return input instanceof URI && input.stringValue().startsWith(prefix);
                            }
                        }), new Function<Resource, URI>() {
                            @Override
                            public URI apply(Resource input) {
                                return (URI) input;
                            }
                        });

                Iterators.advance(result, offset);

                if (limit > 0) {
                    return Iterators.limit(result, limit);
                } else {
                    return result;
                }
            }
        };
    }
}

From source file:org.locationtech.geogig.geotools.data.reader.FeatureReaderBuilder.java

private <T> AutoCloseableIterator<T> applyOffsetAndLimit(AutoCloseableIterator<T> iterator) {
    Integer offset = this.offset;
    Integer limit = this.limit;
    if (offset != null) {
        Iterators.advance(iterator, offset.intValue());
    }/*from  w  w w. j ava  2s .c  o m*/
    if (limit != null) {
        iterator = AutoCloseableIterator.limit(iterator, limit.intValue());
    }
    return iterator;
}

From source file:org.locationtech.geogig.web.api.ResponseWriter.java

/**
 * Writes the response for a set of diffs while also supplying the geometry.
 * //ww  w. java  2  s  .co  m
 * @param geogig - a CommandLocator to call commands from
 * @param diff - a DiffEntry iterator to build the response from
 * @throws XMLStreamException
 */
public void writeGeometryChanges(final Context geogig, Iterator<DiffEntry> diff, int page, int elementsPerPage)
        throws XMLStreamException {

    Iterators.advance(diff, page * elementsPerPage);
    int counter = 0;

    Iterator<GeometryChange> changeIterator = Iterators.transform(diff,
            new Function<DiffEntry, GeometryChange>() {
                @Override
                public GeometryChange apply(DiffEntry input) {
                    Optional<RevObject> feature = Optional.absent();
                    Optional<RevObject> type = Optional.absent();
                    String path = null;
                    String crsCode = null;
                    GeometryChange change = null;
                    if (input.changeType() == ChangeType.ADDED || input.changeType() == ChangeType.MODIFIED) {
                        feature = geogig.command(RevObjectParse.class).setObjectId(input.newObjectId()).call();
                        type = geogig.command(RevObjectParse.class)
                                .setObjectId(input.getNewObject().getMetadataId()).call();
                        path = input.getNewObject().path();

                    } else if (input.changeType() == ChangeType.REMOVED) {
                        feature = geogig.command(RevObjectParse.class).setObjectId(input.oldObjectId()).call();
                        type = geogig.command(RevObjectParse.class)
                                .setObjectId(input.getOldObject().getMetadataId()).call();
                        path = input.getOldObject().path();
                    }
                    if (feature.isPresent() && feature.get() instanceof RevFeature && type.isPresent()
                            && type.get() instanceof RevFeatureType) {
                        RevFeatureType featureType = (RevFeatureType) type.get();
                        Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors();

                        for (PropertyDescriptor attrib : attribs) {
                            PropertyType attrType = attrib.getType();
                            if (attrType instanceof GeometryType) {
                                GeometryType gt = (GeometryType) attrType;
                                CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
                                if (crs != null) {
                                    try {
                                        crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
                                    } catch (FactoryException e) {
                                        crsCode = null;
                                    }
                                    if (crsCode != null) {
                                        crsCode = "EPSG:" + crsCode;
                                    }
                                }
                                break;
                            }
                        }

                        RevFeature revFeature = (RevFeature) feature.get();
                        FeatureBuilder builder = new FeatureBuilder(featureType);
                        GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder
                                .build(revFeature.getId().toString(), revFeature);
                        change = new GeometryChange(simpleFeature, input.changeType(), path, crsCode);
                    }
                    return change;
                }
            });

    while (changeIterator.hasNext() && (elementsPerPage == 0 || counter < elementsPerPage)) {
        GeometryChange next = changeIterator.next();
        if (next != null) {
            GeogigSimpleFeature feature = next.getFeature();
            ChangeType change = next.getChangeType();
            out.writeStartElement("Feature");
            writeElement("change", change.toString());
            writeElement("id", next.getPath());
            List<Object> attributes = feature.getAttributes();
            for (Object attribute : attributes) {
                if (attribute instanceof Geometry) {
                    writeElement("geometry", ((Geometry) attribute).toText());
                    break;
                }
            }
            if (next.getCRS() != null) {
                writeElement("crs", next.getCRS());
            }
            out.writeEndElement();
            counter++;
        }
    }
    if (changeIterator.hasNext()) {
        writeElement("nextPage", "true");
    }
}