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.cocoon.components.expression.jxpath.JXPathExpression.java

public Iterator iterate(ExpressionContext context) throws ExpressionException {
    final JXPathContext jxpathContext = getContext(context);
    Object val = this.compiledExpression.getPointer(jxpathContext, this.expression).getNode();
    // FIXME: workaround for JXPath bug
    if (val instanceof NativeArray)
        return new JSIntrospector.NativeArrayIterator((NativeArray) val);
    else/*from   www.  j a v a2 s. c om*/
        return new Iterator() {
            Iterator iter = compiledExpression.iteratePointers(jxpathContext);

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

            public Object next() {
                return ((Pointer) iter.next()).getNode();
            }

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

From source file:org.apache.hadoop.hbase.util.PairOfSameType.java

@Override
public Iterator<T> iterator() {
    return new Iterator<T>() {
        private int returned = 0;

        @Override/*from w  w w .  j  ava  2  s .co m*/
        public boolean hasNext() {
            return this.returned < 2;
        }

        @Override
        public T next() {
            if (++this.returned == 1)
                return getFirst();
            else if (this.returned == 2)
                return getSecond();
            else
                throw new IllegalAccessError("this.returned=" + this.returned);
        }

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

From source file:com.indeed.lsmtree.core.TestImmutableBTreeIndex.java

public int[] createTree() throws IOException, InstantiationException, IllegalAccessException {
    Random r = new Random(0);
    final int[] ints = new int[treeSize];
    int previous = 0;
    for (int i = 0; i < ints.length; i++) {
        ints[i] = previous + r.nextInt(10) + 1;
        previous = ints[i];//w  w w .ja va2  s .co m
    }
    Iterator<Generation.Entry<Integer, Long>> iterator = new Iterator<Generation.Entry<Integer, Long>>() {

        int index = 0;

        @Override
        public boolean hasNext() {
            return index < ints.length;
        }

        @Override
        public Generation.Entry<Integer, Long> next() {
            int ret = ints[index];
            index++;
            return Generation.Entry.create(ret, (long) ret);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    ImmutableBTreeIndex.Writer.write(tmpDir, iterator, new IntSerializer(), new LongSerializer(), 65536, false);
    return ints;
}

From source file:org.apache.cocoon.el.impl.jxpath.JXPathExpression.java

public Iterator iterate(ObjectModel objectModel) throws ExpressionException {
    final JXPathContext jxpathContext = getContext(objectModel);
    Object val = this.compiledExpression.getPointer(jxpathContext, this.expression).getNode();
    // FIXME: workaround for JXPath bug
    if (val instanceof NativeArray)
        return new JSIntrospector.NativeArrayIterator((NativeArray) val);
    else// w ww  .  j ava  2  s . c  o  m
        return new Iterator() {
            Iterator iter = compiledExpression.iteratePointers(jxpathContext);

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

            public Object next() {
                return ((Pointer) iter.next()).getNode();
            }

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

From source file:org.hippoecm.frontend.plugins.reporting.ReportModel.java

@Override
public Iterator iterator(long first, long count) {
    load();// ww w.j ava 2s  .  c  o m
    if (resultSet != null) {
        try {
            final NodeIterator nodeIterator = resultSet.getNodes();
            return new Iterator<IModel>() {
                public boolean hasNext() {
                    return nodeIterator.hasNext();
                }

                public IModel next() {
                    return new JcrNodeModel(nodeIterator.nextNode());
                }

                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        } catch (RepositoryException ex) {
            log.error("Failed to obtain nodes from query result");
        }
    }
    return new ArrayList(0).iterator();
}

From source file:act.installer.UniprotInstaller.java

public static void main(String[] args)
        throws IOException, SAXException, ParserConfigurationException, CompoundNotFoundException {
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());/*from   www  .ja  va  2  s .  c  om*/
    }

    CommandLine cl = null;
    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        LOGGER.error("Argument parsing failed: %s", e.getMessage());
        HELP_FORMATTER.printHelp(UniprotInstaller.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        System.exit(1);
    }

    if (cl.hasOption("help")) {
        HELP_FORMATTER.printHelp(UniprotInstaller.class.getCanonicalName(), HELP_MESSAGE, opts, null, true);
        System.exit(1);
    }

    File uniprotFile = new File(cl.getOptionValue(OPTION_UNIPROT_PATH));
    String dbName = cl.getOptionValue(OPTION_DB_NAME);

    if (!uniprotFile.exists()) {
        String msg = String.format("Uniprot file path is null");
        LOGGER.error(msg);
        throw new RuntimeException(msg);
    } else {
        MongoDB db = new MongoDB("localhost", 27017, dbName);

        DBIterator iter = db.getDbIteratorOverOrgs();

        Iterator<Organism> orgIterator = new Iterator<Organism>() {
            @Override
            public boolean hasNext() {
                boolean hasNext = iter.hasNext();
                if (!hasNext)
                    iter.close();
                return hasNext;
            }

            @Override
            public Organism next() {
                DBObject o = iter.next();
                return db.convertDBObjectToOrg(o);
            }

        };

        OrgMinimalPrefixGenerator prefixGenerator = new OrgMinimalPrefixGenerator(orgIterator);
        Map<String, String> minimalPrefixMapping = prefixGenerator.getMinimalPrefixMapping();

        UniprotInstaller installer = new UniprotInstaller(uniprotFile, db, minimalPrefixMapping);
        installer.init();
    }
}

From source file:com.addthis.bundle.core.list.ListBundle.java

@Override
public Iterator<BundleField> iterator() {
    return new Iterator<BundleField>() {
        private final Iterator<BundleField> iter = format.iterator();
        private BundleField peek = null;
        private BundleField prev = null;

        @Override/*w w  w  .jav  a2 s  .  c  o m*/
        public boolean hasNext() {
            while (peek == null && iter.hasNext()) {
                BundleField next = iter.next();
                ValueObject value = getRawValue(next);
                if (value == SKIP) {
                    continue;
                }
                peek = next;
                break;
            }
            return peek != null;
        }

        @Override
        public BundleField next() {
            if (hasNext()) {
                BundleField next = peek;
                prev = next;
                peek = null;
                return next;
            }
            throw new NoSuchElementException();
        }

        @Override
        public void remove() {
            checkState(prev != null, "Next has not yet been called or remove already has");
            removeValue(prev);
            prev = null;
        }
    };
}

From source file:Main.java

public static Iterable<Node> eval(Node element, String path) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    // xpath.setNamespaceContext(new NamespaceContext() {
    ////from w  ww .jav  a 2s  . co m
    // /**
    // * @WARNING this code will work only if the namespace is present at
    // * each node of the xml dom and within the xpath queries.
    // * Otherwise you can fix like that:
    // *
    // * <pre>
    // * // FIXME: this is a
    // * hack!! if ("atom".equals(prefix)) return
    // * "http://www.w3.org/2005/Atom"; but it wont work with
    // * </pre>
    // *
    // * different namespaces
    // * @see
    // javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
    // */
    // public String getNamespaceURI(String prefix) {
    // String namespace = DOMUtil.lookupNamespaceURI(node, prefix);
    // return namespace;
    // }
    //
    // public String getPrefix(String namespaceURI) {
    // String prefix = node.lookupPrefix(namespaceURI);
    // return prefix;
    // }
    //
    // public Iterator<?> getPrefixes(final String namespaceURI) {
    // return new Iterator<String>() {
    // String ns = getPrefix(namespaceURI);
    //
    // public boolean hasNext() {
    // return ns != null;
    // }
    //
    // public String next() {
    // String r = ns;
    // ns = null;
    // return r;
    // }
    //
    // public void remove() {
    // }
    //
    // };
    // }
    // });
    XPathExpression expr = xpath.compile(path);
    final NodeList result = (NodeList) expr.evaluate(element, XPathConstants.NODESET);
    return new Iterable<Node>() {
        public Iterator<Node> iterator() {
            return new Iterator<Node>() {
                int len = result.getLength();

                int pos;

                public boolean hasNext() {
                    return pos < len;
                }

                public Node next() {
                    if (pos >= len) {
                        return null;
                    }
                    return result.item(pos++);
                }

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

            };
        }
    };
}

From source file:de.icongmbh.oss.maven.plugin.javassist.ClassnameExtractor.java

/**
 * Wrapping the iterator (as reference) of class file names and extract full qualified class name
 * on//  w w  w  .  jav  a 2 s  . c o  m
 * {@link Iterator#next()}.
 * <p>
 * It is possible that {@link Iterator#hasNext()} returns {@code true} and {@link Iterator#next()}
 * returns {@code null}.
 * </p>
 *
 * @param parentDirectory to remove from {@code classFile} and maybe {@code null}.
 * @param classFileIterator to extract the names from. Must not be {@code null}.
 * @return iterator of full qualified class names based on passed classFiles or {@code null}
 * @see #extractClassNameFromFile(File, File)
 */
// DANGEROUS call by reference
public static Iterator<String> iterateClassnames(final File parentDirectory,
        final Iterator<File> classFileIterator) {
    return new Iterator<String>() {

        @Override
        public boolean hasNext() {
            return classFileIterator == null ? false : classFileIterator.hasNext();
        }

        @Override
        public String next() {
            final File classFile = classFileIterator.next();
            try {
                // possible returns null
                return extractClassNameFromFile(parentDirectory, classFile);
            } catch (final IOException e) {
                throw new RuntimeException(e.getMessage());
            }
        }

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

From source file:de.escalon.hypermedia.spring.uber.AbstractUberNode.java

/**
 * Allows iterating over children of this uber node which have a data attribute.
 */// w w  w  .j  ava  2 s .co m
@Override
public Iterator<UberNode> iterator() {

    return new Iterator<UberNode>() {

        int index = 0;

        @Override
        public void remove() {
            throw new UnsupportedOperationException("removing from uber node is not supported");
        }

        @Override
        public UberNode next() {
            index = findNextChildWithData();
            return data.get(index++);
        }

        @Override
        public boolean hasNext() {
            return findNextChildWithData() != -1;
        }

        private int findNextChildWithData() {
            for (int i = index; i < data.size(); i++) {
                if (!data.get(i).getData().isEmpty()) {
                    return i;
                }
            }
            return -1;
        }

    };
}