Example usage for java.util Iterator Iterator

List of usage examples for java.util Iterator Iterator

Introduction

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

Prototype

Iterator

Source Link

Usage

From source file:org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry.java

/**
 * Returns an iterator over all registered ImagePreloader instances.
 * @return an iterator over ImagePreloader instances.
 *///from  ww w  . j ava2  s  .com
public Iterator getPreloaderIterator() {
    sortPreloaders();
    final Iterator iter = this.preloaders.iterator();
    //Unpack the holders
    return new Iterator() {

        public boolean hasNext() {
            return iter.hasNext();
        }

        public Object next() {
            Object obj = iter.next();
            if (obj != null) {
                return ((PreloaderHolder) obj).preloader;
            } else {
                return null;
            }
        }

        public void remove() {
            iter.remove();
        }

    };
}

From source file:org.onehippo.forge.exdocpicker.impl.field.ExternalDocumentFieldSelectorPlugin.java

private RefreshingView<? extends Serializable> createRefreshingView(
        final ExternalDocumentCollection<Serializable> docCollection) {

    return new RefreshingView<Serializable>("view") {

        private static final long serialVersionUID = 1L;

        private IDataProvider<Serializable> dataProvider = new SimpleExternalDocumentCollectionDataProvider(
                docCollection);//  w w w.  j  a  v  a2s .c om

        @Override
        protected Iterator getItemModels() {

            final Iterator<? extends Serializable> baseIt = dataProvider.iterator(0, docCollection.getSize());

            return new Iterator<IModel<? extends Serializable>>() {
                public boolean hasNext() {
                    return baseIt.hasNext();
                }

                public IModel<? extends Serializable> next() {
                    return dataProvider.model(baseIt.next());
                }

                public void remove() {
                    baseIt.remove();
                }
            };
        }

        @Override
        protected void populateItem(Item item) {
            final Serializable doc = (Serializable) item.getModelObject();

            final Label label = new Label("link-text", new Model<String>() {
                private static final long serialVersionUID = 1L;

                @Override
                public String getObject() {
                    return getExternalDocumentServiceFacade().getDocumentTitle(
                            getExternalDocumentServiceContext(), doc, getRequest().getLocale());
                }
            });
            final String description = getExternalDocumentServiceFacade()
                    .getDocumentDescription(getExternalDocumentServiceContext(), doc, getRequest().getLocale());
            if (StringUtils.isNotBlank(description)) {
                label.add(new AttributeModifier("title", description));
            }
            item.add(label);

            if (item.getIndex() == docCollection.getSize() - 1) {
                item.add(new AttributeAppender("class", new Model("last"), " "));
            }

            addControlsToListItem(docCollection, item);
        }
    };
}

From source file:oculus.aperture.common.JSONProperties.java

