All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.util.collect.ImmutableSetMultimap Maven / Gradle / Ivy

There is a newer version: 8.15.1
Show newest version
/*
 * Copyright (C) 2009 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 org.elasticsearch.util.collect;

import org.elasticsearch.util.annotations.GwtCompatible;

import javax.annotation.Nullable;
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 static org.elasticsearch.util.base.Preconditions.*;

/**
 * An immutable {@link SetMultimap} with reliable user-specified key and value
 * iteration order. Does not permit null keys or values.
 *
 * 

Unlike {@link Multimaps#unmodifiableSetMultimap(SetMultimap)}, which is * a view of a separate multimap which can still change, an instance of * {@code ImmutableSetMultimap} contains its own data and will never * change. {@code ImmutableSetMultimap} 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 Mike Ward */ @GwtCompatible(serializable = true) public class ImmutableSetMultimap extends ImmutableMultimap implements SetMultimap { /** 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(); } /** * Multimap for {@link ImmutableSetMultimap.Builder} that maintains key * and value orderings and performs better than {@link LinkedHashMultimap}. */ private static class BuilderMultimap extends AbstractMultimap { BuilderMultimap() { super(new LinkedHashMap>()); } @Override Collection createCollection() { return Sets.newLinkedHashSet(); } private static final long serialVersionUID = 0; } /** * 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. */ public static final class Builder extends ImmutableMultimap.Builder { private final Multimap builderMultimap = new BuilderMultimap(); /** * Creates a new builder. The returned builder is equivalent to the builder * generated by {@link ImmutableSetMultimap#builder}. */ public Builder() {} /** * Adds a key-value mapping to the built multimap if it is not already * present. */ @Override 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. */ @Override public Builder putAll(K key, Iterable values) { Collection collection = builderMultimap.get(checkNotNull(key)); for (V value : values) { collection.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. */ @Override 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. */ @Override public Builder putAll( Multimap multimap) { for (Map.Entry> entry : multimap.asMap().entrySet()) { putAll(entry.getKey(), entry.getValue()); } return this; } /** * Returns a newly-created immutable set multimap. */ @Override public ImmutableSetMultimap build() { return copyOf(builderMultimap); } } /** * 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. * *

Note: Despite what the method name suggests, if * {@code multimap} is an {@code ImmutableSetMultimap}, 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 ImmutableSetMultimap copyOf( Multimap multimap) { if (multimap.isEmpty()) { return of(); } if (multimap instanceof ImmutableSetMultimap) { @SuppressWarnings("unchecked") // safe since multimap is not writable ImmutableSetMultimap kvMultimap = (ImmutableSetMultimap) multimap; return kvMultimap; } ImmutableMap.Builder> builder = ImmutableMap.builder(); int size = 0; for (Map.Entry> entry : multimap.asMap().entrySet()) { K key = entry.getKey(); Collection values = entry.getValue(); ImmutableSet set = ImmutableSet.copyOf(values); if (!set.isEmpty()) { builder.put(key, set); size += set.size(); } } return new ImmutableSetMultimap(builder.build(), size); } ImmutableSetMultimap(ImmutableMap> map, int size) { super(map, size); } // 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 (set == null) ? ImmutableSet.of() : set; } /** * Guaranteed to throw an exception and leave the multimap unmodified. * * @throws UnsupportedOperationException always */ @Override public ImmutableSet removeAll(Object key) { throw new UnsupportedOperationException(); } /** * Guaranteed to throw an exception and leave the multimap unmodified. * * @throws UnsupportedOperationException always */ @Override public ImmutableSet replaceValues( K key, Iterable 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. */ // TODO: Fix this so that two copies of the entries are not created. @Override public ImmutableSet> entries() { ImmutableSet> result = entries; return (result == null) ? (entries = ImmutableSet.copyOf(super.entries())) : result; } /** * @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(); } ImmutableSet valueSet = ImmutableSet.of(array); if (valueSet.size() != array.length) { throw new InvalidObjectException( "Duplicate key-value pairs exist for key " + key); } builder.put(key, valueSet); 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; }