org.testifyproject.guava.common.collect.ImmutableSetMultimap Maven / Gradle / Ivy
/*
* Copyright (C) 2009 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 org.testifyproject.guava.common.collect;
import static org.testifyproject.guava.common.base.Preconditions.checkNotNull;
import org.testifyproject.guava.common.annotations.Beta;
import org.testifyproject.guava.common.annotations.GwtCompatible;
import org.testifyproject.guava.common.annotations.GwtIncompatible;
import org.testifyproject.guava.common.base.MoreObjects;
import org.testifyproject.guava.common.base.Preconditions;
import org.testifyproject.guava.errorprone.annotations.CanIgnoreReturnValue;
import org.testifyproject.guava.errorprone.annotations.concurrent.LazyInit;
import org.testifyproject.guava.j2objc.annotations.RetainedWith;
import org.testifyproject.guava.j2objc.annotations.Weak;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
/**
* A {@link SetMultimap} whose contents will never change, with many other important properties
* detailed at {@link ImmutableCollection}.
*
* See the Guava User Guide article on
* immutable collections.
*
* @author Mike Ward
* @since 2.0
*/
@GwtCompatible(serializable = true, emulated = true)
public class ImmutableSetMultimap extends ImmutableMultimap
implements SetMultimap {
/**
* Returns a {@link Collector} that accumulates elements into an {@code ImmutableSetMultimap}
* whose keys and values are the result of applying the provided mapping functions to the input
* elements.
*
* For streams with {@linkplain java.util.stream#Ordering defined encounter order}, that order
* is preserved, but entries are grouped by key.
*
* Example:
*
{@code
*
* static final Multimap FIRST_LETTER_MULTIMAP =
* Stream.of("banana", "apple", "carrot", "asparagus", "cherry")
* .collect(toImmutableSetMultimap(str -> str.charAt(0), str -> str.substring(1)));
*
* // is equivalent to
*
* static final Multimap FIRST_LETTER_MULTIMAP =
* new ImmutableSetMultimap.Builder()
* .put('b', "anana")
* .putAll('a', "pple", "sparagus")
* .putAll('c', "arrot", "herry")
* .build();}
* @since 21.0
*/
@Beta
public static Collector> toImmutableSetMultimap(
Function super T, ? extends K> keyFunction,
Function super T, ? extends V> valueFunction) {
checkNotNull(keyFunction, "keyFunction");
checkNotNull(valueFunction, "valueFunction");
return Collector.of(
ImmutableSetMultimap::builder,
(builder, t) -> builder.put(keyFunction.apply(t), valueFunction.apply(t)),
ImmutableSetMultimap.Builder::combine,
ImmutableSetMultimap.Builder::build);
}
/**
* Returns a {@code Collector} accumulating entries into an {@code ImmutableSetMultimap}. Each
* input element is mapped to a key and a stream of values, each of which are put into the
* resulting {@code Multimap}, in the encounter order of the stream and the encounter order of the
* streams of values.
*
* Example:
*
*
{@code
* static final ImmutableSetMultimap FIRST_LETTER_MULTIMAP =
* Stream.of("banana", "apple", "carrot", "asparagus", "cherry")
* .collect(
* flatteningToImmutableSetMultimap(
* str -> str.charAt(0),
* str -> str.substring(1).chars().mapToObj(c -> (char) c));
*
* // is equivalent to
*
* static final ImmutableSetMultimap FIRST_LETTER_MULTIMAP =
* ImmutableSetMultimap.builder()
* .putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a'))
* .putAll('a', Arrays.asList('p', 'p', 'l', 'e'))
* .putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't'))
* .putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's'))
* .putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y'))
* .build();
*
* // after deduplication, the resulting multimap is equivalent to
*
* static final ImmutableSetMultimap FIRST_LETTER_MULTIMAP =
* ImmutableSetMultimap.builder()
* .putAll('b', Arrays.asList('a', 'n'))
* .putAll('a', Arrays.asList('p', 'l', 'e', 's', 'a', 'r', 'g', 'u'))
* .putAll('c', Arrays.asList('a', 'r', 'o', 't', 'h', 'e', 'y'))
* .build();
* }
* }
*
* @since 21.0
*/
@Beta
public static
Collector> flatteningToImmutableSetMultimap(
Function super T, ? extends K> keyFunction,
Function super T, ? extends Stream extends V>> valuesFunction) {
checkNotNull(keyFunction);
checkNotNull(valuesFunction);
return Collectors.collectingAndThen(
Multimaps.flatteningToMultimap(
input -> checkNotNull(keyFunction.apply(input)),
input -> valuesFunction.apply(input).peek(Preconditions::checkNotNull),
MultimapBuilder.linkedHashKeys().linkedHashSetValues()::build),
ImmutableSetMultimap::copyOf);
}
/** Returns the empty multimap. */
// Casting is safe because the multimap will never hold any elements.
@SuppressWarnings("unchecked")
public static ImmutableSetMultimap of() {
return (ImmutableSetMultimap) EmptyImmutableSetMultimap.INSTANCE;
}
/**
* Returns an immutable multimap containing a single entry.
*/
public static ImmutableSetMultimap of(K k1, V v1) {
ImmutableSetMultimap.Builder builder = ImmutableSetMultimap.builder();
builder.put(k1, v1);
return builder.build();
}
/**
* Returns an immutable multimap containing the given entries, in order.
* Repeated occurrences of an entry (according to {@link Object#equals}) after
* the first are ignored.
*/
public static ImmutableSetMultimap of(K k1, V v1, K k2, V v2) {
ImmutableSetMultimap.Builder builder = ImmutableSetMultimap.builder();
builder.put(k1, v1);
builder.put(k2, v2);
return builder.build();
}
/**
* Returns an immutable multimap containing the given entries, in order.
* Repeated occurrences of an entry (according to {@link Object#equals}) after
* the first are ignored.
*/
public static ImmutableSetMultimap of(K k1, V v1, K k2, V v2, K k3, V v3) {
ImmutableSetMultimap.Builder builder = ImmutableSetMultimap.builder();
builder.put(k1, v1);
builder.put(k2, v2);
builder.put(k3, v3);
return builder.build();
}
/**
* Returns an immutable multimap containing the given entries, in order.
* Repeated occurrences of an entry (according to {@link Object#equals}) after
* the first are ignored.
*/
public static ImmutableSetMultimap of(
K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
ImmutableSetMultimap.Builder builder = ImmutableSetMultimap.builder();
builder.put(k1, v1);
builder.put(k2, v2);
builder.put(k3, v3);
builder.put(k4, v4);
return builder.build();
}
/**
* Returns an immutable multimap containing the given entries, in order.
* Repeated occurrences of an entry (according to {@link Object#equals}) after
* the first are ignored.
*/
public static ImmutableSetMultimap of(
K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
ImmutableSetMultimap.Builder builder = ImmutableSetMultimap.builder();
builder.put(k1, v1);
builder.put(k2, v2);
builder.put(k3, v3);
builder.put(k4, v4);
builder.put(k5, v5);
return builder.build();
}
// looking for of() with > 5 entries? Use the builder instead.
/**
* Returns a new {@link Builder}.
*/
public static Builder builder() {
return new Builder<>();
}
/**
* A builder for creating immutable {@code SetMultimap} instances, especially
* {@code public static final} multimaps ("constant multimaps"). Example:
* {@code
*
* static final Multimap STRING_TO_INTEGER_MULTIMAP =
* new ImmutableSetMultimap.Builder()
* .put("one", 1)
* .putAll("several", 1, 2, 3)
* .putAll("many", 1, 2, 3, 4, 5)
* .build();}
*
* Builder instances can be reused; it is safe to call {@link #build} multiple
* times to build multiple multimaps in series. Each multimap contains the
* key-value mappings in the previously created multimaps.
*
* @since 2.0
*/
public static final class Builder extends ImmutableMultimap.Builder {
/**
* Creates a new builder. The returned builder is equivalent to the builder
* generated by {@link ImmutableSetMultimap#builder}.
*/
public Builder() {
super(MultimapBuilder.linkedHashKeys().linkedHashSetValues().build());
}
/**
* Adds a key-value mapping to the built multimap if it is not already
* present.
*/
@CanIgnoreReturnValue
@Override
public Builder put(K key, V value) {
builderMultimap.put(checkNotNull(key), checkNotNull(value));
return this;
}
/**
* Adds an entry to the built multimap if it is not already present.
*
* @since 11.0
*/
@CanIgnoreReturnValue
@Override
public Builder put(Entry extends K, ? extends V> entry) {
builderMultimap.put(checkNotNull(entry.getKey()), checkNotNull(entry.getValue()));
return this;
}
/**
* {@inheritDoc}
*
* @since 19.0
*/
@CanIgnoreReturnValue
@Beta
@Override
public Builder putAll(Iterable extends Entry extends K, ? extends V>> entries) {
super.putAll(entries);
return this;
}
@CanIgnoreReturnValue
@Override
public Builder putAll(K key, Iterable extends V> values) {
Collection collection = builderMultimap.get(checkNotNull(key));
for (V value : values) {
collection.add(checkNotNull(value));
}
return this;
}
@CanIgnoreReturnValue
@Override
public Builder putAll(K key, V... values) {
return putAll(key, Arrays.asList(values));
}
@CanIgnoreReturnValue
@Override
public Builder putAll(Multimap extends K, ? extends V> multimap) {
for (Entry extends K, ? extends Collection extends V>> entry :
multimap.asMap().entrySet()) {
putAll(entry.getKey(), entry.getValue());
}
return this;
}
@CanIgnoreReturnValue
@Override
Builder combine(ImmutableMultimap.Builder other) {
super.combine(other);
return this;
}
/**
* {@inheritDoc}
*
* @since 8.0
*/
@CanIgnoreReturnValue
@Override
public Builder orderKeysBy(Comparator super K> keyComparator) {
this.keyComparator = checkNotNull(keyComparator);
return this;
}
/**
* Specifies the ordering of the generated multimap's values for each key.
*
* If this method is called, the sets returned by the {@code get()}
* method of the generated multimap and its {@link Multimap#asMap()} view
* are {@link ImmutableSortedSet} instances. However, serialization does not
* preserve that property, though it does maintain the key and value
* ordering.
*
* @since 8.0
*/
// TODO: Make serialization behavior consistent.
@CanIgnoreReturnValue
@Override
public Builder orderValuesBy(Comparator super V> valueComparator) {
super.orderValuesBy(valueComparator);
return this;
}
/**
* Returns a newly-created immutable set multimap.
*/
@Override
public ImmutableSetMultimap build() {
if (keyComparator != null) {
Multimap sortedCopy =
MultimapBuilder.linkedHashKeys().linkedHashSetValues().build();
List>> entries =
Ordering.from(keyComparator)
.onKeys()
.immutableSortedCopy(builderMultimap.asMap().entrySet());
for (Map.Entry> entry : entries) {
sortedCopy.putAll(entry.getKey(), entry.getValue());
}
builderMultimap = sortedCopy;
}
return copyOf(builderMultimap, valueComparator);
}
}
/**
* Returns an immutable set multimap containing the same mappings as
* {@code multimap}. The generated multimap's key and value orderings
* correspond to the iteration ordering of the {@code multimap.asMap()} view.
* Repeated occurrences of an entry in the multimap after the first are
* ignored.
*
* 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.
*
* @throws NullPointerException if any key or value in {@code multimap} is
* null
*/
public static ImmutableSetMultimap copyOf(
Multimap extends K, ? extends V> multimap) {
return copyOf(multimap, null);
}
private static ImmutableSetMultimap copyOf(
Multimap extends K, ? extends V> multimap, Comparator super V> valueComparator) {
checkNotNull(multimap); // eager for GWT
if (multimap.isEmpty() && valueComparator == null) {
return of();
}
if (multimap instanceof ImmutableSetMultimap) {
@SuppressWarnings("unchecked") // safe since multimap is not writable
ImmutableSetMultimap kvMultimap = (ImmutableSetMultimap) multimap;
if (!kvMultimap.isPartialView()) {
return kvMultimap;
}
}
ImmutableMap.Builder> builder =
new ImmutableMap.Builder<>(multimap.asMap().size());
int size = 0;
for (Entry extends K, ? extends Collection extends V>> entry :
multimap.asMap().entrySet()) {
K key = entry.getKey();
Collection extends V> values = entry.getValue();
ImmutableSet set = valueSet(valueComparator, values);
if (!set.isEmpty()) {
builder.put(key, set);
size += set.size();
}
}
return new ImmutableSetMultimap<>(builder.build(), size, valueComparator);
}
/**
* Returns an immutable multimap containing the specified entries. The
* returned multimap iterates over keys in the order they were first
* encountered in the input, and the values for each key are iterated in the
* order they were encountered. If two values for the same key are
* {@linkplain Object#equals equal}, the first value encountered is used.
*
* @throws NullPointerException if any key, value, or entry is null
* @since 19.0
*/
@Beta
public static ImmutableSetMultimap copyOf(
Iterable extends Entry extends K, ? extends V>> entries) {
return new Builder().putAll(entries).build();
}
/**
* Returned by get() when a missing key is provided. Also holds the
* comparator, if any, used for values.
*/
private final transient ImmutableSet emptySet;
ImmutableSetMultimap(
ImmutableMap> map,
int size,
@Nullable Comparator super V> valueComparator) {
super(map, size);
this.emptySet = emptySet(valueComparator);
}
// views
/**
* Returns an immutable set of the values for the given key. If no mappings
* in the multimap have the provided key, an empty immutable set is returned.
* The values are in the same order as the parameters used to build this
* multimap.
*/
@Override
public ImmutableSet get(@Nullable K key) {
// This cast is safe as its type is known in constructor.
ImmutableSet set = (ImmutableSet) map.get(key);
return MoreObjects.firstNonNull(set, emptySet);
}
@LazyInit
@RetainedWith
private transient ImmutableSetMultimap inverse;
/**
* {@inheritDoc}
*
* Because an inverse of a set multimap cannot contain multiple pairs with
* the same key and value, this method returns an {@code ImmutableSetMultimap}
* rather than the {@code ImmutableMultimap} specified in the {@code
* ImmutableMultimap} class.
*
* @since 11.0
*/
public ImmutableSetMultimap inverse() {
ImmutableSetMultimap result = inverse;
return (result == null) ? (inverse = invert()) : result;
}
private ImmutableSetMultimap invert() {
Builder builder = builder();
for (Entry entry : entries()) {
builder.put(entry.getValue(), entry.getKey());
}
ImmutableSetMultimap invertedMultimap = builder.build();
invertedMultimap.inverse = this;
return invertedMultimap;
}
/**
* Guaranteed to throw an exception and leave the multimap unmodified.
*
* @throws UnsupportedOperationException always
* @deprecated Unsupported operation.
*/
@CanIgnoreReturnValue
@Deprecated
@Override
public ImmutableSet removeAll(Object key) {
throw new UnsupportedOperationException();
}
/**
* Guaranteed to throw an exception and leave the multimap unmodified.
*
* @throws UnsupportedOperationException always
* @deprecated Unsupported operation.
*/
@CanIgnoreReturnValue
@Deprecated
@Override
public ImmutableSet replaceValues(K key, Iterable extends V> values) {
throw new UnsupportedOperationException();
}
private transient ImmutableSet> entries;
/**
* Returns an immutable collection of all key-value pairs in the multimap.
* Its iterator traverses the values for the first key, the values for the
* second key, and so on.
*/
@Override
public ImmutableSet> entries() {
ImmutableSet> result = entries;
return result == null ? (entries = new EntrySet<>(this)) : result;
}
private static final class EntrySet extends ImmutableSet> {
@Weak private final transient ImmutableSetMultimap multimap;
EntrySet(ImmutableSetMultimap multimap) {
this.multimap = multimap;
}
@Override
public boolean contains(@Nullable Object object) {
if (object instanceof Entry) {
Entry, ?> entry = (Entry, ?>) object;
return multimap.containsEntry(entry.getKey(), entry.getValue());
}
return false;
}
@Override
public int size() {
return multimap.size();
}
@Override
public UnmodifiableIterator> iterator() {
return multimap.entryIterator();
}
@Override
boolean isPartialView() {
return false;
}
}
private static ImmutableSet valueSet(
@Nullable Comparator super V> valueComparator, Collection extends V> values) {
return (valueComparator == null)
? ImmutableSet.copyOf(values)
: ImmutableSortedSet.copyOf(valueComparator, values);
}
private static ImmutableSet emptySet(@Nullable Comparator super V> valueComparator) {
return (valueComparator == null)
? ImmutableSet.of()
: ImmutableSortedSet.emptySet(valueComparator);
}
private static ImmutableSet.Builder valuesBuilder(
@Nullable Comparator super V> valueComparator) {
return (valueComparator == null)
? new ImmutableSet.Builder()
: new ImmutableSortedSet.Builder(valueComparator);
}
/**
* @serialData number of distinct keys, and then for each distinct key: the
* key, the number of values for that key, and the key's values
*/
@GwtIncompatible // java.io.ObjectOutputStream
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
stream.writeObject(valueComparator());
Serialization.writeMultimap(this, stream);
}
@Nullable
Comparator super V> valueComparator() {
return emptySet instanceof ImmutableSortedSet
? ((ImmutableSortedSet) emptySet).comparator()
: null;
}
@GwtIncompatible // java.io.ObjectInputStream
// Serialization type safety is at the caller's mercy.
@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
Comparator