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:com.trenako.utility.Utils.java

/**
 * Returns a reverse version for the provided {@code List}.
 *
 * @param list the original list//from   ww  w. ja va2s. c om
 * @return the reverse iterable
 */
public static <E> Iterable<E> reverseIterable(final List<E> list) {
    return new Iterable<E>() {
        @Override
        public Iterator<E> iterator() {
            return new Iterator<E>() {

                private int pos = list.size() - 1;

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

                @Override
                public E next() {
                    return list.get(pos--);
                }

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

            };
        }
    };
}

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

public static <T> Iterable<T> empty() {
    return new Iterable<T>() {
        @SuppressWarnings("unchecked")
        public Iterator<T> iterator() {
            return EmptyIterator.INSTANCE;
        }/*w  w  w . j a v  a  2 s.com*/
    };
}

From source file:edu.byu.nlp.util.Iterables2.java

public static <E> Iterable<Enumeration<E>> enumerate(final Iterable<E> iterable) {
    return new Iterable<Enumeration<E>>() {

        @Override//  w  w w . j av  a2  s .  c o m
        public Iterator<Enumeration<E>> iterator() {
            return Iterators2.enumerate(iterable.iterator());
        }

    };
}

From source file:org.cmg.ml.sam.core.sim.SimulationSeries.java

public Iterable<SimulationData> getSeries() {
    return new Iterable<SimulationData>() {

        @Override/*from  w  ww.j  ava2 s  .c  om*/
        public Iterator<SimulationData> iterator() {
            return new Iterator<SimulationData>() {

                private int counter;

                @Override
                public boolean hasNext() {
                    return counter < data.length;
                }

                @Override
                public SimulationData next() {
                    SimulationData toReturn = null;
                    if (counter < data.length) {
                        toReturn = new SimulationData(dt * counter, data[counter].getMean(),
                                data[counter].getStandardDeviation() / Math.sqrt(iterations));
                        counter++;
                    }
                    return toReturn;
                }

                @Override
                public void remove() {
                }

            };
        }

    };
}

From source file:Main.java

/**
 * Create new {@link Iterable} object which combine the first and the second
 * parameters./*from w w  w  . j a v  a  2s .com*/
 * 
 * @param <T>
 *            Type of the items in the array of the first specified
 *            parameter, this type also specify the type of the arrays of
 *            the which will be returned by the returned {@link Iterable}
 * @param <E>
 *            Type of the second {@link Iterable} arrays. It may be the same
 *            type as the first {@link Iterable}, or array of any other
 *            subclass
 * @param firstIterable
 *            {@link Iterable}, the first {@link Iterable} which contains
 *            items
 * @param secondIterable
 *            {@link Iterable}, the second {@link Iterable} which contains
 *            items
 * @return {@link Iterable}, object which will return {@link Iterator}s
 *         which will iterate over the second {@link Iterable} parameter as
 *         many times as there are items in the first {@link Iterable}
 *         parameter. The returned arrays will contains every possible
 *         combination between items in the first {@link Iterable}, and the
 *         second {@link Iterable}. For example: if the first
 *         {@link Iterable} contains <code>{1, 2}</code> and the second
 *         {@link Iterable} contains <code>{'a', 'b', 'c'}</code> the
 *         returned {@link Iterable} object will dynamically combine the
 *         {@link Iterable}s and provide following combination in specified
 *         order:
 *         <code>{1, 'a'}, {1, 'b'}, {1, 'c'}, {2, 'a'}, {2, 'b'}, {2, 'c'}</code>
 */
public static <T, E extends T> Iterable<T[]> combineIterables(final Iterable<T[]> firstIterable,
        final Iterable<E[]> secondIterable) {
    return new Iterable<T[]>() {

        /**
         * {@inheritDoc}
         */
        public Iterator<T[]> iterator() {

            return new Iterator<T[]>() {
                private Iterator<T[]> firstArrayIterator = firstIterable.iterator();
                private final Iterator<E[]> secondArrayIterator = secondIterable.iterator();
                private T[] appendArray = secondArrayIterator.next();

                /**
                 * {@inheritDoc}
                 */
                public boolean hasNext() {
                    return firstArrayIterator.hasNext() || secondArrayIterator.hasNext();
                }

                /**
                 * {@inheritDoc}
                 */
                public T[] next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    if (!firstArrayIterator.hasNext()) {
                        firstArrayIterator = firstIterable.iterator();
                        appendArray = secondArrayIterator.next();
                    }
                    T[] streamsItem = firstArrayIterator.next();
                    @SuppressWarnings("unchecked")
                    T[] result = (T[]) Array.newInstance(streamsItem.getClass().getComponentType(),
                            streamsItem.length + appendArray.length);
                    System.arraycopy(streamsItem, 0, result, 0, streamsItem.length);
                    System.arraycopy(appendArray, 0, result, streamsItem.length, appendArray.length);
                    return result;
                }

                /**
                 * {@inheritDoc}
                 */
                public void remove() {
                    throw new UnsupportedOperationException();
                }

            };
        }
    };
}

