package hudson.util;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.ListIterator;

/**
 * Varios {@link Iterator} implementations.
 *
 * @author Kohsuke Kawaguchi
 */
public class Iterators {
    /**
     * Returns the empty iterator.
     */
    public static <T> Iterator<T> empty() {
        return Collections.<T>emptyList().iterator();
    }

    /**
     * Produces {A,B,C,D,E,F} from {{A,B},{C},{},{D,E,F}}.
     */
    public static abstract class FlattenIterator<U,T> implements Iterator<U> {
        private final Iterator<? extends T> core;
        private Iterator<U> cur;

        protected FlattenIterator(Iterator<? extends T> core) {
            this.core = core;
            cur = Collections.<U>emptyList().iterator();
        }

        protected FlattenIterator(Iterable<? extends T> core) {
            this(core.iterator());
        }

        protected abstract Iterator<U> expand(T t);

        public boolean hasNext() {
            while(!cur.hasNext()) {
                if(!core.hasNext())
                    return false;
                cur = expand(core.next());
            }
            return true;
        }

        public U next() {
            if(!hasNext())  throw new NoSuchElementException();
            return cur.next();
        }

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

    /**
     * Returns the {@link Iterable} that lists items in the reverse order.
     */
    public static <T> Iterable<T> reverse(final List<T> lst) {
        return new Iterable<T>() {
            public Iterator<T> iterator() {
                final ListIterator<T> itr = lst.listIterator(lst.size());
                return new Iterator<T>() {
                    public boolean hasNext() {
                        return itr.hasPrevious();
                    }

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

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

@@ -166,16 +166,47 @@
 terator;
+%0Aimport java.util.AbstractList;
 %0A%0A/**%0A *
@@ -2226,11 +2226,1056 @@
 %7D;%0A    %7D
+%0A%0A    /**%0A     * Returns a list that represents %5Bstart,end).%0A     *%0A     * For example sequence(1,5,1)=%7B1,2,3,4%7D, and sequence(7,1,-2)=%7B7.5,3%7D%0A     */%0A    public static List%3CInteger%3E sequence(final int start, int end, final int step) %7B%0A%0A        final int size = (end-start)/step;%0A        if(size%3C0)  throw new IllegalArgumentException(%22List size is negative%22);%0A%0A        return new AbstractList%3CInteger%3E() %7B%0A            public Integer get(int index) %7B%0A                return start+index*step;%0A            %7D%0A%0A            public int size() %7B%0A                return size;%0A            %7D%0A        %7D;%0A    %7D%0A%0A    public static List%3CInteger%3E sequence(int start, int end) %7B%0A        return sequence(start,end,1);%0A    %7D%0A%0A    /**%0A     * The short cut for %7B@code reverse(sequence(start,end,step))%7D.%0A     */%0A    public static List%3CInteger%3E reverseSequence(int start, int end, int step) %7B%0A        return sequence(end-1,start-1,-step);%0A    %7D%0A%0A    public static List%3CInteger%3E reverseSequence(int start, int end) %7B%0A        return reverseSequence(start,end,1);%0A    %7D
 %0A%7D%0A

adding a sequence list generator.