All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.filosganga.geogson.jts.JtsGeometryCollectionIterable Maven / Gradle / Ivy

package com.github.filosganga.geogson.jts;

import com.google.common.collect.UnmodifiableIterator;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;

/**
 * A {@link Geometry} Iterable, to wrap the getGeometryN and getNumGeometries JTS methods.
 */
class JtsGeometryCollectionIterable implements Iterable {

    private final GeometryProvider src;

    private JtsGeometryCollectionIterable(GeometryProvider src) {
        this.src = src;
    }

    public static JtsGeometryCollectionIterable of(final GeometryCollection src) {
        return new JtsGeometryCollectionIterable(new GeometryProvider() {
            @Override
            public int getNumGeometries() {
                return src.getNumGeometries();
            }

            @Override
            public Geometry getGeometryN(int n) {
                return src.getGeometryN(n);
            }
        });
    }


    @Override
    public UnmodifiableIterator iterator() {
        return new Iterator(this.src);
    }

    private static class Iterator extends UnmodifiableIterator {

        private final GeometryProvider src;

        private int index = 0;

        private Iterator(GeometryProvider src) {
            this.src = src;
        }

        @Override
        public boolean hasNext() {
            return this.src.getNumGeometries() > this.index;
        }

        @Override
        public Geometry next() {
            return this.src.getGeometryN(this.index++);
        }

    }

    private interface GeometryProvider {

        int getNumGeometries();

        Geometry getGeometryN(int n);

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy