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

@SuppressWarnings("unchecked")
public static <T> Iterable<T> concat(final Iterable<T>... collections) {
    return new Iterable<T>() {
        @Override/*from www .  j av a 2 s.  com*/
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                final Iterable<T>[] iterables = collections;
                int iteratorIdx = 0;
                Iterator<T> it = collections[0].iterator();

                private synchronized void advanceCursor() {
                    while (!it.hasNext() && iteratorIdx < iterables.length) {
                        iteratorIdx += 1;
                        if (iteratorIdx == iterables.length) {
                            it = new Iterator<T>() {
                                @Override
                                public boolean hasNext() {
                                    return false;
                                }

                                @Override
                                public T next() {
                                    throw new NoSuchElementException();
                                }

                                @Override
                                public void remove() {
                                    throw new RuntimeException();
                                }
                            };
                        } else {
                            it = iterables[iteratorIdx].iterator();
                        }
                    }
                }

                @Override
                public synchronized boolean hasNext() {
                    advanceCursor();
                    return iteratorIdx != iterables.length && it.hasNext();
                }

                @Override
                public synchronized T next() {
                    advanceCursor();
                    if (iteratorIdx == iterables.length)
                        throw new NoSuchElementException();
                    return it.next();
                }

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

From source file:Main.java

/**
 * Convience method for wrapping an iterator into an iterable.
 * This is used mainly for using of a foreach loop with an iterator
 * /*from ww w.  j ava  2  s  .  c  o m*/
 * @param it
 * @return
 */
public static <T> Iterable<T> getIterable(final Iterator<T> it) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return it;
        }
    };
}

From source file:Permutator.java

public static <T> Iterable<List<T>> permutations(final List<T> list) {
    return new Iterable<List<T>>() {
        public Iterator<List<T>> iterator() {
            return new Iterator<List<T>>() {
                private int current = 0;
                private final long length = factorial(list.size());

                public List<T> next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }//  w ww. ja v  a 2s .  co m
                    List<T> permutation = new ArrayList<T>(list);
                    int k = current;
                    for (int j = 2; j <= list.size(); j++) {
                        k /= j - 1;
                        Collections.swap(permutation, (k % j), j - 1);
                    }
                    current++;
                    return permutation;
                }

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

                public boolean hasNext() {
                    return current < length;
                }
            };
        }
    };
}

From source file:Main.java

public static Iterable<Node> iterate(final NodeList nodeList) {
    return new Iterable<Node>() {
        @Override/*  w w w.  ja  v a2  s.c  o m*/
        public Iterator<Node> iterator() {

            return new Iterator<Node>() {

                int index = 0;

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

                @Override
                public Node next() {
                    return nodeList.item(index++);
                }

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

From source file:Main.java

public static Iterable<String> iterate(Matcher matcher) {
    return new Iterable<String>() {
        @Override/*from www . ja  v a2 s  .  c  o m*/
        public Iterator<String> iterator() {
            return new Iterator<String>() {
                @Override
                public boolean hasNext() {
                    return matcher.find();
                }

                @Override
                public String next() {
                    return matcher.group();
                }
            };
        }

        @Override
        public void forEach(Consumer<? super String> action) {
            while (matcher.find()) {
                action.accept(matcher.group());
            }
        }
    };
}

From source file:Main.java

/**
 * Generate an instance of Iterable for an array.
 * This may be used in a foreach loop.//w ww .j  ava2s .co m
 * 
 * @param array
 * @return
 */
public static <T> Iterable<T> getIterable(final T[] array) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return getIterator(array);
        }
    };
}

From source file:Main.java

public static <T> Iterable<T> reverse(final List<T> list) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            final ListIterator<T> listIterator = list.listIterator(list.size());

            return new Iterator<T>() {
                public boolean hasNext() {
                    return listIterator.hasPrevious();
                }/*from w  w w.  j  ava2 s . c o  m*/

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

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

From source file:Main.java

public static <T> Iterable<T> iterable(final Enumeration<T> enumeration) {
    return new Iterable<T>() {
        @Override//from w  w  w .j a v a 2  s.  com
        public Iterator<T> iterator() {
            return org.springframework.util.CollectionUtils.toIterator(enumeration);
        }
    };
}

From source file:Main.java

/**
 * Gets the reverse iterable of an Iterable (i.e. a collection).
 * If this collection implements ListIterator, then this operation is far more efficent.
 * Otherwise, the iterable is iterated through, where the results are stored, before
 * returning a iterable that iterates through the resulting list.
 * //from w  w  w.  j ava2 s.c  o m
 * @param it
 * @return
 */
public static <T> Iterable<T> getReverse(Iterable<T> it) {
    if (it instanceof List) { //list takes up less computation and memory - use built in list iterator
        final ListIterator<T> iter = ((List<T>) it).listIterator(((List<T>) it).size());
        return new Iterable<T>() {
            public Iterator<T> iterator() {
                return getReverseIterator(iter);
            }
        };
    }

    int size = 10;
    if (it instanceof Collection) { //if not a list, but is a collection
        size = ((Collection<T>) it).size();
    }
    List<T> list = new Vector<>(size);
    for (T t : it) {
        list.add(t);
    }
    final ListIterator<T> iter = list.listIterator(list.size());

    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return getReverseIterator(iter);
        }
    };
}

From source file:Main.java

public static <T> Iterable<T> concat(final List<Iterable<? extends T>> iterables) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                Iterator<? extends T> iter;
                int pos = 0;

                public boolean hasNext() {
                    while (pos < iterables.size()) {
                        if (iter == null)
                            iter = iterables.get(pos).iterator();
                        if (iter.hasNext())
                            return true;
                        iter = null;//from   w w  w .j  a v  a  2  s  . com
                        pos++;
                    }
                    return false;
                }

                public T next() {
                    while (pos < iterables.size()) {
                        if (iter == null)
                            iter = iterables.get(pos).iterator();
                        if (iter.hasNext())
                            return iter.next();
                        iter = null;
                        pos++;
                    }
                    throw new NoSuchElementException();
                }

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