From source file:Main.java

/**
 * Concatenates multiple iterables into a single iterable. The iterator exposed by the returned
 * iterable does not support {@link Iterator#remove()} even if the input iterables do.
 *
 * @throws NullPointerException if {@code iterables} or any of its elements are {@code null}
 *//*from w  w w.j a v a 2  s .  co  m*/
public static <T> Iterable<T> concat(List<Iterable<T>> iterables) {
    for (Iterable<T> iterable : iterables) {
        Objects.requireNonNull(iterable);
    }
    return new Iterable<T>() {
        @Override
        public Iterator<T> iterator() {
            if (iterables.size() == 0) {
                return Collections.emptyIterator();
            }
            return new Iterator<T>() {
                Iterator<Iterable<T>> cursor = iterables.iterator();
                Iterator<T> currentIterator = cursor.next().iterator();

                private void advance() {
                    while (!currentIterator.hasNext() && cursor.hasNext()) {
                        currentIterator = cursor.next().iterator();
                    }
                }

                @Override
                public boolean hasNext() {
                    advance();
                    return currentIterator.hasNext();
                }

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

    };
}

From source file:com.bitranger.parknshop.common.recommend.vectors.similarity.Vectors.java

public static Iterable<ImmutablePair<VectorEntry, VectorEntry>> intersect(final ArrayList<Long> v1,
        final ArrayList<Long> v2) {
    return new Iterable<ImmutablePair<VectorEntry, VectorEntry>>() {
        @Override//w w  w. jav a  2s.com
        public Iterator<ImmutablePair<VectorEntry, VectorEntry>> iterator() {
            return Iterators.transform(new FastIntersectIterImpl(v1, v2), IMMUTABLE_PAIR_COPY);
        }
    };
}

From source file:com.adaptris.jdbc.JdbcResultSetImpl.java

@Override
public Iterable<JdbcResultRow> getRows() {
    return new Iterable<JdbcResultRow>() {

        @Override/*from  w w w.  j  ava2  s . com*/
        public Iterator<JdbcResultRow> iterator() {
            try {
                switch (resultSet.getType()) {
                case ResultSet.TYPE_FORWARD_ONLY:
                    return new ForwardOnlyResultSetIterator(resultSet);

                case ResultSet.TYPE_SCROLL_SENSITIVE:
                case ResultSet.TYPE_SCROLL_INSENSITIVE:
                    return new ScrollableResultSetIterator(resultSet);

                default:
                    throw new RuntimeException("ResultSet was of unknown type");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:com.kylinolap.cube.invertedindex.InvertedIndexCLI.java

public static Iterable<Pair<ImmutableBytesWritable, ImmutableBytesWritable>> readSequenceKVs(
        Configuration hconf, String path) throws IOException {
    final Reader reader = new Reader(hconf, SequenceFile.Reader.file(new Path(path)));
    return new Iterable<Pair<ImmutableBytesWritable, ImmutableBytesWritable>>() {
        @Override//from w  w  w .  j  a va2  s.c  o m
        public Iterator<Pair<ImmutableBytesWritable, ImmutableBytesWritable>> iterator() {
            return new Iterator<Pair<ImmutableBytesWritable, ImmutableBytesWritable>>() {
                ImmutableBytesWritable k = new ImmutableBytesWritable();
                ImmutableBytesWritable v = new ImmutableBytesWritable();
                Pair<ImmutableBytesWritable, ImmutableBytesWritable> pair = new Pair<ImmutableBytesWritable, ImmutableBytesWritable>(
                        k, v);

                @Override
                public boolean hasNext() {
                    boolean hasNext = false;
                    try {
                        hasNext = reader.next(k, v);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        if (hasNext == false) {
                            IOUtils.closeQuietly(reader);
                        }
                    }
                    return hasNext;
                }

                @Override
                public Pair<ImmutableBytesWritable, ImmutableBytesWritable> next() {
                    return pair;
                }

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

From source file:com.github.jferard.pgloaderutils.loader.CSVRecordCleanerExample.java

@Override
public Iterable<String> cleanRecord(final CSVRecord record) {
    return new Iterable<String>() {
        @Override//from   w  w  w . j  a  v  a 2 s .  c  o  m
        public Iterator<String> iterator() {
            return new Iterator<String>() {
                private int i = 0;

                @Override
                public boolean hasNext() {
                    return this.i < record.size();
                }

                @Override
                public String next() {
                    String s = record.get(this.i);
                    if (this.i == 11 || this.i == 12 || this.i == 16) // numbers
                        s = s.replaceAll(",", "."); // from continental to US

                    this.i++;
                    return s;
                }

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