/** * Views the array as an immutable list. Does not check for nulls; does not copy. * * <p>The array must be internally created. */ static <E> ImmutableList<E> asImmutableList(Object[] elements) { return asImmutableList(elements, elements.length); }
/** * Returns a newly-created {@code ImmutableList} based on the contents of the {@code Builder}. */ @Override public ImmutableList<E> build() { forceCopy = true; return asImmutableList(contents, size); } }
RegularEntrySet(ImmutableMap<K, V> map, Entry<K, V>[] entries) { this(map, ImmutableList.<Entry<K, V>>asImmutableList(entries)); }
RegularImmutableAsList(ImmutableCollection<E> delegate, Object[] array) { this(delegate, ImmutableList.<E>asImmutableList(array)); }
RegularEntrySet(ImmutableMap<K, V> map, ImmutableList<Entry<K, V>> entries) { this.map = map; this.entries = entries; }
RegularImmutableAsList(ImmutableCollection<E> delegate, Object[] array, int size) { this(delegate, ImmutableList.<E>asImmutableList(array, size)); }
/** * Views the array as an immutable list. Does not check for nulls; does not copy. * * <p>The array must be internally created. */ static <E> ImmutableList<E> asImmutableList(Object[] elements) { return asImmutableList(elements, elements.length); }
/** * Returns a newly-created {@code ImmutableList} based on the contents of the {@code Builder}. */ @Override public ImmutableList<E> build() { forceCopy = true; return asImmutableList(contents, size); } }
@Override ImmutableSet<E> build() { switch (distinct) { case 0: return of(); case 1: return of(dedupedElements[0]); default: return new JdkBackedImmutableSet<E>( delegate, ImmutableList.asImmutableList(dedupedElements, distinct)); } } }
/** Views the array as an immutable list. Checks for nulls; does not copy. */ private static <E> ImmutableList<E> construct(Object... elements) { return asImmutableList(checkElementsNotNull(elements)); }
/** * Returns an immutable list containing the given elements, sorted according to their natural * order. The sorting algorithm used is stable, so elements that compare as equal will stay in the * order in which they appear in the input. * * <p>If your data has no duplicates, or you wish to deduplicate elements, use {@code * ImmutableSortedSet.copyOf(elements)}; if you want a {@code List} you can use its {@code * asList()} view. * * <p><b>Java 8 users:</b> If you want to convert a {@link java.util.stream.Stream} to a sorted * {@code ImmutableList}, use {@code stream.sorted().collect(toImmutableList())}. * * @throws NullPointerException if any element in the input is null * @since 21.0 */ public static <E extends Comparable<? super E>> ImmutableList<E> sortedCopyOf( Iterable<? extends E> elements) { Comparable<?>[] array = Iterables.toArray(elements, new Comparable<?>[0]); checkElementsNotNull((Object[]) array); Arrays.sort(array); return asImmutableList(array); }
/** * Returns an immutable list containing the given elements, in sorted order relative to the * specified comparator. The sorting algorithm used is stable, so elements that compare as equal * will stay in the order in which they appear in the input. * * <p>If your data has no duplicates, or you wish to deduplicate elements, use {@code * ImmutableSortedSet.copyOf(comparator, elements)}; if you want a {@code List} you can use its * {@code asList()} view. * * <p><b>Java 8 users:</b> If you want to convert a {@link java.util.stream.Stream} to a sorted * {@code ImmutableList}, use {@code stream.sorted(comparator).collect(toImmutableList())}. * * @throws NullPointerException if any element in the input is null * @since 21.0 */ public static <E> ImmutableList<E> sortedCopyOf( Comparator<? super E> comparator, Iterable<? extends E> elements) { checkNotNull(comparator); @SuppressWarnings("unchecked") // all supported methods are covariant E[] array = (E[]) Iterables.toArray(elements); checkElementsNotNull(array); Arrays.sort(array, comparator); return asImmutableList(array); }
/** * Creates an {@code ImmutableMap} backed by a JDK HashMap. Used when probable hash flooding is * detected. This implementation may replace the entries in entryArray with its own entry objects * (though they will have the same key/value contents), and will take ownership of entryArray. */ static <K, V> ImmutableMap<K, V> create(int n, Entry<K, V>[] entryArray) { Map<K, V> delegateMap = Maps.newHashMapWithExpectedSize(n); for (int i = 0; i < n; i++) { entryArray[i] = makeImmutable(entryArray[i]); V oldValue = delegateMap.putIfAbsent(entryArray[i].getKey(), entryArray[i].getValue()); if (oldValue != null) { throw conflictException("key", entryArray[i], entryArray[i].getKey() + "=" + oldValue); } } return new JdkBackedImmutableMap<>(delegateMap, ImmutableList.asImmutableList(entryArray, n)); }
@VisibleForTesting static <K, V> ImmutableBiMap<K, V> create(int n, Entry<K, V>[] entryArray) { Map<K, V> forwardDelegate = Maps.newHashMapWithExpectedSize(n); Map<V, K> backwardDelegate = Maps.newHashMapWithExpectedSize(n); for (int i = 0; i < n; i++) { Entry<K, V> e = RegularImmutableMap.makeImmutable(entryArray[i]); entryArray[i] = e; V oldValue = forwardDelegate.putIfAbsent(e.getKey(), e.getValue()); if (oldValue != null) { throw conflictException("key", e.getKey() + "=" + oldValue, entryArray[i]); } K oldKey = backwardDelegate.putIfAbsent(e.getValue(), e.getKey()); if (oldKey != null) { throw conflictException("value", oldKey + "=" + e.getValue(), entryArray[i]); } } ImmutableList<Entry<K, V>> entryList = ImmutableList.asImmutableList(entryArray, n); return new JdkBackedImmutableBiMap<>(entryList, forwardDelegate, backwardDelegate); }
static <E> ImmutableMultiset<E> create(Collection<? extends Entry<? extends E>> entries) { @SuppressWarnings("unchecked") Entry<E>[] entriesArray = entries.toArray(new Entry[0]); Map<E, Integer> delegateMap = Maps.newHashMapWithExpectedSize(entriesArray.length); long size = 0; for (int i = 0; i < entriesArray.length; i++) { Entry<E> entry = entriesArray[i]; int count = entry.getCount(); size += count; E element = checkNotNull(entry.getElement()); delegateMap.put(element, count); if (!(entry instanceof Multisets.ImmutableEntry)) { entriesArray[i] = Multisets.immutableEntry(element, count); } } return new JdkBackedImmutableMultiset<>( delegateMap, ImmutableList.asImmutableList(entriesArray), size); }
/** * Constructs an {@code ImmutableSortedSet} from the first {@code n} elements of {@code contents}. * If {@code k} is the size of the returned {@code ImmutableSortedSet}, then the sorted unique * elements are in the first {@code k} positions of {@code contents}, and {@code contents[i] == * null} for {@code k <= i < n}. * * <p>If {@code k == contents.length}, then {@code contents} may no longer be safe for * modification. * * @throws NullPointerException if any of the first {@code n} elements of {@code contents} is null */ static <E> ImmutableSortedSet<E> construct( Comparator<? super E> comparator, int n, E... contents) { if (n == 0) { return emptySet(comparator); } checkElementsNotNull(contents, n); Arrays.sort(contents, 0, n, comparator); int uniques = 1; for (int i = 1; i < n; i++) { E cur = contents[i]; E prev = contents[uniques - 1]; if (comparator.compare(cur, prev) != 0) { contents[uniques++] = cur; } } Arrays.fill(contents, uniques, n, null); return new RegularImmutableSortedSet<E>( ImmutableList.<E>asImmutableList(contents, uniques), comparator); }
/** * Creates an {@code ImmutableMap} backed by a JDK HashMap. Used when probable hash flooding is * detected. This implementation may replace the entries in entryArray with its own entry objects * (though they will have the same key/value contents), and will take ownership of entryArray. */ static <K, V> ImmutableMap<K, V> create(int n, Entry<K, V>[] entryArray) { Map<K, V> delegateMap = Maps.newHashMapWithExpectedSize(n); for (int i = 0; i < n; i++) { entryArray[i] = makeImmutable(entryArray[i]); V oldValue = delegateMap.putIfAbsent(entryArray[i].getKey(), entryArray[i].getValue()); if (oldValue != null) { throw conflictException("key", entryArray[i], entryArray[i].getKey() + "=" + oldValue); } } return new JdkBackedImmutableMap<>(delegateMap, ImmutableList.asImmutableList(entryArray, n)); }
/** * Returns a newly-created {@code ImmutableSortedSet} based on the contents of the {@code * Builder} and its comparator. */ @Override public ImmutableSortedSet<E> build() { sortAndDedup(); if (n == 0) { return emptySet(comparator); } else { forceCopy = true; return new RegularImmutableSortedSet<E>( ImmutableList.asImmutableList(elements, n), comparator); } } }
/** * Returns an immutable list containing the given elements, in order. * * <p>Despite the method name, this method attempts to avoid actually copying the data when it is * safe to do so. The exact circumstances under which a copy will or will not be performed are * undocumented and subject to change. * * <p>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>This method is safe to use even when {@code elements} is a synchronized or concurrent * collection that is currently being modified by another thread. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableList<E> copyOf(Collection<? extends E> elements) { if (elements instanceof ImmutableCollection) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableList<E> list = ((ImmutableCollection<E>) elements).asList(); return list.isPartialView() ? ImmutableList.<E>asImmutableList(list.toArray()) : list; } return construct(elements.toArray()); }
? JdkBackedImmutableMultiset.create(ImmutableList.asImmutableList(entryArray)) : new RegularImmutableMultiset<E>( entryArray, hashTable, Ints.saturatedCast(size), hashCode, null);