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:la.alsocan.jsonshapeshifter.Transformation.java

/**
 * Iterates through the target schema to look for {@link SchemaNode} still requiring a 
 * binding. This iterates through the nodes of the target schema but skips those which 
 * already have a binding defined as well as <i>object</i> and <i>null</i> nodes (which
 * do not require any binding).<br>
 * <br>// ww w.j av  a2 s  .co  m
 * When {@link Iterator#hasNext()} returns <code>false</code>, the transformation can 
 * be considered fully defined (and invoking {@link #apply(JsonNode)} is safe).
 * 
 * @return An iterator for all target {@link SchemaNode} still requiring a binding
 */
public Iterator<SchemaNode> toBind() {

    final Iterator<SchemaNode> itNodes = target.iterator();
    return new Iterator<SchemaNode>() {

        private SchemaNode remainingNode = getNextNode();

        @Override
        public boolean hasNext() {
            return remainingNode != null;
        }

        @Override
        public SchemaNode next() {
            if (remainingNode == null) {
                throw new NoSuchElementException();
            }
            SchemaNode toReturn = remainingNode;
            remainingNode = getNextNode();
            return toReturn;
        }

        private SchemaNode getNextNode() {
            SchemaNode node = null;
            do {
                if (!itNodes.hasNext()) {
                    return null;
                }
                node = itNodes.next();
            } while (bindings.containsKey(node) || ENodeType.OBJECT.equals(node.getType())
                    || ENodeType.NULL.equals(node.getType()));
            return node;
        }
    };
}

From source file:org.apache.solr.client.solrj.TestSolrJErrorHandling.java

Iterator<SolrInputDocument> manyDocs(final int base, final int numDocs) {
    return new Iterator<SolrInputDocument>() {
        int count = 0;

        @Override//  ww w. j a  v a 2  s . c om
        public boolean hasNext() {
            return count < numDocs;
        }

        @Override
        public SolrInputDocument next() {
            int id = base + count++;
            if (count == 1) { // first doc is legit, and will increment a counter
                return sdoc("id", "test", "count_i", map("inc", 1));
            }
            // include "ignore_exception" so the log doesn't fill up with known exceptions, and change the values for each doc
            // so binary format won't compress too much
            return sdoc("id", Integer.toString(id), "ignore_exception_field_does_not_exist_" + id,
                    "fieldval" + id);
        }

        @Override
        public void remove() {
        }
    };
}

From source file:MicroMap.java

/**
 * @see java.util.Map#keySet()/*from   w w w.j  a  va  2s  .  c  o  m*/
 */
public Set keySet() {
    return new AbstractSet() {
        public Iterator iterator() {
            return new Iterator() {
                public boolean hasNext() {
                    return index < MicroMap.this.size();
                }

                public Object next() {
                    index++;

                    return key;
                }

                public void remove() {
                    MicroMap.this.clear();
                }

                int index;
            };
        }

        public int size() {
            return MicroMap.this.size();
        }
    };
}

From source file:com.cloudera.recordbreaker.analyzer.UnknownTextSchemaDescriptor.java

/**
 * Iterate through Avro-encoded rows of the file
 *///ww  w . j a va  2 s .  c o m
public Iterator getIterator() {
    return new Iterator() {
        int lineno = 0;
        BufferedReader in = null;
        Object nextElt = null;
        {
            try {
                in = new BufferedReader(new InputStreamReader(dd.getRawBytes()));
                nextElt = lookahead();
            } catch (IOException iex) {
                LOG.info("iex: " + iex.toString());
                nextElt = null;
            }
        }

        public boolean hasNext() {
            return nextElt != null;
        }

        public synchronized Object next() {
            Object toReturn = nextElt;
            nextElt = lookahead();
            return toReturn;
        }

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

        Object lookahead() {
            try {
                String str = null;
                while ((str = in.readLine()) != null) {
                    GenericContainer resultObj = typeTree.parse(str);
                    lineno++;
                    if (resultObj != null) {
                        return resultObj;
                    }
                }
                if (in != null) {
                    in.close();
                    in = null;
                }
            } catch (IOException iex) {
                iex.printStackTrace();
            }
            return null;
        }
    };
}

