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