com.google.common.collect.ImmutableSet Maven / Gradle / Ivy
Show all versions of google-collect Show documentation
/*
* Copyright (C) 2007 Google Inc.
*
* 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 com.google.common.base.Nullable;
import static com.google.common.base.Preconditions.checkArgument;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* A high-performance, immutable, hash-based {@code Set} with reliable,
* user-specified iteration order. Does not permit null elements.
*
* Unlike {@link Collections#unmodifiableSet}, which is a view of a
* separate collection that can still change, an instance of {@code
* ImmutableSet} contains its own private data and will never change.
* {@code ImmutableSet} is convenient for {@code public static final} sets
* ("constant sets") and also lets you easily make a "defensive copy" of a set
* provided to your class by a caller.
*
*
Warning: Like most sets, an {@code ImmutableSet} will not function
* correctly if an element is modified after being placed in the set. For this
* reason, and to avoid general confusion, it is strongly recommended to place
* only immutable objects into this collection.
*
*
This class has been observed to perform significantly better than {@link
* HashSet} for objects with very fast {@link Object#hashCode} implementations
* (as a well-behaved immutable object should).
*
*
Note: Although this class is not final, it cannot be subclassed as
* it has no public or protected constructors. Thus, the immutability guarantee
* can be trusted.
*
* @see ImmutableList
* @author Kevin Bourrillion
*/
@SuppressWarnings("serial") // we're overriding default serialization
public abstract class ImmutableSet implements Set, Serializable {
private static final ImmutableSet> EMPTY_IMMUTABLE_SET
= new EmptyImmutableSet();
/**
* Returns the empty immutable set. This set behaves and performs comparably
* to {@link Collections#emptySet}, and is preferable mainly for consistency
* and maintainability of your code.
*/
// Casting to any type is safe because the set will never hold any elements.
@SuppressWarnings({"unchecked"})
public static ImmutableSet of() {
return (ImmutableSet) EMPTY_IMMUTABLE_SET;
}
/**
* Returns an immutable set containing a single element. This set behaves and
* performs comparably to {@link Collections#singleton}, but will not accept
* a null element. It is preferable mainly for consistency and
* maintainability of your code.
*/
public static ImmutableSet of(E element) {
return new SingletonImmutableSet(element, element.hashCode());
}
/**
* 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).
*
* @throws NullPointerException if any of {@code elements} are null
*/
public static ImmutableSet of(E... elements) {
switch (elements.length) {
case 0:
return of();
case 1:
return of(elements[0]);
default:
return create(Arrays.asList(elements), elements.length);
}
}
/**
* 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).
*
* Note that if {@code s} is a {@code Set}, then {@code
* ImmutableSet.copyOf(s)} returns a {@code ImmutableSet} containing
* each of the strings in {@code s}, while ImmutableSet.of(s)} returns a
* {@code ImmutableSet>} containing one element (the given set
* itself).
*
* Note: 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} are null
*/
public static ImmutableSet copyOf(Iterable extends E> elements) {
if (elements instanceof ImmutableSet>) {
@SuppressWarnings("unchecked") // all supported methods are covariant
ImmutableSet set = (ImmutableSet) elements;
return set;
}
int size = Iterables.size(elements);
switch (size) {
case 0:
return of();
case 1:
// TODO: remove "ImmutableSet." when eclipse bug is fixed
return ImmutableSet.of(elements.iterator().next());
default:
return create(elements, size);
}
}
private final int hashCode;
private ImmutableSet(int hashCode) {
this.hashCode = hashCode;
}
// Overriding to mark it Nullable
public abstract boolean contains(@Nullable Object target);
public boolean containsAll(Collection> targets) {
for (Object target : targets) {
if (!contains(target)) {
return false;
}
}
return true;
}
/** Not supported. */
public final boolean add(E newElement) {
throw new UnsupportedOperationException();
}
/** Not supported. */
public final boolean remove(Object oldElement) {
throw new UnsupportedOperationException();
}
/** Not supported. */
public final boolean addAll(Collection extends E> newElements) {
throw new UnsupportedOperationException();
}
/** Not supported. */
public final boolean removeAll(Collection> oldElements) {
throw new UnsupportedOperationException();
}
/** Not supported. */
public final boolean retainAll(Collection> elementsToKeep) {
throw new UnsupportedOperationException();
}
/** Not supported. */
public final void clear() {
throw new UnsupportedOperationException();
}
@Override public final int hashCode() {
return hashCode;
}
private static final class EmptyImmutableSet extends ImmutableSet