From source file:Heap.java

/**
 * Returns an iterator that iterates over all elements of the heap, in no particular order
 * @return//from  w w  w. java 2 s  . c o m
 */
public final Iterator<T> iterator() {
    return new Iterator<T>() {
        private Iterator<Node> iterator = heap.iterator();

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

        @Override
        public T next() {
            return iterator.next().element;
        }

        @Override
        public void remove() {
        }
    };
}

From source file:org.apache.rya.indexing.KeyParts.java

@Override
public Iterator<KeyParts> iterator() {
    final String[] strategies = new String[] { CQ_O_AT, CQ_S_P_AT, CQ_P_AT, CQ_S_AT }; // CQ_END?
    assert !queryMode : "iterator for queryMode is not immplemented";
    if (queryMode) {
        return null;
    }//from  ww w  .  j  a v  a  2s  . co m

    // if (!queryMode)
    return new Iterator<KeyParts>() {
        int nextStrategy = 0;

        @Override
        public boolean hasNext() {
            return nextStrategy < strategies.length;
        }

        @Override
        public KeyParts next() {
            assert (statement != null);
            Text keyText = new Text();
            // increment++ the next strategy AFTER getting the value
            switch (nextStrategy++) {
            case 0: // index o+hash(p+s)
                assert (CQ_O_AT.equals(strategies[0]));
                keyText = new Text(instant.getAsKeyBytes());
                KeyParts.appendUniqueness(statement, keyText);
                return new KeyParts(keyText, new Text(StatementSerializer.writeContext(statement)),
                        new Text(CQ_O_AT), statement);
            case 1:// index hash(s+p)+o
                assert (CQ_S_P_AT.equals(strategies[1]));
                KeyParts.appendSubjectPredicate(statement, keyText);
                KeyParts.appendInstant(instant, keyText);
                // appendUniqueness -- Not needed since it is already unique.
                return new KeyParts(keyText, new Text(StatementSerializer.writeContext(statement)),
                        new Text(CQ_S_P_AT), statement);
            case 2: // index hash(p)+o
                assert (CQ_P_AT.equals(strategies[2]));
                KeyParts.appendPredicate(statement, keyText);
                KeyParts.appendInstant(instant, keyText);
                KeyParts.appendUniqueness(statement, keyText);
                return new KeyParts(keyText, new Text(StatementSerializer.writeContext(statement)),
                        new Text(CQ_P_AT), statement);
            case 3: // index hash(s)+o
                assert (CQ_S_AT.equals(strategies[3]));
                KeyParts.appendSubject(statement, keyText);
                KeyParts.appendInstant(instant, keyText);
                KeyParts.appendUniqueness(statement, keyText);
                return new KeyParts(keyText, new Text(StatementSerializer.writeContext(statement)),
                        new Text(CQ_S_AT), statement);
            }
            throw new Error("Next passed end?  No such nextStrategy=" + (nextStrategy - 1));

        }

        @Override
        public void remove() {
            throw new Error("Remove not Implemented.");
        }
    };
}

From source file:de.codesourcery.jasm16.ast.ASTUtils.java

