Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.google.common.collect.ImmutableMultimap Maven / Gradle / Ivy
Go to download
Google Collections Library is a suite of new collections and collection-related goodness for Java 5.0
/*
* Copyright (C) 2008 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.annotations.GwtCompatible;
import static com.google.common.base.Preconditions.checkNotNull;
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.LinkedHashMap;
import java.util.Map;
import javax.annotation.Nullable;
/**
* An immutable {@link ListMultimap} with reliable user-specified key and value
* iteration order. Does not permit null keys or values.
*
* Unlike {@link Multimaps#unmodifiableListMultimap(ListMultimap)}, which is
* a view of a separate multimap which can still change, an instance of
* {@code ImmutableMultimap} contains its own data and will never
* change. {@code ImmutableMultimap} is convenient for
* {@code public static final} multimaps ("constant multimaps") and also lets
* you easily make a "defensive copy" of a multimap provided to your class by
* a caller.
*
*
Note : Although this class is not final, it cannot be subclassed as
* it has no public or protected constructors. Thus, instances of this class
* are guaranteed to be immutable.
*
* @author Jared Levy
*/
@GwtCompatible
public class ImmutableMultimap
extends AbstractImmutableMultimap>
implements ListMultimap {
private static ImmutableMultimap EMPTY_MULTIMAP
= new EmptyMultimap();
private static class EmptyMultimap extends ImmutableMultimap {
EmptyMultimap() {
super(ImmutableMap.>of(), 0);
}
Object readResolve() {
return EMPTY_MULTIMAP; // preserve singleton property
}
private static final long serialVersionUID = 0;
}
/** Returns the empty multimap. */
// Casting is safe because the multimap will never hold any elements.
@SuppressWarnings("unchecked")
public static ImmutableMultimap of() {
return (ImmutableMultimap) EMPTY_MULTIMAP;
}
/**
* Returns an immutable multimap containing a single entry.
*/
public static ImmutableMultimap of(K k1, V v1) {
ImmutableMultimap.Builder builder = ImmutableMultimap.builder();
builder.put(k1, v1);
return builder.build();
}
/**
* Returns an immutable multimap containing the given entries, in order.
*/
public static ImmutableMultimap of(K k1, V v1, K k2, V v2) {
ImmutableMultimap.Builder builder = ImmutableMultimap.builder();
builder.put(k1, v1);
builder.put(k2, v2);
return builder.build();
}
/**
* Returns an immutable multimap containing the given entries, in order.
*/
public static ImmutableMultimap of(
K k1, V v1, K k2, V v2, K k3, V v3) {
ImmutableMultimap.Builder builder = ImmutableMultimap.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.
*/
public static ImmutableMultimap of(
K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
ImmutableMultimap.Builder builder = ImmutableMultimap.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.
*/
public static ImmutableMultimap of(
K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
ImmutableMultimap.Builder builder = ImmutableMultimap.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 builder. The generated builder is equivalent to the builder
* created by the {@link Builder} constructor.
*/
public static Builder builder() {
return new Builder();
}
/**
* Multimap for {@link ImmutableMultimap.Builder} that maintains key and
* value orderings, allows duplicate values, and performs better than
* {@link LinkedListMultimap}.
*/
private static class BuilderMultimap extends StandardMultimap {
BuilderMultimap() {
super(new LinkedHashMap>());
}
@Override Collection createCollection() {
return Lists.newArrayList();
}
private static final long serialVersionUID = 0;
}
/**
* A builder for creating immutable multimap instances, especially
* {@code public static final} multimaps ("constant multimaps"). Example:
* {@code
*
* static final Multimap STRING_TO_INTEGER_MULTIMAP =
* new ImmutableMultimap.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.
*/
public static class Builder {
private final Multimap builderMultimap = new BuilderMultimap();
/**
* Creates a new builder. The returned builder is equivalent to the builder
* generated by {@link ImmutableMultimap#builder}.
*/
public Builder() {}
/**
* Adds a key-value mapping to the built multimap.
*/
public Builder put(K key, V value) {
builderMultimap.put(checkNotNull(key), checkNotNull(value));
return this;
}
/**
* Stores a collection of values with the same key in the built multimap.
*
* @throws NullPointerException if {@code key}, {@code values}, or any
* element in {@code values} is null. The builder is left in an invalid
* state.
*/
public Builder putAll(K key, Iterable extends V> values) {
Collection valueList = builderMultimap.get(checkNotNull(key));
for (V value : values) {
valueList.add(checkNotNull(value));
}
return this;
}
/**
* Stores an array of values with the same key in the built multimap.
*
* @throws NullPointerException if the key or any value is null. The builder
* is left in an invalid state.
*/
public Builder putAll(K key, V... values) {
return putAll(key, Arrays.asList(values));
}
/**
* Stores another multimap's entries in the built multimap. The generated
* multimap's key and value orderings correspond to the iteration ordering
* of the {@code multimap.asMap()} view, with new keys and values following
* any existing keys and values.
*
* @throws NullPointerException if any key or value in {@code multimap} is
* null. The builder is left in an invalid state.
*/
public Builder putAll(Multimap extends K, ? extends V> multimap) {
for (Map.Entry extends K, ? extends Collection extends V>> entry
: multimap.asMap().entrySet()) {
putAll(entry.getKey(), entry.getValue());
}
return this;
}
/**
* Returns a newly-created immutable multimap.
*/
public ImmutableMultimap build() {
return copyOf(builderMultimap);
}
}
/**
* Returns an immutable 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.
*
* Note: Despite what the method name suggests, if
* {@code multimap} is an {@code ImmutableMultimap}, no copy will actually be
* performed, and the given multimap itself will be returned.
*
* @throws NullPointerException if any key or value in {@code multimap} is
* null
*/
public static ImmutableMultimap copyOf(
Multimap extends K, ? extends V> multimap) {
if (multimap.isEmpty()) {
return of();
}
if (multimap instanceof ImmutableMultimap) {
@SuppressWarnings("unchecked") // safe since multimap is not writable
ImmutableMultimap kvMultimap = (ImmutableMultimap) multimap;
return kvMultimap;
}
ImmutableMap.Builder> builder = ImmutableMap.builder();
int size = 0;
for (Map.Entry extends K, ? extends Collection extends V>> entry
: multimap.asMap().entrySet()) {
ImmutableList list = ImmutableList.copyOf(entry.getValue());
if (!list.isEmpty()) {
builder.put(entry.getKey(), list);
size += list.size();
}
}
return new ImmutableMultimap(builder.build(), size);
}
private ImmutableMultimap(ImmutableMap> map, int size) {
super(map, size);
}
// views
/**
* Returns an immutable list of the values for the given key. If no mappings
* in the multimap have the provided key, an empty immutable list is
* returned. The values are in the same order as the parameters used to build
* this multimap.
*/
public ImmutableList get(@Nullable K key) {
ImmutableList list = map.get(key);
return (list == null) ? ImmutableList.of() : list;
}
/**
* @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
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
Serialization.writeMultimap(this, stream);
}
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
int keyCount = stream.readInt();
if (keyCount < 0) {
throw new InvalidObjectException("Invalid key count " + keyCount);
}
ImmutableMap.Builder> builder
= ImmutableMap.builder();
int tmpSize = 0;
for (int i = 0; i < keyCount; i++) {
Object key = stream.readObject();
int valueCount = stream.readInt();
if (valueCount <= 0) {
throw new InvalidObjectException("Invalid value count " + valueCount);
}
Object[] array = new Object[valueCount];
for (int j = 0; j < valueCount; j++) {
array[j] = stream.readObject();
}
builder.put(key, ImmutableList.of(array));
tmpSize += valueCount;
}
ImmutableMap> tmpMap;
try {
tmpMap = builder.build();
} catch (IllegalArgumentException e) {
throw (InvalidObjectException)
new InvalidObjectException(e.getMessage()).initCause(e);
}
FieldSettersHolder.MAP_FIELD_SETTER.set(this, tmpMap);
FieldSettersHolder.SIZE_FIELD_SETTER.set(this, tmpSize);
}
private static final long serialVersionUID = 0;
}