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:org.apache.chemistry.opencmis.jcr.util.Iterables.java

public static <T> Iterable<T> concat(final Iterable<T> it1, final Iterable<T> it2) {
    return new Iterable<T>() {
        @SuppressWarnings("unchecked")
        public Iterator<T> iterator() {
            return new IteratorChain(it1.iterator(), it2.iterator());
        }// www .ja  va 2  s.  c om
    };
}

From source file:Main.java

/**
 * Creates an Iterable instance that just returns the given Iterator from its iterator() method.
 * //from   w w  w.  j  a  v a  2s. co m
 * This is useful for using an iterator in a foreach loop directly.
 * 
 * <p>
 * Example use:
 * <pre>
 *     for (Object o : iterable(iterator)) { ... }
 * </pre>
 * 
 * @throws NullPointerException if list is {@code null}
 */
public static <T> Iterable<T> iterable(final Iterator<T> iter) {
    if (iter == null)
        throw new NullPointerException("iter parameter is null"); //$NON-NLS-1$

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

From source file:org.sybila.parasim.util.Iterables.java

public static <T> Iterable<T> concat(final List<Iterable<T>> iterables) {
    Validate.notNull(iterables);/*from www . j  av a  2  s.  c  o m*/
    if (iterables.isEmpty()) {
        throw new IllegalArgumentException("At least one iterable has to be given.");
    }
    return new Iterable<T>() {
        @Override
        public Iterator<T> iterator() {
            return new Iterator<T>() {

                private int currentIndex = 0;

                private Iterator<T> currentIterator = iterables.get(0).iterator();

                @Override
                public boolean hasNext() {
                    if (currentIterator.hasNext()) {
                        return true;
                    }
                    while (currentIndex < iterables.size() - 1) {
                        currentIndex++;
                        currentIterator = iterables.get(currentIndex).iterator();
                        if (currentIterator.hasNext()) {
                            return true;
                        }
                    }
                    return false;
                }

                @Override
                public T next() {
                    if (hasNext()) {
                        return currentIterator.next();
                    }
                    return null;
                }

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

From source file:org.apache.accumulo.core.util.format.FormatterFactory.java

@SuppressWarnings("unchecked")
public static Formatter getDefaultFormatter(final Iterable<Entry<Key, Value>> scanner,
        boolean printTimestamps) {
    return new FormatterShim(cloudbase.core.util.format.FormatterFactory
            .getDefaultFormatter(new Iterable<Entry<cloudbase.core.data.Key, cloudbase.core.data.Value>>() {
                @Override//  ww w  . jav a2  s.c  o m
                public Iterator<Entry<cloudbase.core.data.Key, cloudbase.core.data.Value>> iterator() {
                    return IteratorUtils.transformedIterator(scanner.iterator(), new Transformer() {
                        @Override
                        public Entry<cloudbase.core.data.Key, cloudbase.core.data.Value> transform(
                                Object input) {
                            Entry<Key, Value> entry = (Entry<Key, Value>) input;
                            return new AbstractMap.SimpleEntry<cloudbase.core.data.Key, cloudbase.core.data.Value>(
                                    entry.getKey().impl, entry.getValue().impl);
                        }
                    });
                }
            }, printTimestamps));
}

From source file:org.apache.chemistry.opencmis.jcr.util.Iterables.java

public static <T> Iterable<T> singleton(final T element) {
    return new Iterable<T>() {
        @SuppressWarnings("unchecked")
        public Iterator<T> iterator() {
            return new SingletonIterator(element);
        }/*from ww  w.  jav  a2s  .c o  m*/
    };
}

From source file:com.parse.ParseJSONUtils.java

/**
 * A helper for nonugly iterating over JSONObject keys.
 */// www . ja v  a  2 s. co m
public static Iterable<String> keys(JSONObject object) {
    final JSONObject finalObject = object;
    return new Iterable<String>() {
        @Override
        public Iterator<String> iterator() {
            return finalObject.keys();
        }
    };
}

From source file:com.bitranger.parknshop.common.recommend.collections.CollectionUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <E> Iterable<E> fast(final Iterable<E> iter) {
    if (iter instanceof FastIterable) {
        return new Iterable<E>() {
            @Override//w  w  w .  j  a  v a  2 s. c om
            public Iterator<E> iterator() {
                return ((FastIterable) iter).fastIterator();
            }
        };
    } else if (iter instanceof Cursor) {
        throw new IllegalArgumentException();
    } else {
        Optional<Method> fastMethod = fastIteratorMethods.getUnchecked(iter.getClass());
        if (fastMethod.isPresent()) {
            final Method method = fastMethod.get();
            return new Iterable<E>() {
                @Override
                public Iterator<E> iterator() {
                    try {
                        return (Iterator<E>) method.invoke(iter);
                    } catch (IllegalAccessException e) {
                        return iter.iterator();
                    } catch (InvocationTargetException e) {
                        throw Throwables.propagate(e.getCause());
                    }
                }
            };
        } else {
            return iter;
        }
    }
}

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  w w  w  .  j  a v a2  s  .  com
        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:Main.java

/**
 * Wraps an {@link Enumeration} as an instance of {@link Iterable} so that
 * it can be passed into a "for each" logical construct.
 * /* w  w  w  .j a  v  a2 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:com.thoughtworks.studios.journey.utils.IterableUtils.java

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