public static Iterator<ASTNode> createInOrderIterator(ASTNode node) {
    if (node == null) {
        throw new IllegalArgumentException("node must not be NULL");
    }/*from w w  w.  j  a va2s.  c  o m*/
    final Stack<ASTNode> stack = new Stack<ASTNode>();
    stack.push(node);
    return new Iterator<ASTNode>() {

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

        /*    A (1)
         *    |\
         *(5) E B (2)
         *    |\ 
         *(4) D C (3)
         */
        @Override
        public ASTNode next() {
            if (stack.isEmpty()) {
                throw new NoSuchElementException();
            }
            ASTNode result = stack.pop();

            final List<ASTNode> children = result.getChildren();
            Collections.reverse(children);

            for (ASTNode child : children) {
                stack.push(child);
            }
            return result;
        }

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

From source file:org.apache.kylin.dict.lookup.cache.RocksDBLookupTableTest.java

private ILookupTable getLookupTableWithRandomData(final int rowNum) {
    return new ILookupTable() {
        @Override//from  w ww  .j av  a 2s. c o  m
        public String[] getRow(Array<String> key) {
            return new String[0];
        }

        @Override
        public void close() throws IOException {

        }

        @Override
        public Iterator<String[]> iterator() {
            return new Iterator<String[]>() {
                private int iterCnt = 0;

                @Override
                public boolean hasNext() {
                    return iterCnt < rowNum;
                }

                @Override
                public String[] next() {
                    iterCnt++;
                    return genRandomRow(iterCnt);
                }

                @Override
                public void remove() {

                }
            };
        }
    };
}

From source file:mvm.rya.indexing.KeyParts.java

@Override
public Iterator<KeyParts> iterator() {
    final String[] strategies = new String[] { CQ_O_AT, CQ_S_P_AT, CQ_P_AT, CQ_S_AT }; // CQ_END?
    assert !queryMode : "iterator for queryMode is not immplemented";
    if (queryMode)
        return null;

    // if (!queryMode)
    return new Iterator<KeyParts>() {
        int nextStrategy = 0;

        @Override/*from w ww.  j ava2s  .  co  m*/
        public boolean hasNext() {
            return nextStrategy < strategies.length;
        }

        @Override
        public KeyParts next() {
            assert (statement != null);
            Text keyText = new Text();
            // increment++ the next strategy AFTER getting the value
            switch (nextStrategy++) {
            case 0: // index o+hash(p+s)
                assert (CQ_O_AT.equals(strategies[0]));
                keyText = new Text(instant.getAsKeyBytes());
                KeyParts.appendUniqueness(statement, keyText);
                return new KeyParts(keyText, new Text(StatementSerializer.writeContext(statement)),
                        new Text(CQ_O_AT), statement);
            case 1:// index hash(s+p)+o
                assert (CQ_S_P_AT.equals(strategies[1]));
                KeyParts.appendSubjectPredicate(statement, keyText);
                KeyParts.appendInstant(instant, keyText);
                // appendUniqueness -- Not needed since it is already unique.
                return new KeyParts(keyText, new Text(StatementSerializer.writeContext(statement)),
                        new Text(CQ_S_P_AT), statement);
            case 2: // index hash(p)+o
                assert (CQ_P_AT.equals(strategies[2]));
                KeyParts.appendPredicate(statement, keyText);
                KeyParts.appendInstant(instant, keyText);
                KeyParts.appendUniqueness(statement, keyText);
                return new KeyParts(keyText, new Text(StatementSerializer.writeContext(statement)),
                        new Text(CQ_P_AT), statement);
            case 3: // index hash(s)+o
                assert (CQ_S_AT.equals(strategies[3]));
                KeyParts.appendSubject(statement, keyText);
                KeyParts.appendInstant(instant, keyText);
                KeyParts.appendUniqueness(statement, keyText);
                return new KeyParts(keyText, new Text(StatementSerializer.writeContext(statement)),
                        new Text(CQ_S_AT), statement);
            }
            throw new Error("Next passed end?  No such nextStrategy=" + (nextStrategy - 1));

        }

        @Override
        public void remove() {
            throw new Error("Remove not Implemented.");
        }
    };
}

From source file:info.rmarcus.birkhoffvonneumann.MatrixUtils.java

private static Iterator<BitSet> bitStringsOfSize(int n) {
    return new Iterator<BitSet>() {
        private BigInteger b = new BigInteger("0");

        @Override//from w w  w . j a v  a2  s .  com
        public boolean hasNext() {
            return b.compareTo((new BigInteger("2")).pow(n)) < 0;
        }

        @Override
        public @NonNull BitSet next() {
            byte[] bytes = b.toByteArray();
            BitSet toR = BitSet.valueOf(bytes);
            BigInteger nxt = b.add(new BigInteger("1"));
            if (nxt == null || toR == null) {
                throw new NoSuchElementException("Unable to increment big int");
            }
            b = nxt;
            return toR;
        }

    };
}