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