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

/**
 * Returns an iterator over the values referenced by the elements of {@code
 * iterable}.//from  w  w  w.jav  a  2s . c o m
 *
 * @param trim true to remove reference objects from the iterable after
 *     their referenced values have been cleared.
 */
public static <T> Iterable<T> dereferenceIterable(final Iterable<? extends Reference<T>> iterable,
        final boolean trim) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                private final Iterator<? extends Reference<T>> delegate = iterable.iterator();
                private boolean removeIsOkay;
                private T next;

                private void computeNext() {
                    removeIsOkay = false;
                    while (next == null && delegate.hasNext()) {
                        next = delegate.next().get();
                        if (trim && next == null) {
                            delegate.remove();
                        }
                    }
                }

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

                @Override
                public T next() {
                    if (!hasNext()) {
                        throw new IllegalStateException();
                    }
                    T result = next;
                    removeIsOkay = true;
                    next = null;
                    return result;
                }

                public void remove() {
                    if (!removeIsOkay) {
                        throw new IllegalStateException();
                    }
                    delegate.remove();
                }
            };
        }
    };
}

From source file:Main.java

/**
 * Zips the specified stream with its indices.
 *//*from   w  w w .j  a va 2  s . c o  m*/
public static <T> Stream<Map.Entry<Integer, T>> zipWithIndex(Stream<? extends T> stream, int startIndex) {
    return iterate(new Iterator<Map.Entry<Integer, T>>() {
        private final Iterator<? extends T> streamIterator = stream.iterator();
        private int index = startIndex;

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

        @Override
        public Map.Entry<Integer, T> next() {
            return new AbstractMap.SimpleImmutableEntry<>(index++, streamIterator.next());
        }
    });
}

From source file:Main.java

static Iterable<JavaSourceFromString> getJavaSourceFromString(String code) {
    final JavaSourceFromString jsfs = new JavaSourceFromString("code", code);
    return new Iterable<JavaSourceFromString>() {
        public Iterator<JavaSourceFromString> iterator() {
            return new Iterator<JavaSourceFromString>() {
                boolean isNext = true;

                public boolean hasNext() {
                    return isNext;
                }/*from ww w  .  jav  a2 s.  c  o  m*/

                public JavaSourceFromString next() {
                    if (!isNext)
                        throw new NoSuchElementException();
                    isNext = false;
                    return jsfs;
                }

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

From source file:CompileString.java

static Iterable<JavaSourceFromString> getJavaSourceFromString(String code) {
    final JavaSourceFromString jsfs;
    jsfs = new JavaSourceFromString("code", code);
    return new Iterable<JavaSourceFromString>() {
        public Iterator<JavaSourceFromString> iterator() {
            return new Iterator<JavaSourceFromString>() {
                boolean isNext = true;

                public boolean hasNext() {
                    return isNext;
                }/*from  w w  w . ja  v a2 s.  c o  m*/

                public JavaSourceFromString next() {
                    if (!isNext)
                        throw new NoSuchElementException();
                    isNext = false;
                    return jsfs;
                }

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

From source file:Main.java

public static Iterable<Node> iterateOverChildren(final Node element) {
    return new Iterable<Node>() {

        NodeList childNodes = element.getChildNodes();
        int itemIndex = 0;
        int nItems = childNodes.getLength();

        @Override//  ww  w .j a va 2  s . c o  m
        public Iterator<Node> iterator() {
            return new Iterator<Node>() {

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

                @Override
                public Node next() {
                    return childNodes.item(itemIndex++);
                }

                @Override
                public boolean hasNext() {
                    return itemIndex < nItems;
                }
            };
        }
    };
}

From source file:Main.java

public static Collection<Node> createNodeCollection(final NodeList nodeList) {
    // http://www.java2s.com/Code/Java/XML/WrapNodeListtoCollection.htm
    // Written by Tomer Gabel under the Apache License Version 2.0
    return new Collection<Node>() {
        @Override/*from  w  w  w.  ja v a  2s. c  om*/
        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:Util.java

/**
 * Renvoie les lments enfants (uniquement de type ELEMENT)
 * //  www .j  a v a 2s.  c  om
 * @param e
 * @return
 */
public static Iterable<Element> getChildElements(final Element e) {
    return new Iterable<Element>() {
        public Iterator<Element> iterator() {
            final NodeList list = e.getChildNodes();
            int i = 0;
            for (; i < list.getLength(); i++) {
                if (list.item(i).getNodeType() == Node.ELEMENT_NODE)
                    break;
            }
            final int init = i;
            return new Iterator<Element>() {
                int cur = init;

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

                public Element next() {
                    Element item = (Element) list.item(cur);
                    for (cur++; cur < list.getLength(); cur++) {
                        if (list.item(cur).getNodeType() == Node.ELEMENT_NODE)
                            break;
                    }
                    return item;
                }

                public boolean hasNext() {
                    return cur < list.getLength();
                }

            };
        }
    };
}

From source file:IteratorUtils.java

public static Iterator oneElementUnmodifiableIterator(final Object elem) {
    return new Iterator() {
        boolean shot = false;

        public boolean hasNext() {
            return (!shot);
        }//ww  w .j  a  va  2s .  co m

        public Object next() {
            if (shot)
                throw new NoSuchElementException();
            else {
                shot = true;
                return elem;
            }
        }

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

From source file:Main.java

/**
 * Convenient method to chain iterables together.
 * @param iterables to chain/*from ww w  . j  a va  2s .c om*/
 * @return chained iterator
 */
public static <T> Iterator chain(Iterator<T>... iterables) {
    List<Iterator<T>> iterableList = Arrays.asList(iterables);
    return new Iterator<T>() {
        @Override
        public boolean hasNext() {
            return iterableList.stream().anyMatch(Iterator::hasNext);
        }

        @Override
        public T next() {
            return iterableList.stream().filter(Iterator::hasNext).findFirst().map(Iterator::next).orElse(null);
        }
    };
}

From source file:CollectionUtilities.java

public static Iterator iteratorUnion(final Iterator[] iterators) {
    return new Iterator() {
        private int iteratorIndex = 0;
        private Iterator current = iterators.length > 0 ? iterators[0] : null;

        public boolean hasNext() {
            for (;;) {
                if (current == null) {
                    return false;
                }/*from w w w . ja va 2  s .  co m*/
                if (current.hasNext()) {
                    return true;
                }
                iteratorIndex++;
                current = iteratorIndex >= iterators.length ? null : iterators[iteratorIndex];
            }
        }

        public Object next() {
            for (;;) {
                if (this.current == null) {
                    throw new NoSuchElementException();
                }
                try {
                    return this.current.next();
                } catch (NoSuchElementException nse) {
                    this.iteratorIndex++;
                    this.current = this.iteratorIndex >= iterators.length ? null
                            : iterators[this.iteratorIndex];
                }
            }
        }

        public void remove() {
            if (this.current == null) {
                throw new NoSuchElementException();
            }
            this.current.remove();
        }
    };
}