AbstractListMultimap.java revision 7dd252788645e940eada959bdde927426e2531c9
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 17package com.google.common.collect; 18 19import com.google.common.annotations.GwtCompatible; 20 21import java.util.Collection; 22import java.util.List; 23import java.util.Map; 24 25import javax.annotation.Nullable; 26 27/** 28 * Basic implementation of the {@link ListMultimap} interface. It's a wrapper 29 * around {@link AbstractMapBasedMultimap} that converts the returned collections into 30 * {@code Lists}. The {@link #createCollection} method must return a {@code 31 * List}. 32 * 33 * @author Jared Levy 34 * @since 2.0 (imported from Google Collections Library) 35 */ 36@GwtCompatible 37abstract class AbstractListMultimap<K, V> extends AbstractMapBasedMultimap<K, V> implements 38 ListMultimap<K, V> { 39 /** 40 * Creates a new multimap that uses the provided map. 41 * 42 * @param map place to store the mapping from each key to its corresponding 43 * values 44 */ 45 protected AbstractListMultimap(Map<K, Collection<V>> map) { 46 super(map); 47 } 48 49 @Override 50 abstract List<V> createCollection(); 51 52 @Override 53 List<V> createUnmodifiableEmptyCollection() { 54 return ImmutableList.of(); 55 } 56 57 // Following Javadoc copied from ListMultimap. 58 59 /** 60 * {@inheritDoc} 61 * 62 * <p>Because the values for a given key may have duplicates and follow the 63 * insertion ordering, this method returns a {@link List}, instead of the 64 * {@link Collection} specified in the {@link Multimap} interface. 65 */ 66 @Override 67 public List<V> get(@Nullable K key) { 68 return (List<V>) super.get(key); 69 } 70 71 /** 72 * {@inheritDoc} 73 * 74 * <p>Because the values for a given key may have duplicates and follow the 75 * insertion ordering, this method returns a {@link List}, instead of the 76 * {@link Collection} specified in the {@link Multimap} interface. 77 */ 78 @Override 79 public List<V> removeAll(@Nullable Object key) { 80 return (List<V>) super.removeAll(key); 81 } 82 83 /** 84 * {@inheritDoc} 85 * 86 * <p>Because the values for a given key may have duplicates and follow the 87 * insertion ordering, this method returns a {@link List}, instead of the 88 * {@link Collection} specified in the {@link Multimap} interface. 89 */ 90 @Override 91 public List<V> replaceValues(@Nullable K key, Iterable<? extends V> values) { 92 return (List<V>) super.replaceValues(key, values); 93 } 94 95 /** 96 * Stores a key-value pair in the multimap. 97 * 98 * @param key key to store in the multimap 99 * @param value value to store in the multimap 100 * @return {@code true} always 101 */ 102 @Override 103 public boolean put(@Nullable K key, @Nullable V value) { 104 return super.put(key, value); 105 } 106 107 /** 108 * {@inheritDoc} 109 * 110 * <p>Though the method signature doesn't say so explicitly, the returned map 111 * has {@link List} values. 112 */ 113 @Override 114 public Map<K, Collection<V>> asMap() { 115 return super.asMap(); 116 } 117 118 /** 119 * Compares the specified object to this multimap for equality. 120 * 121 * <p>Two {@code ListMultimap} instances are equal if, for each key, they 122 * contain the same values in the same order. If the value orderings disagree, 123 * the multimaps will not be considered equal. 124 */ 125 @Override 126 public boolean equals(@Nullable Object object) { 127 return super.equals(object); 128 } 129 130 private static final long serialVersionUID = 6588350623831699109L; 131} 132