List of usage examples for java.lang Iterable iterator
Iterator<T> iterator();
From source file:com.cmsz.cloudplatform.utils.StringUtils.java
public static String join(Iterable<? extends Object> iterable, String delim) { StringBuilder sb = new StringBuilder(); if (iterable != null) { Iterator<? extends Object> iter = iterable.iterator(); if (iter.hasNext()) { Object next = iter.next(); sb.append(next.toString());//from w ww . j a va 2s. c om } while (iter.hasNext()) { Object next = iter.next(); sb.append(delim + next.toString()); } } return sb.toString(); }
From source file:com.wrmsr.kleist.util.Itertools.java
public static <T> Iterable<EnumeratedElement<T>> enumerate(Iterable<T> iterable) { return new Iterable<EnumeratedElement<T>>() { @Override/*from ww w. ja v a 2 s . c o m*/ public Iterator<EnumeratedElement<T>> iterator() { return enumerate(iterable.iterator()); } }; }
From source file:Main.java
static <T> Iterator<T> cycle(final Iterable<T> iterable) { return new Iterator<T>() { Iterator<T> iterator = Collections.<T>emptySet().iterator(); @Override//from w ww . j a va 2 s .co m public boolean hasNext() { return true; } @Override public T next() { if (!iterator.hasNext()) { iterator = iterable.iterator(); } return iterator.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
From source file:com.emc.atmos.api.RestUtil.java
public static String join(Iterable<?> list, String delimiter) { if (list == null) return null; StringBuilder builder = new StringBuilder(); for (Iterator<?> i = list.iterator(); i.hasNext();) { Object value = i.next();/*w ww. ja v a 2s . c o m*/ builder.append(value); if (i.hasNext()) builder.append(delimiter); } return builder.toString(); }
From source file:Main.java
/** * Creates a string with {@code toString()} of each object with the given separator. * * <pre>/*w w w . j a va 2 s . co m*/ * expect: * join(",", ["a"]) == "a" * join(",", ["a", "b", "c"]) == "a,b,c" * join(",", []) == "" * </pre> * * The {@code separator} must not be null and {@code objects} must not be null. * * @param separator The string by which to join each string representation * @param objects The objects to join the string representations of * @return The joined string */ public static String join(String separator, Iterable<?> objects) { if (separator == null) { throw new NullPointerException("The 'separator' cannot be null"); } if (objects == null) { throw new NullPointerException("The 'objects' cannot be null"); } StringBuilder string = new StringBuilder(); Iterator<?> iterator = objects.iterator(); if (iterator.hasNext()) { string.append(iterator.next().toString()); while (iterator.hasNext()) { string.append(separator); string.append(iterator.next().toString()); } } return string.toString(); }
From source file:Main.java
/** * Removes and returns the indexed value into the {@code Iterable}. It * first checks to see if the {@code Iterable} is a {@code List}, and if so * calls the remove method. Otherwise, it walks the {@code Iterable} to * get to the element and remove it. This only works on {@code Iterable}s * that are {@code List}s or whose {@code Iterator} implements the optional * {@code remove} method./*w w w. j av a 2 s . co m*/ * * @param <DataType> * The type of data. * @param iterable * The iterable to remove the value from. * @param index * The 0-based index to remove from the iterable. * @return * The value removed from the given index in the iterable. * @throws IndexOutOfBoundsException * If the index is less than zero or greater than or equal to the * number of elements in the iterable. * @throws UnsupportedOperationException * If the iterable does not support remove. */ public static <DataType> DataType removeElement(final Iterable<DataType> iterable, int index) { if (iterable instanceof List<?>) { return ((List<DataType>) iterable).remove(index); } else { if (index < 0) { // Bad index. throw new IndexOutOfBoundsException("index must be >= 0"); } Iterator<DataType> iterator = iterable.iterator(); while (iterator.hasNext()) { DataType value = iterator.next(); if (index == 0) { iterator.remove(); return value; } index--; } // Bad index. throw new IndexOutOfBoundsException("index >= iterable size"); } }
From source file:com.github.tomakehurst.wiremock.testsupport.WireMatchers.java
public static <T> Matcher<Iterable<T>> hasExactly(final Matcher<T>... items) { return new TypeSafeMatcher<Iterable<T>>() { @Override/*from w w w .ja v a 2s.c om*/ public void describeTo(Description desc) { desc.appendText("Collection must match exactly"); } @Override public boolean matchesSafely(Iterable<T> actual) { Iterator<T> actualIter = actual.iterator(); for (Matcher<T> matcher : items) { if (!matcher.matches(actualIter.next())) { return false; } } return !actualIter.hasNext(); } }; }
From source file:Main.java
/** * Create new {@link Iterable} object which combine the first and the second * parameters./*from w w w.jav a 2s . c o m*/ * * @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:com.swtxml.util.lang.Filters.java
public static <A> IFilter<A> and(final Iterable<IFilter<A>> filters) { return new IFilter<A>() { public boolean match(A obj) { for (IFilter<A> filter : filters) { if (!filter.match(obj)) { return false; }//w w w . j a va 2s .com } return true; } @Override public String toString() { return "(" + StringUtils.join(filters.iterator(), " and ") + ")"; } }; }
From source file:lux.xqts.TestCase.java
public static String resultToString(Iterable<?> results) throws XPathException { Iterator<?> iterator = results.iterator(); if (!iterator.hasNext()) { return ""; }// www.jav a2 s.c o m Object result = iterator.next(); StringBuilder buf = new StringBuilder(resultToString(result)); boolean lastNode = result instanceof XdmNode; while (iterator.hasNext()) { result = iterator.next(); if (!(result instanceof XdmNode) && !lastNode) { buf.append(' '); } buf.append(resultToString(result)); lastNode = result instanceof XdmNode; } return buf.toString(); }