001    /*
002     * Copyright (C) 2007 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.collect;
018    
019    import static com.google.common.base.Preconditions.checkArgument;
020    
021    import com.google.common.annotations.GwtCompatible;
022    import com.google.common.annotations.GwtIncompatible;
023    import com.google.common.annotations.VisibleForTesting;
024    
025    import java.io.IOException;
026    import java.io.ObjectInputStream;
027    import java.io.ObjectOutputStream;
028    import java.util.ArrayList;
029    import java.util.Collection;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    /**
035     * Implementation of {@code Multimap} that uses an {@code ArrayList} to store
036     * the values for a given key. A {@link HashMap} associates each key with an
037     * {@link ArrayList} of values.
038     *
039     * <p>When iterating through the collections supplied by this class, the
040     * ordering of values for a given key agrees with the order in which the values
041     * were added.
042     *
043     * <p>This multimap allows duplicate key-value pairs. After adding a new
044     * key-value pair equal to an existing key-value pair, the {@code
045     * ArrayListMultimap} will contain entries for both the new value and the old
046     * value.
047     *
048     * <p>Keys and values may be null. All optional multimap methods are supported,
049     * and all returned views are modifiable.
050     *
051     * <p>The lists returned by {@link #get}, {@link #removeAll}, and {@link
052     * #replaceValues} all implement {@link java.util.RandomAccess}.
053     *
054     * <p>This class is not threadsafe when any concurrent operations update the
055     * multimap. Concurrent read operations will work correctly. To allow concurrent
056     * update operations, wrap your multimap with a call to {@link
057     * Multimaps#synchronizedListMultimap}.
058     *
059     * @author Jared Levy
060     * @since 2.0 (imported from Google Collections Library)
061     */
062    @GwtCompatible(serializable = true, emulated = true)
063    public final class ArrayListMultimap<K, V> extends AbstractListMultimap<K, V> {
064      // Default from ArrayList
065      private static final int DEFAULT_VALUES_PER_KEY = 10;
066    
067      @VisibleForTesting transient int expectedValuesPerKey;
068    
069      /**
070       * Creates a new, empty {@code ArrayListMultimap} with the default initial
071       * capacities.
072       */
073      public static <K, V> ArrayListMultimap<K, V> create() {
074        return new ArrayListMultimap<K, V>();
075      }
076    
077      /**
078       * Constructs an empty {@code ArrayListMultimap} with enough capacity to hold
079       * the specified numbers of keys and values without resizing.
080       *
081       * @param expectedKeys the expected number of distinct keys
082       * @param expectedValuesPerKey the expected average number of values per key
083       * @throws IllegalArgumentException if {@code expectedKeys} or {@code
084       *      expectedValuesPerKey} is negative
085       */
086      public static <K, V> ArrayListMultimap<K, V> create(
087          int expectedKeys, int expectedValuesPerKey) {
088        return new ArrayListMultimap<K, V>(expectedKeys, expectedValuesPerKey);
089      }
090    
091      /**
092       * Constructs an {@code ArrayListMultimap} with the same mappings as the
093       * specified multimap.
094       *
095       * @param multimap the multimap whose contents are copied to this multimap
096       */
097      public static <K, V> ArrayListMultimap<K, V> create(
098          Multimap<? extends K, ? extends V> multimap) {
099        return new ArrayListMultimap<K, V>(multimap);
100      }
101    
102      private ArrayListMultimap() {
103        super(new HashMap<K, Collection<V>>());
104        expectedValuesPerKey = DEFAULT_VALUES_PER_KEY;
105      }
106    
107      private ArrayListMultimap(int expectedKeys, int expectedValuesPerKey) {
108        super(Maps.<K, Collection<V>>newHashMapWithExpectedSize(expectedKeys));
109        checkArgument(expectedValuesPerKey >= 0);
110        this.expectedValuesPerKey = expectedValuesPerKey;
111      }
112    
113      private ArrayListMultimap(Multimap<? extends K, ? extends V> multimap) {
114        this(multimap.keySet().size(),
115            (multimap instanceof ArrayListMultimap) ?
116                ((ArrayListMultimap<?, ?>) multimap).expectedValuesPerKey :
117                DEFAULT_VALUES_PER_KEY);
118        putAll(multimap);
119      }
120    
121      /**
122       * Creates a new, empty {@code ArrayList} to hold the collection of values for
123       * an arbitrary key.
124       */
125      @Override List<V> createCollection() {
126        return new ArrayList<V>(expectedValuesPerKey);
127      }
128    
129      /**
130       * Reduces the memory used by this {@code ArrayListMultimap}, if feasible.
131       */
132      public void trimToSize() {
133        for (Collection<V> collection : backingMap().values()) {
134          ArrayList<V> arrayList = (ArrayList<V>) collection;
135          arrayList.trimToSize();
136        }
137      }
138    
139      /**
140       * @serialData expectedValuesPerKey, number of distinct keys, and then for
141       *     each distinct key: the key, number of values for that key, and the
142       *     key's values
143       */
144      @GwtIncompatible("java.io.ObjectOutputStream")
145      private void writeObject(ObjectOutputStream stream) throws IOException {
146        stream.defaultWriteObject();
147        stream.writeInt(expectedValuesPerKey);
148        Serialization.writeMultimap(this, stream);
149      }
150    
151      @GwtIncompatible("java.io.ObjectOutputStream")
152      private void readObject(ObjectInputStream stream)
153          throws IOException, ClassNotFoundException {
154        stream.defaultReadObject();
155        expectedValuesPerKey = stream.readInt();
156        int distinctKeys = Serialization.readCount(stream);
157        Map<K, Collection<V>> map = Maps.newHashMapWithExpectedSize(distinctKeys);
158        setMap(map);
159        Serialization.populateMultimap(this, stream, distinctKeys);
160      }
161    
162      @GwtIncompatible("Not needed in emulated source.")
163      private static final long serialVersionUID = 0;
164    }