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:eu.stratosphere.pact.runtime.sort.CombiningUnilateralSortMergerITCase.java

private static Iterator<Integer> getReducingIterator(MutableObjectIterator<Record> data,
        TypeSerializer<Record> serializer, TypeComparator<Record> comparator) {

    final KeyGroupedIterator<Record> groupIter = new KeyGroupedIterator<Record>(data, serializer, comparator);

    return new Iterator<Integer>() {

        private boolean hasNext = false;

        @Override//from   w  w w . ja v  a 2 s  .  co  m
        public boolean hasNext() {
            if (hasNext) {
                return true;
            }

            try {
                hasNext = groupIter.nextKey();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return hasNext;
        }

        @Override
        public Integer next() {
            if (hasNext()) {
                hasNext = false;

                Iterator<Record> values = groupIter.getValues();

                Record rec = null;
                int cnt = 0;
                while (values.hasNext()) {
                    rec = values.next();
                    cnt += rec.getField(1, IntValue.class).getValue();
                }

                return cnt;
            } else {
                throw new NoSuchElementException();
            }
        }

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

    };
}

From source file:com.vaushell.superpipes.tools.scribe.fb.FacebookClient.java

/**
 * Iterate a Facebook Feed./*from w  ww  .j a  v a  2 s  .com*/
 *
 * @param forcedTarget Target's ID (user or page). Could be null to use login target.
 * @param limit Maximum number of results by call. Could be null to use default.
 * @return a posts iterator
 */
public Iterator<FB_Post> iteratorFeed(final String forcedTarget, final Integer limit) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getClass().getSimpleName() + "] iteratorFeed() : forcedTarget=" + forcedTarget
                + " / limit=" + limit);
    }

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

                    List<FB_Post> linksFiltered;
                    do {
                        final Properties properties = new Properties();
                        if (limit != null) {
                            properties.setProperty("limit", Integer.toString(limit));
                        }
                        if (lastTimestamp != null) {
                            properties.setProperty("until",
                                    Long.toString(lastTimestamp.getMillis() / 1000L - 1L));
                        }

                        final String realTarget = forcedTarget == null ? target : forcedTarget;
                        final List<FB_Post> links = readFeedImpl(
                                "https://graph.facebook.com/" + realTarget + "/feed", properties);
                        if (links.isEmpty()) {
                            return false;
                        }

                        lastTimestamp = links.get(links.size() - 1).getCreatedTime();

                        linksFiltered = filter(links, realTarget);
                    } while (linksFiltered.isEmpty());

                    buffer.addAll(linksFiltered);

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

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

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

        // PRIVATE
        private final List<FB_Post> buffer = new ArrayList<>();
        private int bufferCursor;
        private DateTime lastTimestamp;
    };
}

From source file:org.normandra.orientdb.graph.OrientEdgeQuery.java

@Override
public Iterator<Edge<T>> iterator() {
    final Iterator<ODocument> itr = this.delegate.iterator();
    return new Iterator<Edge<T>>() {
        @Override/*from  w ww. j a va  2  s.c o m*/
        public boolean hasNext() {
            return itr.hasNext();
        }

        @Override
        public Edge next() {
            return graph.buildEdge(itr.next());
        }
    };
}

From source file:org.normandra.orientdb.graph.OrientNodeQuery.java

@Override
public Iterator<Node<T>> iterator() {
    final Iterator<ODocument> itr = this.delegate.iterator();
    return new Iterator<Node<T>>() {
        @Override/*from   w  ww.j a v a 2  s  .  c  o  m*/
        public boolean hasNext() {
            return itr.hasNext();
        }

        @Override
        public Node<T> next() {
            return graph.buildNode(itr.next());
        }
    };
}

From source file:com.oltpbenchmark.util.CollectionUtil.java

