• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.annotations.GwtCompatible;
20 import com.google.errorprone.annotations.CanIgnoreReturnValue;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.Set;
26 import javax.annotation.CheckForNull;
27 import org.checkerframework.checker.nullness.qual.Nullable;
28 
29 /**
30  * Basic implementation of the {@link SetMultimap} interface. It's a wrapper around {@link
31  * AbstractMapBasedMultimap} that converts the returned collections into {@code Sets}. The {@link
32  * #createCollection} method must return a {@code Set}.
33  *
34  * @author Jared Levy
35  */
36 @GwtCompatible
37 @ElementTypesAreNonnullByDefault
38 abstract class AbstractSetMultimap<K extends @Nullable Object, V extends @Nullable Object>
39     extends AbstractMapBasedMultimap<K, V> implements SetMultimap<K, V> {
40   /**
41    * Creates a new multimap that uses the provided map.
42    *
43    * @param map place to store the mapping from each key to its corresponding values
44    */
AbstractSetMultimap(Map<K, Collection<V>> map)45   protected AbstractSetMultimap(Map<K, Collection<V>> map) {
46     super(map);
47   }
48 
49   @Override
createCollection()50   abstract Set<V> createCollection();
51 
52   @Override
createUnmodifiableEmptyCollection()53   Set<V> createUnmodifiableEmptyCollection() {
54     return Collections.emptySet();
55   }
56 
57   @Override
unmodifiableCollectionSubclass( Collection<E> collection)58   <E extends @Nullable Object> Collection<E> unmodifiableCollectionSubclass(
59       Collection<E> collection) {
60     return Collections.unmodifiableSet((Set<E>) collection);
61   }
62 
63   @Override
wrapCollection(@arametricNullness K key, Collection<V> collection)64   Collection<V> wrapCollection(@ParametricNullness K key, Collection<V> collection) {
65     return new WrappedSet(key, (Set<V>) collection);
66   }
67 
68   // Following Javadoc copied from SetMultimap.
69 
70   /**
71    * {@inheritDoc}
72    *
73    * <p>Because a {@code SetMultimap} has unique values for a given key, this method returns a
74    * {@link Set}, instead of the {@link Collection} specified in the {@link Multimap} interface.
75    */
76   @Override
get(@arametricNullness K key)77   public Set<V> get(@ParametricNullness K key) {
78     return (Set<V>) super.get(key);
79   }
80 
81   /**
82    * {@inheritDoc}
83    *
84    * <p>Because a {@code SetMultimap} has unique values for a given key, this method returns a
85    * {@link Set}, instead of the {@link Collection} specified in the {@link Multimap} interface.
86    */
87   @Override
entries()88   public Set<Entry<K, V>> entries() {
89     return (Set<Entry<K, V>>) super.entries();
90   }
91 
92   /**
93    * {@inheritDoc}
94    *
95    * <p>Because a {@code SetMultimap} has unique values for a given key, this method returns a
96    * {@link Set}, instead of the {@link Collection} specified in the {@link Multimap} interface.
97    */
98   @CanIgnoreReturnValue
99   @Override
removeAll(@heckForNull Object key)100   public Set<V> removeAll(@CheckForNull Object key) {
101     return (Set<V>) super.removeAll(key);
102   }
103 
104   /**
105    * {@inheritDoc}
106    *
107    * <p>Because a {@code SetMultimap} has unique values for a given key, this method returns a
108    * {@link Set}, instead of the {@link Collection} specified in the {@link Multimap} interface.
109    *
110    * <p>Any duplicates in {@code values} will be stored in the multimap once.
111    */
112   @CanIgnoreReturnValue
113   @Override
replaceValues(@arametricNullness K key, Iterable<? extends V> values)114   public Set<V> replaceValues(@ParametricNullness K key, Iterable<? extends V> values) {
115     return (Set<V>) super.replaceValues(key, values);
116   }
117 
118   /**
119    * {@inheritDoc}
120    *
121    * <p>Though the method signature doesn't say so explicitly, the returned map has {@link Set}
122    * values.
123    */
124   @Override
asMap()125   public Map<K, Collection<V>> asMap() {
126     return super.asMap();
127   }
128 
129   /**
130    * Stores a key-value pair in the multimap.
131    *
132    * @param key key to store in the multimap
133    * @param value value to store in the multimap
134    * @return {@code true} if the method increased the size of the multimap, or {@code false} if the
135    *     multimap already contained the key-value pair
136    */
137   @CanIgnoreReturnValue
138   @Override
put(@arametricNullness K key, @ParametricNullness V value)139   public boolean put(@ParametricNullness K key, @ParametricNullness V value) {
140     return super.put(key, value);
141   }
142 
143   /**
144    * Compares the specified object to this multimap for equality.
145    *
146    * <p>Two {@code SetMultimap} instances are equal if, for each key, they contain the same values.
147    * Equality does not depend on the ordering of keys or values.
148    */
149   @Override
equals(@heckForNull Object object)150   public boolean equals(@CheckForNull Object object) {
151     return super.equals(object);
152   }
153 
154   private static final long serialVersionUID = 7431625294878419160L;
155 }
156