com.google.common.collect.ImmutableSet Maven / Gradle / Ivy
Show all versions of guava Show documentation
/*
* Copyright (C) 2007 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.collect;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.math.IntMath;
import com.google.common.primitives.Ints;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.RetainedWith;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Collector;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A {@link Set} whose contents will never change, with many other important properties detailed at
* {@link ImmutableCollection}.
*
* @since 2.0
*/
@GwtCompatible(serializable = true, emulated = true)
@SuppressWarnings("serial") // we're overriding default serialization
@ElementTypesAreNonnullByDefault
public abstract class ImmutableSet extends ImmutableCollection implements Set {
static final int SPLITERATOR_CHARACTERISTICS =
ImmutableCollection.SPLITERATOR_CHARACTERISTICS | Spliterator.DISTINCT;
/**
* Returns a {@code Collector} that accumulates the input elements into a new {@code
* ImmutableSet}. Elements appear in the resulting set in the encounter order of the stream; if
* the stream contains duplicates (according to {@link Object#equals(Object)}), only the first
* duplicate in encounter order will appear in the result.
*
* @since 21.0
*/
public static Collector> toImmutableSet() {
return CollectCollectors.toImmutableSet();
}
/**
* Returns the empty immutable set. Preferred over {@link Collections#emptySet} for code
* consistency, and because the return type conveys the immutability guarantee.
*
* Performance note: the instance returned is a singleton.
*/
@SuppressWarnings({"unchecked"}) // fully variant implementation (never actually produces any Es)
public static ImmutableSet of() {
return (ImmutableSet) RegularImmutableSet.EMPTY;
}
/**
* Returns an immutable set containing the given element. Preferred over {@link
* Collections#singleton} for code consistency, {@code null} rejection, and because the return
* type conveys the immutability guarantee.
*/
public static ImmutableSet of(E e1) {
return new SingletonImmutableSet<>(e1);
}
/*
* TODO: b/315526394 - Skip the Builder entirely for the of(...) methods, since we don't need to
* worry that we might trigger the fallback to the JDK-backed implementation? (The varargs one
* _could_, so we could keep it as it is. Or we could convince ourselves that hash flooding is
* unlikely in practice there, too.)
*/
/**
* Returns an immutable set containing the given elements, minus duplicates, in the order each was
* first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except
* the first are ignored.
*/
public static ImmutableSet of(E e1, E e2) {
return new RegularSetBuilderImpl(2).add(e1).add(e2).review().build();
}
/**
* Returns an immutable set containing the given elements, minus duplicates, in the order each was
* first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except
* the first are ignored.
*/
public static ImmutableSet of(E e1, E e2, E e3) {
return new RegularSetBuilderImpl(3).add(e1).add(e2).add(e3).review().build();
}
/**
* Returns an immutable set containing the given elements, minus duplicates, in the order each was
* first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except
* the first are ignored.
*/
public static ImmutableSet of(E e1, E e2, E e3, E e4) {
return new RegularSetBuilderImpl(4).add(e1).add(e2).add(e3).add(e4).review().build();
}
/**
* Returns an immutable set containing the given elements, minus duplicates, in the order each was
* first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except
* the first are ignored.
*/
public static ImmutableSet of(E e1, E e2, E e3, E e4, E e5) {
return new RegularSetBuilderImpl(5).add(e1).add(e2).add(e3).add(e4).add(e5).review().build();
}
/**
* Returns an immutable set containing the given elements, minus duplicates, in the order each was
* first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except
* the first are ignored.
*
* The array {@code others} must not be longer than {@code Integer.MAX_VALUE - 6}.
*
* @since 3.0 (source-compatible since 2.0)
*/
@SafeVarargs // For Eclipse. For internal javac we have disabled this pointless type of warning.
public static ImmutableSet of(E e1, E e2, E e3, E e4, E e5, E e6, E... others) {
checkArgument(
others.length <= Integer.MAX_VALUE - 6, "the total number of elements must fit in an int");
SetBuilderImpl builder = new RegularSetBuilderImpl<>(6 + others.length);
builder = builder.add(e1).add(e2).add(e3).add(e4).add(e5).add(e6);
for (int i = 0; i < others.length; i++) {
builder = builder.add(others[i]);
}
return builder.review().build();
}
/**
* Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
* each appears first in the source collection.
*
* Performance note: This method will sometimes recognize that the actual copy operation
* is unnecessary; for example, {@code copyOf(copyOf(anArrayList))} will copy the data only once.
* This reduces the expense of habitually making defensive copies at API boundaries. However, the
* precise conditions for skipping the copy operation are undefined.
*
* @throws NullPointerException if any of {@code elements} is null
* @since 7.0 (source-compatible since 2.0)
*/
// This the best we could do to get copyOfEnumSet to compile in the mainline.
// The suppression also covers the cast to E[], discussed below.
// In the backport, we don't have those cases and thus don't need this suppression.
// We keep it to minimize diffs.
@SuppressWarnings("unchecked")
public static ImmutableSet copyOf(Collection extends E> elements) {
/*
* TODO(lowasser): consider checking for ImmutableAsList here
* TODO(lowasser): consider checking for Multiset here
*/
// Don't refer to ImmutableSortedSet by name so it won't pull in all that code
if (elements instanceof ImmutableSet && !(elements instanceof SortedSet)) {
@SuppressWarnings("unchecked") // all supported methods are covariant
ImmutableSet set = (ImmutableSet) elements;
if (!set.isPartialView()) {
return set;
}
} else if (elements instanceof EnumSet) {
return copyOfEnumSet((EnumSet>) elements);
}
if (elements.isEmpty()) {
// We avoid allocating anything.
return of();
}
// Collection.toArray() is required to contain only E instances, and all we do is read them.
// TODO(cpovirk): Consider using Object[] anyway.
E[] array = (E[]) elements.toArray();
/*
* For a Set, we guess that it contains no duplicates. That's just a guess for purpose of
* sizing; if the Set uses different equality semantics, it might contain duplicates according
* to equals(), and we will deduplicate those properly, albeit at some cost in allocations.
*/
int expectedSize =
elements instanceof Set ? array.length : estimatedSizeForUnknownDuplication(array.length);
return fromArrayWithExpectedSize(array, expectedSize);
}
/**
* Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
* each appears first in the source iterable. This method iterates over {@code elements} only
* once.
*
* Performance note: This method will sometimes recognize that the actual copy operation
* is unnecessary; for example, {@code copyOf(copyOf(anArrayList))} should copy the data only
* once. This reduces the expense of habitually making defensive copies at API boundaries.
* However, the precise conditions for skipping the copy operation are undefined.
*
* @throws NullPointerException if any of {@code elements} is null
*/
public static ImmutableSet copyOf(Iterable extends E> elements) {
return (elements instanceof Collection)
? copyOf((Collection extends E>) elements)
: copyOf(elements.iterator());
}
/**
* Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
* each appears first in the source iterator.
*
* @throws NullPointerException if any of {@code elements} is null
*/
public static ImmutableSet copyOf(Iterator extends E> elements) {
// We special-case for 0 or 1 elements, but anything further is madness.
if (!elements.hasNext()) {
return of();
}
E first = elements.next();
if (!elements.hasNext()) {
return of(first);
} else {
return new ImmutableSet.Builder().add(first).addAll(elements).build();
}
}
/**
* Returns an immutable set containing each of {@code elements}, minus duplicates, in the order
* each appears first in the source array.
*
* @throws NullPointerException if any of {@code elements} is null
* @since 3.0
*/
public static ImmutableSet copyOf(E[] elements) {
return fromArrayWithExpectedSize(elements, estimatedSizeForUnknownDuplication(elements.length));
}
private static ImmutableSet fromArrayWithExpectedSize(E[] elements, int expectedSize) {
switch (elements.length) {
case 0:
return of();
case 1:
return of(elements[0]);
default:
SetBuilderImpl builder = new RegularSetBuilderImpl<>(expectedSize);
for (int i = 0; i < elements.length; i++) {
builder = builder.add(elements[i]);
}
return builder.review().build();
}
}
@SuppressWarnings({"rawtypes", "unchecked"}) // necessary to compile against Java 8
private static ImmutableSet copyOfEnumSet(EnumSet> enumSet) {
return ImmutableEnumSet.asImmutable(EnumSet.copyOf((EnumSet) enumSet));
}
ImmutableSet() {}
/** Returns {@code true} if the {@code hashCode()} method runs quickly. */
boolean isHashCodeFast() {
return false;
}
@Override
public boolean equals(@CheckForNull Object object) {
if (object == this) {
return true;
}
if (object instanceof ImmutableSet
&& isHashCodeFast()
&& ((ImmutableSet>) object).isHashCodeFast()
&& hashCode() != object.hashCode()) {
return false;
}
return Sets.equalsImpl(this, object);
}
@Override
public int hashCode() {
return Sets.hashCodeImpl(this);
}
// This declaration is needed to make Set.iterator() and
// ImmutableCollection.iterator() consistent.
@Override
public abstract UnmodifiableIterator iterator();
@GwtCompatible
abstract static class CachingAsList extends ImmutableSet {
@LazyInit @RetainedWith @CheckForNull private transient ImmutableList asList;
@Override
public ImmutableList asList() {
ImmutableList result = asList;
if (result == null) {
return asList = createAsList();
} else {
return result;
}
}
ImmutableList createAsList() {
return new RegularImmutableAsList<>(this, toArray());
}
// redeclare to help optimizers with b/310253115
@SuppressWarnings("RedundantOverride")
@Override
@J2ktIncompatible // serialization
@GwtIncompatible // serialization
Object writeReplace() {
return super.writeReplace();
}
}
abstract static class Indexed extends CachingAsList {
abstract E get(int index);
@Override
public UnmodifiableIterator iterator() {
return asList().iterator();
}
@Override
public Spliterator spliterator() {
return CollectSpliterators.indexed(size(), SPLITERATOR_CHARACTERISTICS, this::get);
}
@Override
public void forEach(Consumer super E> consumer) {
checkNotNull(consumer);
int n = size();
for (int i = 0; i < n; i++) {
consumer.accept(get(i));
}
}
@Override
int copyIntoArray(@Nullable Object[] dst, int offset) {
return asList().copyIntoArray(dst, offset);
}
@Override
ImmutableList createAsList() {
return new ImmutableAsList() {
@Override
public E get(int index) {
return Indexed.this.get(index);
}
@Override
Indexed delegateCollection() {
return Indexed.this;
}
// redeclare to help optimizers with b/310253115
@SuppressWarnings("RedundantOverride")
@Override
@J2ktIncompatible // serialization
@GwtIncompatible // serialization
Object writeReplace() {
return super.writeReplace();
}
};
}
// redeclare to help optimizers with b/310253115
@SuppressWarnings("RedundantOverride")
@Override
@J2ktIncompatible // serialization
@GwtIncompatible // serialization
Object writeReplace() {
return super.writeReplace();
}
}
/*
* This class is used to serialize all ImmutableSet instances, except for
* ImmutableEnumSet/ImmutableSortedSet, regardless of implementation type. It
* captures their "logical contents" and they are reconstructed using public
* static factories. This is necessary to ensure that the existence of a
* particular implementation type is an implementation detail.
*/
@J2ktIncompatible // serialization
private static class SerializedForm implements Serializable {
final Object[] elements;
SerializedForm(Object[] elements) {
this.elements = elements;
}
Object readResolve() {
return copyOf(elements);
}
private static final long serialVersionUID = 0;
}
@Override
@J2ktIncompatible // serialization
Object writeReplace() {
return new SerializedForm(toArray());
}
@J2ktIncompatible // serialization
private void readObject(ObjectInputStream stream) throws InvalidObjectException {
throw new InvalidObjectException("Use SerializedForm");
}
/**
* Returns a new builder. The generated builder is equivalent to the builder created by the {@link
* Builder} constructor.
*/
public static Builder builder() {
return new Builder<>();
}
/**
* Returns a new builder, expecting the specified number of distinct elements to be added.
*
* If {@code expectedSize} is exactly the number of distinct elements added to the builder
* before {@link Builder#build} is called, the builder is likely to perform better than an unsized
* {@link #builder()} would have.
*
*
It is not specified if any performance benefits apply if {@code expectedSize} is close to,
* but not exactly, the number of distinct elements added to the builder.
*
* @since 23.1
*/
public static Builder builderWithExpectedSize(int expectedSize) {
checkNonnegative(expectedSize, "expectedSize");
return new Builder<>(expectedSize);
}
/**
* A builder for creating {@code ImmutableSet} instances. Example:
*
* {@code
* static final ImmutableSet GOOGLE_COLORS =
* ImmutableSet.builder()
* .addAll(WEBSAFE_COLORS)
* .add(new Color(0, 191, 255))
* .build();
* }
*
* Elements appear in the resulting set in the same order they were first added to the builder.
*
*
Building does not change the state of the builder, so it is still possible to add more
* elements and to build again.
*
* @since 2.0
*/
public static class Builder extends ImmutableCollection.Builder {
/*
* `impl` is null only for instances of the subclass, ImmutableSortedSet.Builder. That subclass
* overrides all the methods that access it here. Thus, all the methods here can safely assume
* that this field is non-null.
*/
@CheckForNull private SetBuilderImpl impl;
boolean forceCopy;
public Builder() {
this(0);
}
Builder(int capacity) {
if (capacity > 0) {
impl = new RegularSetBuilderImpl<>(capacity);
} else {
impl = EmptySetBuilderImpl.instance();
}
}
Builder(@SuppressWarnings("unused") boolean subclass) {
this.impl = null; // unused
}
@VisibleForTesting
void forceJdk() {
requireNonNull(impl); // see the comment on the field
this.impl = new JdkBackedSetBuilderImpl<>(impl);
}
final void copyIfNecessary() {
if (forceCopy) {
copy();
forceCopy = false;
}
}
void copy() {
requireNonNull(impl); // see the comment on the field
impl = impl.copy();
}
@Override
@CanIgnoreReturnValue
public Builder add(E element) {
requireNonNull(impl); // see the comment on the field
checkNotNull(element);
copyIfNecessary();
impl = impl.add(element);
return this;
}
@Override
@CanIgnoreReturnValue
public Builder add(E... elements) {
super.add(elements);
return this;
}
/**
* Adds each element of {@code elements} to the {@code ImmutableSet}, ignoring duplicate
* elements (only the first duplicate element is added).
*
* @param elements the elements to add
* @return this {@code Builder} object
* @throws NullPointerException if {@code elements} is null or contains a null element
*/
@Override
@CanIgnoreReturnValue
public Builder addAll(Iterable extends E> elements) {
super.addAll(elements);
return this;
}
@Override
@CanIgnoreReturnValue
public Builder addAll(Iterator extends E> elements) {
super.addAll(elements);
return this;
}
@CanIgnoreReturnValue
Builder combine(Builder other) {
requireNonNull(impl);
requireNonNull(other.impl);
/*
* For discussion of requireNonNull, see the comment on the field.
*
* (And I don't believe there's any situation in which we call x.combine(y) when x is a plain
* ImmutableSet.Builder but y is an ImmutableSortedSet.Builder (or vice versa). Certainly
* ImmutableSortedSet.Builder.combine() is written as if its argument will never be a plain
* ImmutableSet.Builder: It casts immediately to ImmutableSortedSet.Builder.)
*/
copyIfNecessary();
this.impl = this.impl.combine(other.impl);
return this;
}
@Override
public ImmutableSet build() {
requireNonNull(impl); // see the comment on the field
forceCopy = true;
impl = impl.review();
return impl.build();
}
}
/** Swappable internal implementation of an ImmutableSet.Builder. */
private abstract static class SetBuilderImpl {
// The first `distinct` elements are non-null.
// Since we can never access null elements, we don't mark this nullable.
E[] dedupedElements;
int distinct;
@SuppressWarnings("unchecked")
SetBuilderImpl(int expectedCapacity) {
this.dedupedElements = (E[]) new Object[expectedCapacity];
this.distinct = 0;
}
/** Initializes this SetBuilderImpl with a copy of the deduped elements array from toCopy. */
SetBuilderImpl(SetBuilderImpl toCopy) {
this.dedupedElements = Arrays.copyOf(toCopy.dedupedElements, toCopy.dedupedElements.length);
this.distinct = toCopy.distinct;
}
/**
* Resizes internal data structures if necessary to store the specified number of distinct
* elements.
*/
private void ensureCapacity(int minCapacity) {
if (minCapacity > dedupedElements.length) {
int newCapacity =
ImmutableCollection.Builder.expandedCapacity(dedupedElements.length, minCapacity);
dedupedElements = Arrays.copyOf(dedupedElements, newCapacity);
}
}
/** Adds e to the insertion-order array of deduplicated elements. Calls ensureCapacity. */
final void addDedupedElement(E e) {
ensureCapacity(distinct + 1);
dedupedElements[distinct++] = e;
}
/**
* Adds e to this SetBuilderImpl, returning the updated result. Only use the returned
* SetBuilderImpl, since we may switch implementations if e.g. hash flooding is detected.
*/
abstract SetBuilderImpl add(E e);
/** Adds all the elements from the specified SetBuilderImpl to this SetBuilderImpl. */
final SetBuilderImpl combine(SetBuilderImpl other) {
SetBuilderImpl result = this;
for (int i = 0; i < other.distinct; i++) {
/*
* requireNonNull is safe because we ensure that the first `distinct` elements have been
* populated.
*/
result = result.add(requireNonNull(other.dedupedElements[i]));
}
return result;
}
/**
* Creates a new copy of this SetBuilderImpl. Modifications to that SetBuilderImpl will not
* affect this SetBuilderImpl or sets constructed from this SetBuilderImpl via build().
*/
abstract SetBuilderImpl copy();
/**
* Call this before build(). Does a final check on the internal data structures, e.g. shrinking
* unnecessarily large structures or detecting previously unnoticed hash flooding.
*/
SetBuilderImpl review() {
return this;
}
abstract ImmutableSet build();
}
private static final class EmptySetBuilderImpl extends SetBuilderImpl {
private static final EmptySetBuilderImpl