public static <T> Iterable<T> iterable(final T values[]) {
    return (new Iterable<T>() {
        @Override/*from w  w w .jav  a2 s .  co  m*/
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                private int idx = 0;

                @Override
                public boolean hasNext() {
                    return (this.idx < values.length);
                }

                @Override
                public T next() {
                    if (this.idx == values.length)
                        throw new NoSuchElementException();
                    return values[this.idx++];
                }

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

From source file:org.libreplan.business.workingday.IntraDayDate.java

private static Iterator<PartialDay> createIterator(final IntraDayDate start,
        final IterationPredicate predicate) {

    return new Iterator<IntraDayDate.PartialDay>() {
        private IntraDayDate current = start;

        @Override// w  w w . j  av a  2 s .co  m
        public boolean hasNext() {
            return predicate.hasNext(current);
        }

        @Override
        public PartialDay next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            IntraDayDate start = current;
            current = calculateNext(current);
            return new PartialDay(start, current);
        }

        private IntraDayDate calculateNext(IntraDayDate date) {
            IntraDayDate nextDay = IntraDayDate.startOfDay(date.date.plusDays(1));
            return predicate.limitNext(nextDay);
        }

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

From source file:com.ibm.research.rdf.store.jena.impl.DB2QueryExecutionImpl.java

@Override
public Iterator<Triple> execConstructTriples() {
    if (query.isConstructType()) {

        qp = QueryProcessorFactory.create(query.getDB2Query(), getConnection(), getStore(), getNativeContext(),
                compiler);/*from w  w  w .  jav a2s. c o  m*/
        rs = qp.execConstruct();
        if (rs == null) {
            return new LinkedList<Triple>().iterator();
        }

        final ConstructQuery q = query.getDB2Query().getConstructQuery();
        final java.sql.ResultSet sqlrs = rs.getResultSet();
        Iterator<Triple> ret = new Iterator<Triple>() {
            private int blankNodeId = 0;
            private boolean incrementBlankNodeId = false;

            private List<Triple> buf = new LinkedList<Triple>();

            {
                tryFillBuffer();
            }

            private void tryFillBuffer() {
                try {
                    while (buf.isEmpty() && sqlrs.next()) {
                        List<QueryTriple> triples = q.getConstructPattern();
                        for (int i = 0; i < triples.size(); i++) {
                            QueryTriple t = triples.get(i);

                            Node sNode = null;
                            Node pNode = null;
                            Node oNode = null;

                            QueryTripleTerm sub = t.getSubject();
                            if (sub.isVariable()) {
                                if (sub.getVariable() instanceof BlankNodeVariable) {
                                    sNode = new String2Node(Constants.NAME_COLUMN_SUBJECT,
                                            sub.getVariable().toString() + blankNodeId).getNode().asNode();
                                    incrementBlankNodeId = true;
                                } else {
                                    String dbValue = sqlrs.getString(sub.getVariable().getName().toLowerCase());
                                    if (dbValue != null) {
                                        sNode = new String2Node(Constants.NAME_COLUMN_SUBJECT, dbValue)
                                                .getNode().asNode();
                                    }
                                }
                            } else {
                                sNode = NodeFactory.createURI(sub.getIRI().getValue());
                            }

                            PropertyTerm pred = t.getPredicate();
                            if (pred.isVariable()) {
                                String varName = sqlrs.getString(pred.getVariable().getName().toLowerCase());
                                if (varName != null) {
                                    pNode = new String2Node(Constants.NAME_COLUMN_PREDICATE, varName).getNode()
                                            .asNode();
                                }
                            } else {
                                assert pred.isIRI() : pred; // complex path not allow in construct template
                                pNode = NodeFactory.createURI(pred.getIRI().getValue());
                            }

                            QueryTripleTerm obj = t.getObject();
                            if (obj.isVariable()) {
                                String varName = obj.getVariable().getName();
                                String dbvalue = sqlrs.getString(varName.toLowerCase());
                                if (dbvalue != null) {
                                    if (rs.isLiteralVariable(varName)) {
                                        short type = sqlrs
                                                .getShort((varName + Constants.TYP_COLUMN_SUFFIX_IN_SPARQL_RS)
                                                        .toLowerCase());
                                        oNode = new String2Node(Constants.NAME_COLUMN_OBJECT, dbvalue, type)
                                                .getNode().asNode();
                                    } else {
                                        oNode = new String2Node(Constants.NAME_COLUMN_SUBJECT, dbvalue)
                                                .getNode().asNode();
                                    }

                                }
                            } else {
                                if (obj.isIRI()) {
                                    oNode = NodeFactory.createURI(obj.getIRI().getValue());
                                } else {
                                    oNode = NodeFactory.createURI(obj.getConstant().toString());
                                }
                            }

                            if (oNode != null && pNode != null && sNode != null) {
                                buf.add(Triple.create(sNode, pNode, oNode));
                            }
                        }
                        if (incrementBlankNodeId) {
                            blankNodeId++;
                        }
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            public Triple next() {
                assert !buf.isEmpty();
                try {
                    return buf.remove(0);
                } finally {
                    if (buf.isEmpty()) {
                        tryFillBuffer();
                    }
                }
            }

            @Override
            public boolean hasNext() {
                return !buf.isEmpty();
            }

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

    throw new IllegalArgumentException("Query was executed as CONSTRUCT TRIPLE query, though it's not one");

}

From source file:com.taobao.common.store.journal.JournalStore.java

@Override
public Iterator<byte[]> iterator() throws IOException {
    final Iterator<BytesKey> it = this.indices.keyIterator();
    return new Iterator<byte[]>() {
        @Override/* w  w w .j  a  v a 2  s  .co  m*/
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public byte[] next() {
            final BytesKey bk = it.next();
            if (null != bk) {
                return bk.getData();
            }
            return null;
        }

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

From source file:com.thoughtworks.go.config.merge.MergePipelineConfigs.java

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

        private int currentIndex = 0;
        private int count = size();

        @Override//from w  w w .  j a v a2 s  .  c o  m
        public boolean hasNext() {
            return currentIndex < count;
        }

        @Override
        public PipelineConfig next() {
            return get(currentIndex++);
        }

        @Override
        public void remove() {
            throw new RuntimeException("Not implemented");
        }
    };
}

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

/**
 * Iterate over all ChEBI relationships of a certain type.
 * @return An iterator over all ChEBI relationships of a certain type.
 * @throws SQLException/*  w w  w.  ja v  a 2  s  . co  m*/
 */
public Iterator<BrendaChebiOntology.ChebiRelationship> getChebiRelationships(int relationshipType)
        throws SQLException {
    PreparedStatement stmt = brendaConn.prepareStatement(BrendaChebiOntology.ChebiRelationship.QUERY);
    BrendaChebiOntology.ChebiRelationship.bindType(stmt, relationshipType);
    stmt.setInt(1, relationshipType);
    final ResultSet results = stmt.executeQuery();

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

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

        }
    };
}