Example usage for java.lang Iterable Iterable

List of usage examples for java.lang Iterable Iterable

Introduction

In this page you can find the example usage for java.lang Iterable Iterable.

Prototype

Iterable

Source Link

Usage

From source file:Main.java

public static <E> Iterable<E> asIterable(final Iterator<E> i) {
    return new Iterable<E>() {
        @Override// www.j a  v  a  2  s  . c o  m
        public Iterator<E> iterator() {
            return i;
        }
    };
}

From source file:Main.java

public static <T> Iterable<T> iterableOf(final Iterator<T> keys) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return keys;
        }/*from w w  w  . j ava  2s. com*/

    };
}

From source file:Main.java

public static <T> Iterable<T> makeEmptyIterable() {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                public boolean hasNext() {
                    return false;
                }/*  w ww  .  j  a v a 2 s .  com*/

                public T next() {
                    return null;
                }

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

From source file:Main.java

public static <T> Iterable<T> asIterable(final Iterator<T> iterator) {
    return new Iterable<T>() {
        boolean called = false;

        @Override/*  ww  w.j av  a2 s  .c om*/
        public Iterator<T> iterator() {
            if (called) {
                throw new IllegalArgumentException("wrapped iterable twice iterator call");
            }
            called = true;

            return iterator;
        }
    };
}

From source file:Main.java

public static <T> Iterable<T> asIterable(final T[] a) {
    return new Iterable<T>() {
        @Override//from w  w w . jav  a  2s . co m
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                int index = 0;

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

                @Override
                public T next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return a[index++];
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            };
        }
    };
}

From source file:Main.java

public static <T> Iterable<T> cycle(final Iterable<T> it) {
    return new Iterable<T>() {

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

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

                public boolean hasNext() {
                    return true; // forever cycles
                }/*from w  ww . j a  va2 s.c  om*/

                public T next() {
                    if (!nextCycle.hasNext()) {
                        nextCycle = it.iterator(); // create the next cycle
                    }
                    return nextCycle.next();
                }
            };
        }
    };
}

From source file:Main.java

public static <T> List<T> iteratorToList(final Iterator<T> iterator) {
    List<T> list = new ArrayList<T>();
    for (T element : new Iterable<T>() {
        public Iterator<T> iterator() {
            return iterator;
        }//from w  ww . j  av  a  2s . co  m
    }) {
        list.add(element);
    }
    return list;
}

From source file:Main.java

public static <T> Iterable<T> concat(Iterable<T> first, Iterable<T> second) {

    return new Iterable<T>() {

        final Iterator<T> it1 = first.iterator();
        final Iterator<T> it2 = second.iterator();

        @Override/*  w  w  w  .j  ava 2  s .  com*/
        public Iterator<T> iterator() {

            return new Iterator<T>() {

                Iterator<T> curr;

                @Override
                public boolean hasNext() {
                    if (it1.hasNext()) {
                        curr = it1;
                        return true;
                    } else if (it2.hasNext()) {
                        curr = it2;
                        return true;
                    }
                    return false;
                }

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

From source file:Main.java

public static <E> Iterable<E> reverse(final List<E> list) {
    return new Iterable<E>() {
        @Override//from  www . j  a  v  a 2s .c  o m
        public Iterator<E> iterator() {
            final ListIterator<E> it = list.listIterator(list.size());

            return new Iterator<E>() {
                @Override
                public boolean hasNext() {
                    return it.hasPrevious();
                }

                @Override
                public E next() {
                    return it.previous();
                }

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

From source file:Main.java

public static <T> Iterable<T> replicate(final T value, final int count) {
    return new Iterable<T>() {
        @Override// w w  w  .java 2  s  .  co  m
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                int remaining = count;

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

                @Override
                public T next() {
                    if (remaining-- <= 0)
                        throw new NoSuchElementException();
                    return value;
                }

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