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 * https://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 serialization.
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 *
91 * @return A new set
92 */
93 @Override
94 protected abstract Set<V> createCollection();
95
96 /**
97 * Gets the set of values associated with the specified key. This would
98 * return an empty set in case the mapping is not present
99 *
100 * @param key The key to retrieve
101 * @return The {@code Set} of values, will return an empty
102 * {@code Set} for no mapping
103 */
104 @Override
105 public Set<V> get(final K key) {
106 return wrappedCollection(key);
107 }
108
109 @Override
110 @SuppressWarnings("unchecked")
111 protected Map<K, Set<V>> getMap() {
112 return (Map<K, Set<V>>) super.getMap();
113 }
114
115 /**
116 * Removes all values associated with the specified key.
117 * <p>
118 * A subsequent {@code get(Object)} would return an empty set.
119 * </p>
120 *
121 * @param key The key to remove values from
122 * @return The {@code Set} of values removed, will return an empty,
123 * unmodifiable set for no mapping found.
124 */
125 @Override
126 public Set<V> remove(final Object key) {
127 return SetUtils.emptyIfNull(getMap().remove(key));
128 }
129
130 @Override
131 Set<V> wrappedCollection(final K key) {
132 return new WrappedSet(key);
133 }
134 }