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:XmlUtils.java

public static Collection<Node> wrapNodeList(final NodeList nodeList) throws IllegalArgumentException {
    if (nodeList == null)
        throw new IllegalArgumentException("Cannot wrap null NodeList");

    return new Collection<Node>() {
        @Override/*from   ww w. j  ava2  s.c  o m*/
        public int size() {
            return nodeList.getLength();
        }

        @Override
        public boolean isEmpty() {
            return nodeList.getLength() > 0;
        }

        @Override
        public boolean contains(final Object o) {
            if (o == null || !(o instanceof Node))
                return false;
            for (int i = 0; i < nodeList.getLength(); ++i)
                if (o == nodeList.item(i))
                    return true;
            return false;
        }

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

                @Override
                public boolean hasNext() {
                    return nodeList.getLength() > this.index;
                }

                @Override
                public Node next() {
                    if (this.index >= nodeList.getLength())
                        throw new NoSuchElementException();
                    return nodeList.item(this.index++);
                }

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

        @Override
        public Object[] toArray() {
            final Node[] array = new Node[nodeList.getLength()];
            for (int i = 0; i < array.length; ++i)
                array[i] = nodeList.item(i);
            return array;
        }

        @Override
        @SuppressWarnings({ "unchecked" })
        public <T> T[] toArray(final T[] a) throws ArrayStoreException {
            if (!a.getClass().getComponentType().isAssignableFrom(Node.class))
                throw new ArrayStoreException(
                        a.getClass().getComponentType().getName() + " is not the same or a supertype of Node");

            if (a.length >= nodeList.getLength()) {
                for (int i = 0; i < nodeList.getLength(); ++i)
                    a[i] = (T) nodeList.item(i);
                if (a.length > nodeList.getLength())
                    a[nodeList.getLength()] = null;
                return a;
            }

            return (T[]) toArray();
        }

        @Override
        public boolean add(final Node node) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean remove(final Object o) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean containsAll(final Collection<?> c) {
            for (final Object o : c)
                if (!this.contains(o))
                    return false;
            return true;
        }

        @Override
        public boolean addAll(final Collection<? extends Node> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean removeAll(final Collection<?> c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean retainAll(final Collection<?> c) {
            throw new UnsupportedOperationException();
        }

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

From source file:Main.java

/**
 * Extracts child elements with given name from node whith type <code>ELEMENT_NODE</code>.
 *
 * @param node        root node of XML document for search.
 * @param elementName name of elements that should be returned.
 * @return iterator with proper node childs.
 *//*  w  w  w . j a  v  a2s  .co m*/
public static Iterator<Element> getChildElements(final Node node, final String elementName) {
    //        node.normalize();
    return new Iterator<Element>() {
        private final NodeList nodes = node.getChildNodes();
        private int nextPos = 0;
        private Element nextElement = seekNext();

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

        public Element next() {
            if (nextElement == null)
                throw new NoSuchElementException();
            final Element result = nextElement;
            nextElement = seekNext();
            return result;
        }

        public void remove() {
            throw new UnsupportedOperationException("operation not supported");
        }

        private Element seekNext() {
            for (int i = nextPos, len = nodes.getLength(); i < len; i++) {
                final Node childNode = nodes.item(i);
                if (childNode.getNodeType() == Node.ELEMENT_NODE
                        && childNode.getNodeName().equals(elementName)) {
                    nextPos = i + 1;
                    return (Element) childNode;
                }
            }
            return null;
        }
    };
}

From source file:org.cmg.ml.sam.core.sim.SimulationSeries.java

public Iterable<SimulationData> getSeries() {
    return new Iterable<SimulationData>() {

        @Override/*www .  ja v a 2s.co  m*/
        public Iterator<SimulationData> iterator() {
            return new Iterator<SimulationData>() {

                private int counter;

                @Override
                public boolean hasNext() {
                    return counter < data.length;
                }

                @Override
                public SimulationData next() {
                    SimulationData toReturn = null;
                    if (counter < data.length) {
                        toReturn = new SimulationData(dt * counter, data[counter].getMean(),
                                data[counter].getStandardDeviation() / Math.sqrt(iterations));
                        counter++;
                    }
                    return toReturn;
                }

                @Override
                public void remove() {
                }

            };
        }

    };
}

From source file:cherry.goods.paginate.Range.java

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

        private long current = from - 1L;

        @Override//  w w  w  .  j  a  v  a2  s .c om
        public boolean hasNext() {
            return current < to;
        }

        @Override
        public Long next() {
            if (current >= to) {
                throw new IllegalStateException();
            }
            current += 1L;
            return current;
        }

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

From source file:Main.java

/**
 * Create new {@link Iterable} object which combine the first and the second
 * parameters.//from   ww  w  . ja va  2  s  . c  om
 * 
 * @param <T>
 *            Type of the items in the array of the first specified
 *            parameter, this type also specify the type of the arrays of
 *            the which will be returned by the returned {@link Iterable}
 * @param <E>
 *            Type of the second {@link Iterable} arrays. It may be the same
 *            type as the first {@link Iterable}, or array of any other
 *            subclass
 * @param firstIterable
 *            {@link Iterable}, the first {@link Iterable} which contains
 *            items
 * @param secondIterable
 *            {@link Iterable}, the second {@link Iterable} which contains
 *            items
 * @return {@link Iterable}, object which will return {@link Iterator}s
 *         which will iterate over the second {@link Iterable} parameter as
 *         many times as there are items in the first {@link Iterable}
 *         parameter. The returned arrays will contains every possible
 *         combination between items in the first {@link Iterable}, and the
 *         second {@link Iterable}. For example: if the first
 *         {@link Iterable} contains <code>{1, 2}</code> and the second
 *         {@link Iterable} contains <code>{'a', 'b', 'c'}</code> the
 *         returned {@link Iterable} object will dynamically combine the
 *         {@link Iterable}s and provide following combination in specified
 *         order:
 *         <code>{1, 'a'}, {1, 'b'}, {1, 'c'}, {2, 'a'}, {2, 'b'}, {2, 'c'}</code>
 */
public static <T, E extends T> Iterable<T[]> combineIterables(final Iterable<T[]> firstIterable,
        final Iterable<E[]> secondIterable) {
    return new Iterable<T[]>() {

        /**
         * {@inheritDoc}
         */
        public Iterator<T[]> iterator() {

            return new Iterator<T[]>() {
                private Iterator<T[]> firstArrayIterator = firstIterable.iterator();
                private final Iterator<E[]> secondArrayIterator = secondIterable.iterator();
                private T[] appendArray = secondArrayIterator.next();

                /**
                 * {@inheritDoc}
                 */
                public boolean hasNext() {
                    return firstArrayIterator.hasNext() || secondArrayIterator.hasNext();
                }

                /**
                 * {@inheritDoc}
                 */
                public T[] next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    if (!firstArrayIterator.hasNext()) {
                        firstArrayIterator = firstIterable.iterator();
                        appendArray = secondArrayIterator.next();
                    }
                    T[] streamsItem = firstArrayIterator.next();
                    @SuppressWarnings("unchecked")
                    T[] result = (T[]) Array.newInstance(streamsItem.getClass().getComponentType(),
                            streamsItem.length + appendArray.length);
                    System.arraycopy(streamsItem, 0, result, 0, streamsItem.length);
                    System.arraycopy(appendArray, 0, result, streamsItem.length, appendArray.length);
                    return result;
                }

                /**
                 * {@inheritDoc}
                 */
                public void remove() {
                    throw new UnsupportedOperationException();
                }

            };
        }
    };
}

From source file:Main.java

/**
 * Returns an iterator that iterates backwards over the given list.
 * The remove() method is not implemented and will throw UnsupportedOperationException.
 * The returned iterator does not support the remove() method.
 * /*w w  w  .  j  av  a2 s  .  c  o  m*/
 * @throws NullPointerException if list is {@code null}
 */
public static <T> Iterator<T> reverseIterator(final List<T> list) {
    return new Iterator<T>() {
        ListIterator<T> iterator = list.listIterator(list.size());

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

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

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove() not supported"); //$NON-NLS-1$
        }
    };
}

From source file:org.apache.solr.kelvin.QueryPerformerLoader.java

public Iterator<QueryPerformer> iterator() {
    return new Iterator<QueryPerformer>() {

        private Iterator<IConfigurable> i = resources.iterator();

        public boolean hasNext() {
            return i.hasNext();
        }// w w w. j a  va2 s.c om

        public QueryPerformer next() {
            return (QueryPerformer) i.next();
        }

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

        }

    };
}

From source file:org.apache.hadoop.hbase.regionserver.compactions.GaussianFileListGenerator.java

@Override
public Iterator<List<StoreFile>> iterator() {
    return new Iterator<List<StoreFile>>() {
        private GaussianRandomGenerator gen = new GaussianRandomGenerator(
                new MersenneTwister(random.nextInt()));
        private int count = 0;

        @Override/* w ww.j  a  v  a 2s.  c o  m*/
        public boolean hasNext() {
            return count < MAX_FILE_GEN_ITERS;
        }

        @Override
        public List<StoreFile> next() {
            count += 1;
            ArrayList<StoreFile> files = new ArrayList<StoreFile>(NUM_FILES_GEN);
            for (int i = 0; i < NUM_FILES_GEN; i++) {
                files.add(createMockStoreFile(
                        (int) Math.ceil(Math.max(0, gen.nextNormalizedDouble() * 32 + 32))));
            }

            return files;
        }

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

From source file:jetbrains.exodus.query.InMemoryHeapSortIterable.java

@Override
public Iterator<Entity> iterator() {
    return new Iterator<Entity>() {
        private int i;
        private int size;
        private List<Entity> heap;

        @Override/* www  .  jav  a2 s. c  om*/
        public boolean hasNext() {
            if (heap == null) {
                init();
            }
            return i < size;
        }

        @Override
        public Entity next() {
            if (heap == null) {
                init();
            }
            if (i >= size) {
                throw new NoSuchElementException();
            }
            ++i;
            siftDown(0);
            final Entity current = heap.get(0);
            heap.set(0, heap.remove(size - 1));
            return current;
        }

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

        private void init() {
            heap = new ArrayList<>();
            for (final Entity entity : source) {
                heap.add(entity);
            }
            size = heap.size();
            if (size > 16) {
                if (log.isTraceEnabled()) {
                    log.trace("HeapSort called, size = " + size, new Exception());
                }
            }
            for (int i = (size - 2) / 2; i > 0; i -= 1) {
                siftDown(i);
            }
        }

        private void siftDown(int i) {
            final int count = heap.size();
            while (true) {
                int j = (i * 2) + 1;
                if (j >= count) {
                    break;
                }
                Entity child = heap.get(j);
                if (j < count - 1) {
                    final Entity rightChild = heap.get(j + 1);
                    if (comparator.compare(child, rightChild) >= 0) {
                        child = rightChild;
                        j += 1;
                    }
                }
                final Entity parent = heap.get(i);
                if (comparator.compare(child, parent) >= 0) {
                    break;
                }
                heap.set(i, child);
                heap.set(j, parent);
                i = j;
            }
        }
    };
}

From source file:org.apache.solr.kelvin.testcases.SimpleTestCase.java

public Iterator<Properties> getQueryParameterIterator() {

    return new Iterator<Properties>() {
        private Iterator<String> it = queries.iterator();

        public boolean hasNext() {
            return it.hasNext();
        }//from  w w  w.j  a va2 s  . c o m

        public Properties next() {
            Properties ret = new Properties();
            ret.put("q", it.next());
            return ret;
        }

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