@Override
public Iterable<Long> getLongs(String key) {
    try {//from ww w  . j ava2s  .c  o m
        final JSONArray array = obj.getJSONArray(key);

        return new Iterable<Long>() {
            @Override
            public Iterator<Long> iterator() {
                return new Iterator<Long>() {
                    private final int n = array.length();
                    private int i = 0;

                    @Override
                    public boolean hasNext() {
                        return n > i;
                    }

                    @Override
                    public Long next() {
                        try {
                            return (n > i) ? array.getLong(i++) : null;
                        } catch (JSONException e) {
                            return null;
                        }
                    }

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

    } catch (JSONException e) {
        return EmptyIterable.instance();
    }
}

From source file:act.installer.brenda.SQLConnection.java

/**
 * Iterate over all ChEBI ontologies.// w  w w .  j  ava2s  .co m
 * @return An iterator over all ChEBI ontologies.
 * @throws SQLException
 */
public Iterator<BrendaChebiOntology.ChebiOntology> getChebiOntologies() throws SQLException {
    final PreparedStatement stmt = brendaConn.prepareStatement(BrendaChebiOntology.ChebiOntology.QUERY);
    final ResultSet results = stmt.executeQuery();

    return new Iterator<BrendaChebiOntology.ChebiOntology>() {
        @Override
        public boolean hasNext() {
            return SQLConnection.hasNextHelper(results, stmt);
        }

        @Override
        public BrendaChebiOntology.ChebiOntology next() {
            try {
                results.next();
                return BrendaChebiOntology.ChebiOntology.fromResultSet(results);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    };
}

From source file:com.vaushell.superpipes.tools.scribe.tumblr.TumblrClient.java

/**
 * Iterate a Tumblr Feed.//from  ww w . j  a  v a2s. c o  m
 *
 * @param blogname Blog name
 * @param limit Maximum number of results by call. Could be null to use default.
 * @return a posts iterator
 */
public Iterator<TB_Post> iteratorFeed(final String blogname, final Integer limit) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getClass().getSimpleName() + "] iteratorFeed() : blogname=" + blogname + " / limit="
                + limit);
    }

    return new Iterator<TB_Post>() {
        @Override
        public boolean hasNext() {
            try {
                if (bufferCursor < buffer.size()) {
                    return true;
                } else {
                    buffer.clear();
                    bufferCursor = 0;

                    final Properties properties = new Properties();
                    properties.setProperty("api_key", getKey());
                    properties.setProperty("filter", "raw");
                    if (limit != null) {
                        properties.setProperty("limit", Integer.toString(limit));
                    }
                    if (offset > 0) {
                        properties.setProperty("offset", Integer.toString(offset));
                    }

                    final List<TB_Post> links = readFeedImpl(
                            "http://api.tumblr.com/v2/blog/" + blogname + "/posts/", properties);
                    if (links.isEmpty()) {
                        return false;
                    } else {
                        offset += links.size();

                        buffer.addAll(links);

                        return true;
                    }
                }
            } catch (final TumblrException | IOException ex) {
                throw new RuntimeException(ex);
            }
        }

        @Override
        public TB_Post next() {
            return buffer.get(bufferCursor++);
        }

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

        // PRIVATE
        private final List<TB_Post> buffer = new ArrayList<>();
        private int bufferCursor;
        private int offset;
    };
}

From source file:org.ovirt.api.metamodel.tests.XmlWriterTest.java

/**
 * Checks that one million of VMs can be serialized in a reasonable time and without exhausting the memory of the
 * virtual machine. Note that the test is currently disabled because if it fails it will block other tests, but it
 * is still useful to run it manually, so please don't remove it.
 */// w w w.  ja v  a  2 s.com
@Test
@Ignore
public void testOneMillion() throws IOException {
    // Create a iterator that generates the objects:
    Iterator<V4Vm> infinite = new Iterator<V4Vm>() {
        private int count;

        @Override
        public boolean hasNext() {
            return count < 1_000_000;
        }

        @Override
        public V4Vm next() {
            // Note that these are extremely simple objects, they should probably be more complicated for this
            // test to be realistic:
            V4Vm object = vm().id(String.valueOf(count)).name("vm" + count)
                    .disks(disk().id("123").alias("disk1")).disks(disk().id("456").alias("disk2")).build();
            count++;
            return object;
        }
    };

    // Write the objects generated by the iterator to a null stream, so that the only work performed is creating
    // the objects and converting them to XML:
    long before = System.currentTimeMillis();
    try (XmlWriter writer = new XmlWriter(new NullOutputStream(), false)) {
        V4XmlVmWriter.writeMany(infinite, writer);
    }
    long after = System.currentTimeMillis();
    long elapsed = after - before;

    // Check if it took less than a minute (it should take much more less, approx 2s, but lets be
    // conservative:
    assertTrue(elapsed < 60_000_000);
}

From source file:Cache.java

public synchronized Set<Map.Entry<K, V>> entrySet() {
    // Warning -- this method returns CacheObject instances and not Objects
    // in the same form they were put into cache.

    // First, clear all entries that have been in cache longer than the
    // maximum defined age.
    deleteExpiredEntries();//from   w w  w. j ava2s . c o m

    return new AbstractSet<Map.Entry<K, V>>() {
        private final Set<Map.Entry<K, CacheObject<V>>> set = map.entrySet();

        public Iterator<Entry<K, V>> iterator() {
            return new Iterator<Entry<K, V>>() {
                private final Iterator<Entry<K, CacheObject<V>>> it = set.iterator();

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

                public Entry<K, V> next() {
                    Map.Entry<K, CacheObject<V>> entry = it.next();
                    return new AbstractMapEntry<K, V>(entry.getKey(), entry.getValue().object) {
                        @Override
                        public V setValue(V value) {
                            throw new UnsupportedOperationException("Cannot set");
                        }
                    };
                }

                public void remove() {
                    it.remove();
                }
            };

        }

        public int size() {
            return set.size();
        }
    };
}

From source file:org.hippoecm.frontend.editor.builder.RenderPluginEditorPlugin.java

protected Iterator<ILayoutTransition> getTransitionIterator() {
    if (layoutContext != null) {
        final ILayoutPad pad = layoutContext.getLayoutPad();
        final Iterator<String> transitionIter = pad.getTransitions().iterator();
        return new Iterator<ILayoutTransition>() {

            public boolean hasNext() {
                return transitionIter.hasNext();
            }//  w  w w. jav  a  2s. co  m

            public ILayoutTransition next() {
                return pad.getTransition(transitionIter.next());
            }

            public void remove() {
                transitionIter.remove();
            }

        };
    } else {
        List<ILayoutTransition> list = Collections.emptyList();
        return list.iterator();
    }
}

From source file:com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfParser.java

/**
 * Make a NodeList behave like Iterable<Element>.
 * @param n/*  w w w .j  a va  2  s.  c om*/
 * @return
 */
private Iterable<Element> iterableElements(final NodeList n) {
    return () -> new Iterator<Element>() {
        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < n.getLength();
        }

        @Override
        public Element next() {
            if (hasNext()) {
                return (Element) n.item(index++);
            } else {
                throw new NoSuchElementException();
            }
        }

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

From source file:org.apache.blur.kvs.HdfsKeyValueStore.java

private Iterable<Entry<BytesRef, BytesRef>> getIterable(NavigableMap<BytesRef, Value> map) {
    final Set<Entry<BytesRef, Value>> entrySet = map.entrySet();
    return new Iterable<Entry<BytesRef, BytesRef>>() {
        @Override/*from   w  w w.  j av  a 2s  .com*/
        public Iterator<Entry<BytesRef, BytesRef>> iterator() {
            final Iterator<Entry<BytesRef, Value>> iterator = entrySet.iterator();
            return new Iterator<Entry<BytesRef, BytesRef>>() {

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

                @Override
                public Entry<BytesRef, BytesRef> next() {
                    final Entry<BytesRef, Value> e = iterator.next();
                    return new Entry<BytesRef, BytesRef>() {

                        @Override
                        public BytesRef setValue(BytesRef value) {
                            throw new RuntimeException("Read only.");
                        }

                        @Override
                        public BytesRef getValue() {
                            return e.getValue()._bytesRef;
                        }

                        @Override
                        public BytesRef getKey() {
                            return e.getKey();
                        }
                    };
                }

                @Override
                public void remove() {
                    throw new RuntimeException("Read only.");
                }
            };
        }
    };
}