| | |
| | | |
| | | package com.forgerock.opendj.util; |
| | | |
| | | import java.util.Collection; |
| | | import java.util.Iterator; |
| | | |
| | | import org.forgerock.opendj.ldap.Function; |
| | |
| | | this.a = a; |
| | | } |
| | | |
| | | @Override |
| | | public Iterator<M> iterator() { |
| | | return Iterators.arrayIterator(a); |
| | | } |
| | | } |
| | | |
| | | private static final class EmptyIterable<M> extends AbstractIterable<M> { |
| | | @Override |
| | | public Iterator<M> iterator() { |
| | | return Iterators.emptyIterator(); |
| | | } |
| | |
| | | this.parameter = p; |
| | | } |
| | | |
| | | @Override |
| | | public Iterator<M> iterator() { |
| | | return Iterators.filteredIterator(iterable.iterator(), predicate, parameter); |
| | | } |
| | |
| | | this.value = value; |
| | | } |
| | | |
| | | @Override |
| | | public Iterator<M> iterator() { |
| | | return Iterators.singletonIterator(value); |
| | | } |
| | |
| | | this.parameter = p; |
| | | } |
| | | |
| | | @Override |
| | | public Iterator<N> iterator() { |
| | | return Iterators.transformedIterator(iterable.iterator(), function, parameter); |
| | | } |
| | |
| | | this.iterable = iterable; |
| | | } |
| | | |
| | | @Override |
| | | public Iterator<M> iterator() { |
| | | return Iterators.unmodifiableIterator(iterable.iterator()); |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns {@code true} if the provided iterable does not contain any |
| | | * elements. |
| | | * |
| | | * @param iterable |
| | | * The iterable. |
| | | * @return {@code true} if the provided iterable does not contain any |
| | | * elements. |
| | | */ |
| | | public static boolean isEmpty(final Iterable<?> iterable) { |
| | | if (iterable instanceof Collection) { |
| | | // Fall-through if possible and potentially avoid allocation. |
| | | return ((Collection<?>) iterable).isEmpty(); |
| | | } else { |
| | | return !iterable.iterator().hasNext(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Returns an iterable containing the single element {@code value}. The |
| | | * returned iterable's iterator does not support element removal via the |
| | | * {@code remove()} method. |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the number of elements contained in the provided iterable. |
| | | * |
| | | * @param iterable |
| | | * The iterable. |
| | | * @return The number of elements contained in the provided iterable. |
| | | */ |
| | | public static int size(final Iterable<?> iterable) { |
| | | if (iterable instanceof Collection) { |
| | | // Fall-through if possible and potentially benefit from constant time calculation. |
| | | return ((Collection<?>) iterable).size(); |
| | | } else { |
| | | final Iterator<?> i = iterable.iterator(); |
| | | int sz = 0; |
| | | while (i.hasNext()) { |
| | | i.next(); |
| | | sz++; |
| | | } |
| | | return sz; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Returns a string representation of the provided iterable composed of an |
| | | * opening square bracket, followed by each element separated by commas, and |
| | | * then a closing square bracket. |
| | | * |
| | | * @param iterable |
| | | * The iterable whose string representation is to be returned. |
| | | * @return A string representation of the provided iterable. |
| | | * @see java.util.AbstractCollection#toString() |
| | | */ |
| | | public static String toString(final Iterable<?> iterable) { |
| | | if (iterable instanceof Collection) { |
| | | // Fall-through if possible. |
| | | return ((Collection<?>) iterable).toString(); |
| | | } else { |
| | | final StringBuilder builder = new StringBuilder(); |
| | | boolean firstValue = true; |
| | | builder.append('['); |
| | | for (final Object value : iterable) { |
| | | if (!firstValue) { |
| | | builder.append(", "); |
| | | } |
| | | builder.append(String.valueOf(value)); |
| | | firstValue = false; |
| | | } |
| | | builder.append(']'); |
| | | return builder.toString(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Returns a view of {@code iterable} whose values have been mapped to |
| | | * elements of type {@code N} using {@code function}. The returned |
| | | * iterable's iterator supports element removal via the {@code remove()} |
| | |
| | | return new UnmodifiableIterable<M>(iterable); |
| | | } |
| | | |
| | | /** |
| | | * Returns a string representation of the provided iterable composed of an |
| | | * opening square bracket, followed by each element separated by commas, and |
| | | * then a closing square bracket. |
| | | * |
| | | * @param iterable |
| | | * The iterable whose string representation is to be returned. |
| | | * @return A string representation of the provided iterable. |
| | | */ |
| | | public static String toString(Iterable<?> iterable) { |
| | | final StringBuilder builder = new StringBuilder(); |
| | | boolean firstValue = true; |
| | | builder.append('['); |
| | | for (Object value : iterable) { |
| | | if (!firstValue) { |
| | | builder.append(','); |
| | | } |
| | | builder.append(String.valueOf(value)); |
| | | firstValue = false; |
| | | } |
| | | builder.append(']'); |
| | | return builder.toString(); |
| | | } |
| | | |
| | | // Prevent instantiation |
| | | private Iterables() { |
| | | // Do nothing. |