1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections4.multimap;
18
19 import java.util.Collections;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.apache.commons.collections4.SetUtils;
24 import org.apache.commons.collections4.SetValuedMap;
25
26 /**
27 * Abstract implementation of the {@link SetValuedMap} interface to simplify the
28 * creation of subclass implementations.
29 * <p>
30 * Subclasses specify a Map implementation to use as the internal storage and
31 * the Set implementation to use as values.
32 * </p>
33 *
34 * @param <K> the type of the keys in this map
35 * @param <V> the type of the values in this map
36 * @since 4.1
37 */
38 public abstract class AbstractSetValuedMap<K, V> extends AbstractMultiValuedMap<K, V>
39 implements SetValuedMap<K, V> {
40
41 /**
42 * Wrapped set to handle add and remove on the collection returned by
43 * {@code get(Object)}.
44 */
45 private final class WrappedSet extends WrappedCollection implements Set<V> {
46
47 WrappedSet(final K key) {
48 super(key);
49 }
50
51 @Override
52 public boolean equals(final Object other) {
53 final Set<V> set = (Set<V>) getMapping();
54 if (set == null) {
55 return Collections.emptySet().equals(other);
56 }
57 if (!(other instanceof Set)) {
58 return false;
59 }
60 final Set<?> otherSet = (Set<?>) other;
61 return SetUtils.isEqualSet(set, otherSet);
62 }
63
64 @Override
65 public int hashCode() {
66 final Set<V> set = (Set<V>) getMapping();
67 return SetUtils.hashCodeForSet(set);
68 }
69
70 }
71
72 /**
73 * Constructor needed for subclass serialisation.
74 */
75 protected AbstractSetValuedMap() {
76 }
77
78 /**
79 * A constructor that wraps, not copies
80 *
81 * @param map the map to wrap, must not be null
82 * @throws NullPointerException if the map is null
83 */
84 protected AbstractSetValuedMap(final Map<K, ? extends Set<V>> map) {
85 super(map);
86 }
87
88 /**
89 * Creates a new value collection using the provided factory.
90 * @return a new set
91 */
92 @Override
93 protected abstract Set<V> createCollection();
94
95 /**
96 * Gets the set of values associated with the specified key. This would
97 * return an empty set in case the mapping is not present
98 *
99 * @param key the key to retrieve
100 * @return the {@code Set} of values, will return an empty
101 * {@code Set} for no mapping
102 */
103 @Override
104 public Set<V> get(final K key) {
105 return wrappedCollection(key);
106 }
107
108 @Override
109 @SuppressWarnings("unchecked")
110 protected Map<K, Set<V>> getMap() {
111 return (Map<K, Set<V>>) super.getMap();
112 }
113
114 /**
115 * Removes all values associated with the specified key.
116 * <p>
117 * A subsequent {@code get(Object)} would return an empty set.
118 * </p>
119 *
120 * @param key the key to remove values from
121 * @return the {@code Set} of values removed, will return an empty,
122 * unmodifiable set for no mapping found.
123 */
124 @Override
125 public Set<V> remove(final Object key) {
126 return SetUtils.emptyIfNull(getMap().remove(key));
127 }
128
129 @Override
130 Set<V> wrappedCollection(final K key) {
131 return new WrappedSet(key);
132 }
133 }