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

public static <T> Iterator<T> getReverseIterator(final ListIterator<T> iter) {
    return new Iterator<T>() {
        public boolean hasNext() {
            return iter.hasPrevious();
        }/*  w  ww  . j  a v  a 2  s  . c  om*/

        public T next() {
            return iter.previous();
        }

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

From source file:org.mule.module.fws.api.PaginatedIterable.java

public Iterator<T> iterator() {
    final Page initialPageInfo = firstPage();
    return new Iterator<T>() {
        private Page currentList = initialPageInfo;
        private Iterator<T> currentIter = pageIterator(initialPageInfo);

        public boolean hasNext() {
            updateIter();/* w w  w  .j  a  va  2 s  .c  om*/
            return currentIter.hasNext();
        }

        public T next() {
            updateIter();
            return currentIter.next();
        }

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

        private void updateIter() {
            if (!currentIter.hasNext() && hasNextPage(currentList)) {
                currentList = nextPage(currentList);
                currentIter = pageIterator(currentList);
            }
        }
    };
}

From source file:Main.java

public static <S, T> Iterator<T> mapIterator(final Iterator<? extends S> delegate,
        final Function<? super S, ? extends T> mapping) {
    return new Iterator<T>() {
        @Override/*from ww w  . j a v  a2  s  .  c  om*/
        public boolean hasNext() {
            return delegate.hasNext();
        }

        @Override
        public T next() {
            return mapping.apply(delegate.next());
        }
    };
}

From source file:Main.java

/**
 * Returns an iterator that returns up to max number of elements from the
 * Collection/*from ww w  .  j av a 2 s .  com*/
 */
private static <T> Iterator<T> iterator(final Collection<T> c, final int count) {
    return new Iterator<T>() {

        private final Iterator<T> it = c.iterator();

        private int item = 0;

        public boolean hasNext() {
            if (item >= count) {
                return false;
            }
            return it.hasNext();
        }

        public T next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            item++;
            return it.next();
        }

        public void remove() {
            it.remove();
            item--;
        }
    };
}

From source file:Main.java

/**
 * Wraps an {@link Enumeration} as an instance of {@link Iterable} so that
 * it can be passed into a "for each" logical construct.
 * //ww  w  .j  a  v  a  2 s  . c om
 * @param <T>
 * @param enumeration
 * @return An Iterable representation of the enumeration parameter.
 */
public static <T> Iterable<T> toIterable(final Enumeration<T> enumeration) {

    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return new Iterator<T>() {

                public boolean hasNext() {
                    return enumeration.hasMoreElements();
                }

                public T next() {
                    return enumeration.nextElement();
                }

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

        }
    };

}

From source file:Main.java

public Iterator<E> iterator() {
    final Iterator<E> i = internalSet.iterator();
    return new Iterator<E>() {
        public boolean hasNext() {
            return i.hasNext();
        }//from   ww  w  .  ja v a  2s .c  om

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

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

From source file:com.trenako.utility.Utils.java

/**
 * Returns a reverse version for the provided {@code List}.
 *
 * @param list the original list/*from  w ww.  j  a  va 2s .  c  o m*/
 * @return the reverse iterable
 */
public static <E> Iterable<E> reverseIterable(final List<E> list) {
    return new Iterable<E>() {
        @Override
        public Iterator<E> iterator() {
            return new Iterator<E>() {

                private int pos = list.size() - 1;

                @Override
                public boolean hasNext() {
                    return pos >= 0;
                }

                @Override
                public E next() {
                    return list.get(pos--);
                }

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

            };
        }
    };
}

From source file:com.examples.with.different.packagename.ClassHierarchyIncludingInterfaces.java

public static Iterable<Class<?>> hierarchy(final Class<?> type, final Interfaces interfacesBehavior) {
    final Iterable<Class<?>> classes = new Iterable<Class<?>>() {

        @Override/*from  ww w  .  jav  a 2s.co m*/
        public Iterator<Class<?>> iterator() {
            final MutableObject<Class<?>> next = new MutableObject<Class<?>>(type);
            return new Iterator<Class<?>>() {

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

                @Override
                public Class<?> next() {
                    final Class<?> result = next.getValue();
                    next.setValue(result.getSuperclass());
                    return result;
                }

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

            };
        }

    };
    if (interfacesBehavior != Interfaces.INCLUDE) {
        return classes;
    }
    return new Iterable<Class<?>>() {

        @Override
        public Iterator<Class<?>> iterator() {
            final Set<Class<?>> seenInterfaces = new HashSet<Class<?>>();
            final Iterator<Class<?>> wrapped = classes.iterator();

            return new Iterator<Class<?>>() {
                Iterator<Class<?>> interfaces = Collections.<Class<?>>emptySet().iterator();

                @Override
                public boolean hasNext() {
                    return interfaces.hasNext() || wrapped.hasNext();
                }

                @Override
                public Class<?> next() {
                    if (interfaces.hasNext()) {
                        final Class<?> nextInterface = interfaces.next();
                        seenInterfaces.add(nextInterface);
                        return nextInterface;
                    }
                    final Class<?> nextSuperclass = wrapped.next();
                    final Set<Class<?>> currentInterfaces = new LinkedHashSet<Class<?>>();
                    walkInterfaces(currentInterfaces, nextSuperclass);
                    interfaces = currentInterfaces.iterator();
                    return nextSuperclass;
                }

                private void walkInterfaces(final Set<Class<?>> addTo, final Class<?> c) {
                    for (final Class<?> iface : c.getInterfaces()) {
                        if (!seenInterfaces.contains(iface)) {
                            addTo.add(iface);
                        }
                        walkInterfaces(addTo, iface);
                    }
                }

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

            };
        }
    };
}

From source file:IndexedSet.java

public Iterator<T> iterator() {
    return new Iterator<T>() {
        Iterator<T> realIterator = list.iterator();
        T current = null;/*w  w w.  j a  v  a  2s.co m*/

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

        public T next() {
            T next = realIterator.next();
            current = next;
            return next;
        }

        public void remove() {
            realIterator.remove();
            set.remove(current);
            current = null;
        }
    };
}

From source file:com.wrmsr.kleist.util.Itertools.java

public static <L, R> Iterator<Pair<L, R>> zip(Iterator<L> left, Iterator<R> right) {
    return new Iterator<Pair<L, R>>() {
        @Override//from  w w  w. ja  v a  2  s . c o m
        public boolean hasNext() {
            return left.hasNext() && right.hasNext();
        }

        @Override
        public Pair<L, R> next() {
            return ImmutablePair.of(left.next(), right.next());
        }
    };
}