Coverage Summary for Class: ArrayListMultimap (com.google.common.collect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| ArrayListMultimap | 100% (1/1) | 40% (4/10) | 25% (8/32) |
1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import static com.google.common.collect.CollectPreconditions.checkNonnegative; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.annotations.VisibleForTesting; 24 import java.io.IOException; 25 import java.io.ObjectInputStream; 26 import java.io.ObjectOutputStream; 27 import java.util.ArrayList; 28 import java.util.Collection; 29 import java.util.HashMap; 30 import java.util.List; 31 import java.util.Map; 32 33 /** 34 * Implementation of {@code Multimap} that uses an {@code ArrayList} to store the values for a given 35 * key. A {@link HashMap} associates each key with an {@link ArrayList} of values. 36 * 37 * <p>When iterating through the collections supplied by this class, the ordering of values for a 38 * given key agrees with the order in which the values were added. 39 * 40 * <p>This multimap allows duplicate key-value pairs. After adding a new key-value pair equal to an 41 * existing key-value pair, the {@code ArrayListMultimap} will contain entries for both the new 42 * value and the old value. 43 * 44 * <p>Keys and values may be null. All optional multimap methods are supported, and all returned 45 * views are modifiable. 46 * 47 * <p>The lists returned by {@link #get}, {@link #removeAll}, and {@link #replaceValues} all 48 * implement {@link java.util.RandomAccess}. 49 * 50 * <p>This class is not threadsafe when any concurrent operations update the multimap. Concurrent 51 * read operations will work correctly. To allow concurrent update operations, wrap your multimap 52 * with a call to {@link Multimaps#synchronizedListMultimap}. 53 * 54 * <p>See the Guava User Guide article on <a href= 55 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multimap"> {@code 56 * Multimap}</a>. 57 * 58 * @author Jared Levy 59 * @since 2.0 60 */ 61 @GwtCompatible(serializable = true, emulated = true) 62 public final class ArrayListMultimap<K, V> 63 extends ArrayListMultimapGwtSerializationDependencies<K, V> { 64 // Default from ArrayList 65 private static final int DEFAULT_VALUES_PER_KEY = 3; 66 67 @VisibleForTesting transient int expectedValuesPerKey; 68 69 /** 70 * Creates a new, empty {@code ArrayListMultimap} with the default initial capacities. 71 * 72 * <p>This method will soon be deprecated in favor of {@code 73 * MultimapBuilder.hashKeys().arrayListValues().build()}. 74 */ 75 public static <K, V> ArrayListMultimap<K, V> create() { 76 return new ArrayListMultimap<>(); 77 } 78 79 /** 80 * Constructs an empty {@code ArrayListMultimap} with enough capacity to hold the specified 81 * numbers of keys and values without resizing. 82 * 83 * <p>This method will soon be deprecated in favor of {@code 84 * MultimapBuilder.hashKeys(expectedKeys).arrayListValues(expectedValuesPerKey).build()}. 85 * 86 * @param expectedKeys the expected number of distinct keys 87 * @param expectedValuesPerKey the expected average number of values per key 88 * @throws IllegalArgumentException if {@code expectedKeys} or {@code expectedValuesPerKey} is 89 * negative 90 */ 91 public static <K, V> ArrayListMultimap<K, V> create(int expectedKeys, int expectedValuesPerKey) { 92 return new ArrayListMultimap<>(expectedKeys, expectedValuesPerKey); 93 } 94 95 /** 96 * Constructs an {@code ArrayListMultimap} with the same mappings as the specified multimap. 97 * 98 * <p>This method will soon be deprecated in favor of {@code 99 * MultimapBuilder.hashKeys().arrayListValues().build(multimap)}. 100 * 101 * @param multimap the multimap whose contents are copied to this multimap 102 */ 103 public static <K, V> ArrayListMultimap<K, V> create(Multimap<? extends K, ? extends V> multimap) { 104 return new ArrayListMultimap<>(multimap); 105 } 106 107 private ArrayListMultimap() { 108 this(12, DEFAULT_VALUES_PER_KEY); 109 } 110 111 private ArrayListMultimap(int expectedKeys, int expectedValuesPerKey) { 112 super(Platform.<K, Collection<V>>newHashMapWithExpectedSize(expectedKeys)); 113 checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey"); 114 this.expectedValuesPerKey = expectedValuesPerKey; 115 } 116 117 private ArrayListMultimap(Multimap<? extends K, ? extends V> multimap) { 118 this( 119 multimap.keySet().size(), 120 (multimap instanceof ArrayListMultimap) 121 ? ((ArrayListMultimap<?, ?>) multimap).expectedValuesPerKey 122 : DEFAULT_VALUES_PER_KEY); 123 putAll(multimap); 124 } 125 126 /** 127 * Creates a new, empty {@code ArrayList} to hold the collection of values for an arbitrary key. 128 */ 129 @Override 130 List<V> createCollection() { 131 return new ArrayList<V>(expectedValuesPerKey); 132 } 133 134 /** 135 * Reduces the memory used by this {@code ArrayListMultimap}, if feasible. 136 * 137 * @deprecated For a {@link ListMultimap} that automatically trims to size, use {@link 138 * ImmutableListMultimap}. If you need a mutable collection, remove the {@code trimToSize} 139 * call, or switch to a {@code HashMap<K, ArrayList<V>>}. 140 */ 141 @Deprecated 142 public void trimToSize() { 143 for (Collection<V> collection : backingMap().values()) { 144 ArrayList<V> arrayList = (ArrayList<V>) collection; 145 arrayList.trimToSize(); 146 } 147 } 148 149 /** 150 * @serialData expectedValuesPerKey, number of distinct keys, and then for each distinct key: the 151 * key, number of values for that key, and the key's values 152 */ 153 @GwtIncompatible // java.io.ObjectOutputStream 154 private void writeObject(ObjectOutputStream stream) throws IOException { 155 stream.defaultWriteObject(); 156 Serialization.writeMultimap(this, stream); 157 } 158 159 @GwtIncompatible // java.io.ObjectOutputStream 160 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 161 stream.defaultReadObject(); 162 expectedValuesPerKey = DEFAULT_VALUES_PER_KEY; 163 int distinctKeys = Serialization.readCount(stream); 164 Map<K, Collection<V>> map = Maps.newHashMap(); 165 setMap(map); 166 Serialization.populateMultimap(this, stream, distinctKeys); 167 } 168 169 @GwtIncompatible // Not needed in emulated source. 170 private static final long serialVersionUID = 0; 171 }