Example usage for java.util AbstractCollection AbstractCollection

List of usage examples for java.util AbstractCollection AbstractCollection

Introduction

In this page you can find the example usage for java.util AbstractCollection AbstractCollection.

Prototype

protected AbstractCollection() 

Source Link

Document

Sole constructor.

Usage

From source file:org.openstreetmap.josm.tools.Utils.java

/**
 * Transforms the collection {@code c} into an unmodifiable collection and
 * applies the {@link Function} {@code f} on each element upon access.
 * @param <A> class of input collection
 * @param <B> class of transformed collection
 * @param c a collection//from   ww  w. ja va 2 s  .  c  om
 * @param f a function that transforms objects of {@code A} to objects of {@code B}
 * @return the transformed unmodifiable collection
 */
public static <A, B> Collection<B> transform(final Collection<? extends A> c, final Function<A, B> f) {
    return new AbstractCollection<B>() {

        @Override
        public int size() {
            return c.size();
        }

        @Override
        public Iterator<B> iterator() {
            return new Iterator<B>() {

                private final Iterator<? extends A> it = c.iterator();

                @Override
                public boolean hasNext() {
                    return it.hasNext();
                }

                @Override
                public B next() {
                    return f.apply(it.next());
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}

From source file:org.polymap.core.data.PipelineFeatureSource.java

public List<FeatureId> addFeatures(FeatureCollection<SimpleFeatureType, SimpleFeature> features,
        final ProgressListener monitor) throws IOException {
    checkWritePermission();/*from  w ww  . j a va2s.c  om*/

    monitor.started();

    //        FeatureType schema = getSchema();
    //        if (!schema.equals( features.getSchema() )) {
    //            log.warn( "addFeatures(): Given features have different schema - performing retype..." );
    //            features = new ReTypingFeatureCollection( features, (SimpleFeatureType)schema );
    //        }
    final FeatureCollection fc = features;
    // build a Collection that pipes the features through its Iterator; so
    // the features don't need to be loaded in memory all together; and no
    // chunks are needed; and events are sent correctly
    Collection coll = new AbstractCollection() {
        private volatile int size = -1;

        public int size() {
            return size < 0 ? size = fc.size() : size;
        }

        public Iterator iterator() {
            return new Iterator() {
                private FeatureIterator it = fc.features();
                private int count = 0;

                @Override
                public boolean hasNext() {
                    if (it != null && !it.hasNext()) {
                        it.close();
                        it = null;
                        return false;
                    } else {
                        return true;
                    }
                }

                @Override
                public Object next() {
                    if ((++count % 100) == 0) {
                        monitor.setTask(new SimpleInternationalString("" + count));
                        monitor.progress(100);
                    }
                    return it.next();
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }

                @Override
                protected void finalize() throws Throwable {
                    if (it != null) {
                        it.close();
                        it = null;
                    }
                }
            };
        }
    };

    try {
        final List<FeatureId> fids = new ArrayList(1024);
        // request
        AddFeaturesRequest request = new AddFeaturesRequest(features.getSchema(), coll);
        pipeline.process(request, new ResponseHandler() {
            public void handle(ProcessorResponse r) throws Exception {
                fids.addAll(((ModifyFeaturesResponse) r).getFeatureIds());
            }
        });

        // fire event
        store.listeners.fireFeaturesAdded(getSchema().getTypeName(), tx, null, false);

        monitor.complete();
        return fids;
    } catch (RuntimeException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.polymap.core.model2.store.feature.FeatureStoreUnitOfWork.java

@Override
public <T extends Entity> Collection find(Class<T> entityClass) {
    try {// w w  w.  j a v  a2 s .  c om
        // schema
        FeatureStore fs = featureSource(entityClass);
        FeatureType schema = fs.getSchema();

        // features (just IDs) 
        final FeatureCollection features = fs.getFeatures(
                new DefaultQuery(schema.getName().getLocalPart(), Filter.INCLUDE, new String[] {}));
        final Iterator<Feature> it = features.iterator();

        return new AbstractCollection<String>() {

            private LazyInit<Integer> size = new PlainLazyInit();

            public Iterator<String> iterator() {
                return Iterators.transform(it, new Function<Feature, String>() {
                    public String apply(Feature input) {
                        return input.getIdentifier().getID();
                    }
                });
            }

            public int size() {
                return size.get(new Supplier<Integer>() {
                    public Integer get() {
                        return features.size();
                    }
                });
            }

            public boolean isEmpty() {
                return size() == 0; //features.isEmpty();
            }

            protected void finalize() throws Throwable {
                if (it != null) {
                    features.close(it);
                }
            }
        };
    } catch (IOException e) {
        throw new ModelRuntimeException(e);
    }
}