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:tpt.dbweb.cat.tools.Utility.java

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

        @Override// w  w  w .  j  a  v  a 2  s .c  o m
        public Iterator<T> iterator() {
            return iterator;
        }
    };
}

From source file:Main.java

public static <E> Iterable<E> iterable(final Iterator<E> iterator) {
    if (iterator == null) {
        throw new NullPointerException();
    }//  w w  w.  ja  va2  s.  com
    return new Iterable<E>() {
        public Iterator<E> iterator() {
            return iterator;
        }
    };
}

From source file:Util.java

/**
 * Renvoie les lments enfants (uniquement de type ELEMENT)
 * /*from   ww w. j a  va 2  s. c  o  m*/
 * @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:Main.java

public static Iterable<Element> childrenElementsOf(final Node parent) {
    return new Iterable<Element>() {

        public Iterator<Element> iterator() {
            return new ElementIterator();
        }/*from w w w  . java  2s .  c om*/

        class ElementIterator implements Iterator<Element> {
            private Node continueFrom;
            private Element curNode;

            ElementIterator() {
                continueFrom = parent.getFirstChild();
                fetch();
            }

            public boolean hasNext() {
                return curNode != null;
            }

            public Element next() {
                Element res = curNode;
                fetch();
                return res;
            }

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

            private void fetch() {
                while (!(continueFrom == null || continueFrom instanceof Element)) {
                    continueFrom = continueFrom.getNextSibling();
                }
                if (continueFrom != null) {
                    curNode = (Element) continueFrom;
                    continueFrom = curNode.getNextSibling();
                } else {
                    curNode = null;
                }
            }

        }

    };
}

From source file:Main.java

public static <T> Iterable<T> iterable(final Iterator<T> iterator) {
    return new Iterable<T>() {
        @Override/*from  ww w  .  j  a  v a  2 s .  c  om*/
        public Iterator<T> iterator() {
            return iterator;
        }
    };
}

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//from   w  ww. j  ava2 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 <T> Iterable<T> iterable(final Iterator<T> iterator) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return iterator;
        }/*from w  ww  .j a v a  2 s. c  om*/
    };
}

From source file:Main.java

/**
 * Returns an iterator over the values referenced by the elements of {@code
 * iterable}./*from   w  w  w .  j a va  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

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;
                }/*w w  w .  j a  v a 2s  .  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   ww w . java  2 s . c  om*/

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

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