/** Simplified version of {@link Iterables#toArray} that is GWT safe. */ private static <T> Object[] newObjectArray(Iterable<T> iterable) { Collection<T> collection = Collections2.toCollection(iterable); Object[] array = new Object[collection.size()]; return collection.toArray(array); }
/** * Returns an immutable list containing the given elements, in order. Note * that if {@code list} is a {@code List<String>}, then {@code * ImmutableList.copyOf(list)} returns an {@code ImmutableList<String>} * containing each of the strings in {@code list}, while * ImmutableList.of(list)} returns an {@code ImmutableList<List<String>>} * containing one element (the given list itself). * * <p><b>Note:</b> Despite what the method name suggests, if {@code elements} * is an {@code ImmutableList}, no copy will actually be performed, and the * given list itself will be returned. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableList<E> copyOf(Iterable<? extends E> elements) { if (elements instanceof ImmutableList) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableList<E> list = (ImmutableList<E>) elements; return list; } return copyOfInternal(Collections2.toCollection(elements)); }
/** * Copies an iterable's elements into an array. * * @param iterable the iterable to copy * @param type the type of the elements * @return a newly-allocated array into which all the elements of the iterable * have been copied */ @GwtIncompatible("Array.newInstance(Class, int)") public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) { Collection<? extends T> collection = Collections2.toCollection(iterable); T[] array = ObjectArrays.newArray(type, collection.size()); return collection.toArray(array); }
/** * Returns an immutable set containing the given elements, in order. Repeated * occurrences of an element (according to {@link Object#equals}) after the * first are ignored (but too many of these may result in the set being * sized inappropriately). This method iterates over {@code elements} at most * once. * * <p>Note that if {@code s} is a {@code Set<String>}, then {@code * ImmutableSet.copyOf(s)} returns an {@code ImmutableSet<String>} containing * each of the strings in {@code s}, while {@code ImmutableSet.of(s)} returns * a {@code ImmutableSet<Set<String>>} containing one element (the given set * itself). * * <p><b>Note:</b> Despite what the method name suggests, if {@code elements} * is an {@code ImmutableSet} (but not an {@code ImmutableSortedSet}), no copy * will actually be performed, and the given set itself will be returned. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements) { if (elements instanceof ImmutableSet && !(elements instanceof ImmutableSortedSet)) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableSet<E> set = (ImmutableSet<E>) elements; return set; } return copyOfInternal(Collections2.toCollection(elements)); }
/** * Returns an immutable set containing the given elements, in order. Repeated * occurrences of an element (according to {@link Object#equals}) after the * first are ignored (but too many of these may result in the set being * sized inappropriately). This method iterates over {@code elements} at most * once. * * <p>Note that if {@code s} is a {@code Set<String>}, then {@code * ImmutableSet.copyOf(s)} returns an {@code ImmutableSet<String>} containing * each of the strings in {@code s}, while {@code ImmutableSet.of(s)} returns * a {@code ImmutableSet<Set<String>>} containing one element (the given set * itself). * * <p><b>Note:</b> Despite what the method name suggests, if {@code elements} * is an {@code ImmutableSet} (but not an {@code ImmutableSortedSet}), no copy * will actually be performed, and the given set itself will be returned. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements) { if (elements instanceof ImmutableSet && !(elements instanceof ImmutableSortedSet)) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableSet<E> set = (ImmutableSet<E>) elements; return set; } return copyOfInternal(Collections2.toCollection(elements)); }
/** * Returns an immutable set containing the given elements, in order. Repeated * occurrences of an element (according to {@link Object#equals}) after the * first are ignored. This method iterates over {@code elements} at most * once. * * <p>Note that if {@code s} is a {@code Set<String>}, then {@code * ImmutableSet.copyOf(s)} returns an {@code ImmutableSet<String>} containing * each of the strings in {@code s}, while {@code ImmutableSet.of(s)} returns * a {@code ImmutableSet<Set<String>>} containing one element (the given set * itself). * * <p><b>Note:</b> Despite what the method name suggests, if {@code elements} * is an {@code ImmutableSet} (but not an {@code ImmutableSortedSet}), no copy * will actually be performed, and the given set itself will be returned. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements) { if (elements instanceof ImmutableSet && !(elements instanceof ImmutableSortedSet)) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableSet<E> set = (ImmutableSet<E>) elements; return set; } return copyOfInternal(Collections2.toCollection(elements)); }
/** * Returns an immutable set containing the given elements, in order. Repeated * occurrences of an element (according to {@link Object#equals}) after the * first are ignored (but too many of these may result in the set being * sized inappropriately). * * <p>Note that if {@code s} is a {@code Set<String>}, then {@code * ImmutableSet.copyOf(s)} returns an {@code ImmutableSet<String>} containing * each of the strings in {@code s}, while {@code ImmutableSet.of(s)} returns * a {@code ImmutableSet<Set<String>>} containing one element (the given set * itself). * * <p><b>Note:</b> Despite what the method name suggests, if {@code elements} * is an {@code ImmutableSet}, no copy will actually be performed, and the * given set itself will be returned. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements) { if (elements instanceof ImmutableSet) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableSet<E> set = (ImmutableSet<E>) elements; return set; } return copyOfInternal(Collections2.toCollection(elements)); }