[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[2702] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[4668] | 4 | import java.util.ArrayList;
|
---|
[3674] | 5 | import java.util.Collection;
|
---|
[9755] | 6 | import java.util.Collections;
|
---|
[2990] | 7 | import java.util.HashMap;
|
---|
[5822] | 8 | import java.util.LinkedHashSet;
|
---|
[4668] | 9 | import java.util.List;
|
---|
[3637] | 10 | import java.util.Map;
|
---|
[3674] | 11 | import java.util.Map.Entry;
|
---|
[9371] | 12 | import java.util.Objects;
|
---|
[3637] | 13 | import java.util.Set;
|
---|
[2702] | 14 |
|
---|
| 15 | /**
|
---|
[7275] | 16 | * MultiMap - maps keys to multiple values.
|
---|
[3674] | 17 | *
|
---|
| 18 | * Corresponds to Google guava LinkedHashMultimap and Apache Collections MultiValueMap
|
---|
[4668] | 19 | * but it is an independent (simple) implementation.
|
---|
[3674] | 20 | *
|
---|
[7275] | 21 | * @param <A> Key type
|
---|
| 22 | * @param <B> Value type
|
---|
| 23 | *
|
---|
| 24 | * @since 2702
|
---|
[2702] | 25 | */
|
---|
[3674] | 26 | public class MultiMap<A, B> {
|
---|
[3637] | 27 |
|
---|
[5783] | 28 | private final Map<A, Set<B>> map;
|
---|
[3674] | 29 |
|
---|
[7005] | 30 | /**
|
---|
| 31 | * Constructs a new {@code MultiMap}.
|
---|
| 32 | */
|
---|
[3674] | 33 | public MultiMap() {
|
---|
[7005] | 34 | map = new HashMap<>();
|
---|
[3674] | 35 | }
|
---|
| 36 |
|
---|
[7005] | 37 | /**
|
---|
[7275] | 38 | * Constructs a new {@code MultiMap} with the specified initial capacity.
|
---|
[7005] | 39 | * @param capacity the initial capacity
|
---|
| 40 | */
|
---|
[3674] | 41 | public MultiMap(int capacity) {
|
---|
[7005] | 42 | map = new HashMap<>(capacity);
|
---|
[3674] | 43 | }
|
---|
| 44 |
|
---|
[2946] | 45 | /**
|
---|
[9755] | 46 | * Constructs a new {@code MultiMap} from an ordinary {@code Map}.
|
---|
| 47 | * @param map0 the {@code Map}
|
---|
| 48 | */
|
---|
| 49 | public MultiMap(Map<A, Set<B>> map0) {
|
---|
| 50 | if (map0 == null) {
|
---|
| 51 | map = new HashMap<>();
|
---|
| 52 | } else {
|
---|
| 53 | map = new HashMap<>(Utils.hashMapInitialCapacity(map0.size()));
|
---|
| 54 | for (Entry<A, Set<B>> e : map0.entrySet()) {
|
---|
| 55 | map.put(e.getKey(), new LinkedHashSet<>(e.getValue()));
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
[4685] | 61 | * Map a key to a value.
|
---|
| 62 | *
|
---|
| 63 | * Can be called multiple times with the same key, but different value.
|
---|
[7275] | 64 | * @param key key with which the specified value is to be associated
|
---|
| 65 | * @param value value to be associated with the specified key
|
---|
[2946] | 66 | */
|
---|
| 67 | public void put(A key, B value) {
|
---|
[5783] | 68 | Set<B> vals = map.get(key);
|
---|
[2702] | 69 | if (vals == null) {
|
---|
[7005] | 70 | vals = new LinkedHashSet<>();
|
---|
[3637] | 71 | map.put(key, vals);
|
---|
[2702] | 72 | }
|
---|
| 73 | vals.add(value);
|
---|
| 74 | }
|
---|
[2990] | 75 |
|
---|
[2946] | 76 | /**
|
---|
[3674] | 77 | * Put a key that maps to nothing. (Only if it is not already in the map)
|
---|
[4685] | 78 | *
|
---|
[3674] | 79 | * Afterwards containsKey(key) will return true and get(key) will return
|
---|
| 80 | * an empty Set instead of null.
|
---|
[7275] | 81 | * @param key key with which an empty set is to be associated
|
---|
[2946] | 82 | */
|
---|
| 83 | public void putVoid(A key) {
|
---|
[3637] | 84 | if (map.containsKey(key))
|
---|
[2946] | 85 | return;
|
---|
[5822] | 86 | map.put(key, new LinkedHashSet<B>());
|
---|
[2946] | 87 | }
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
[4685] | 90 | * Map the key to all the given values.
|
---|
| 91 | *
|
---|
| 92 | * Adds to the mappings that are already there.
|
---|
[7275] | 93 | * @param key key with which the specified values are to be associated
|
---|
| 94 | * @param values values to be associated with the specified key
|
---|
[2946] | 95 | */
|
---|
[4685] | 96 | public void putAll(A key, Collection<B> values) {
|
---|
[5783] | 97 | Set<B> vals = map.get(key);
|
---|
[4685] | 98 | if (vals == null) {
|
---|
[7005] | 99 | vals = new LinkedHashSet<>(values);
|
---|
[4685] | 100 | map.put(key, vals);
|
---|
| 101 | }
|
---|
| 102 | vals.addAll(values);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * Get the keySet.
|
---|
[7275] | 107 | * @return a set view of the keys contained in this map
|
---|
| 108 | * @see Map#keySet()
|
---|
[4685] | 109 | */
|
---|
[3674] | 110 | public Set<A> keySet() {
|
---|
| 111 | return map.keySet();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
[4685] | 115 | * Returns the Set associated with the given key. Result is null if
|
---|
| 116 | * nothing has been mapped to this key.
|
---|
| 117 | *
|
---|
| 118 | * Modifications of the returned list changes the underling map,
|
---|
| 119 | * but you should better not do that.
|
---|
[7275] | 120 | * @param key the key whose associated value is to be returned
|
---|
| 121 | * @return the set of values to which the specified key is mapped, or {@code null} if this map contains no mapping for the key
|
---|
| 122 | * @see Map#get(Object)
|
---|
[3674] | 123 | */
|
---|
| 124 | public Set<B> get(A key) {
|
---|
| 125 | return map.get(key);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * Like get, but returns an empty Set if nothing has been mapped to the key.
|
---|
[7275] | 130 | * @param key the key whose associated value is to be returned
|
---|
| 131 | * @return the set of values to which the specified key is mapped, or an empty set if this map contains no mapping for the key
|
---|
[3674] | 132 | */
|
---|
[5783] | 133 | public Set<B> getValues(A key) {
|
---|
[3637] | 134 | if (!map.containsKey(key))
|
---|
[7005] | 135 | return new LinkedHashSet<>();
|
---|
[3637] | 136 | return map.get(key);
|
---|
[2946] | 137 | }
|
---|
[3637] | 138 |
|
---|
[7275] | 139 | /**
|
---|
| 140 | * Returns {@code true} if this map contains no key-value mappings.
|
---|
| 141 | * @return {@code true} if this map contains no key-value mappings
|
---|
| 142 | * @see Map#isEmpty()
|
---|
| 143 | */
|
---|
[3674] | 144 | public boolean isEmpty() {
|
---|
| 145 | return map.isEmpty();
|
---|
[3637] | 146 | }
|
---|
| 147 |
|
---|
[7275] | 148 | /**
|
---|
| 149 | * Returns {@code true} if this map contains a mapping for the specified key.
|
---|
| 150 | * @param key key whose presence in this map is to be tested
|
---|
| 151 | * @return {@code true} if this map contains a mapping for the specified key
|
---|
| 152 | * @see Map#containsKey(Object)
|
---|
| 153 | */
|
---|
[3674] | 154 | public boolean containsKey(A key) {
|
---|
| 155 | return map.containsKey(key);
|
---|
[3637] | 156 | }
|
---|
[3674] | 157 |
|
---|
| 158 | /**
|
---|
[4685] | 159 | * Returns true if the multimap contains a value for a key.
|
---|
| 160 | *
|
---|
[3674] | 161 | * @param key The key
|
---|
| 162 | * @param value The value
|
---|
| 163 | * @return true if the key contains the value
|
---|
| 164 | */
|
---|
| 165 | public boolean contains(A key, B value) {
|
---|
| 166 | Set<B> values = get(key);
|
---|
[9074] | 167 | return values != null && values.contains(value);
|
---|
[3674] | 168 | }
|
---|
| 169 |
|
---|
[7275] | 170 | /**
|
---|
| 171 | * Removes all of the mappings from this map. The map will be empty after this call returns.
|
---|
| 172 | * @see Map#clear()
|
---|
| 173 | */
|
---|
[3674] | 174 | public void clear() {
|
---|
| 175 | map.clear();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[7275] | 178 | /**
|
---|
| 179 | * Returns a Set view of the mappings contained in this map.
|
---|
| 180 | * The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
|
---|
| 181 | * @return a set view of the mappings contained in this map
|
---|
| 182 | * @see Map#entrySet()
|
---|
| 183 | */
|
---|
[5783] | 184 | public Set<Entry<A, Set<B>>> entrySet() {
|
---|
[3674] | 185 | return map.entrySet();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /**
|
---|
[4685] | 189 | * Returns the number of keys.
|
---|
[7275] | 190 | * @return the number of key-value mappings in this map
|
---|
| 191 | * @see Map#size()
|
---|
[3674] | 192 | */
|
---|
| 193 | public int size() {
|
---|
| 194 | return map.size();
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
[4685] | 198 | * Returns a collection of all value sets.
|
---|
[7275] | 199 | * @return a collection view of the values contained in this map
|
---|
| 200 | * @see Map#values()
|
---|
[3674] | 201 | */
|
---|
[5783] | 202 | public Collection<Set<B>> values() {
|
---|
[3674] | 203 | return map.values();
|
---|
| 204 | }
|
---|
[4668] | 205 |
|
---|
| 206 | /**
|
---|
[7275] | 207 | * Removes a certain key=value mapping.
|
---|
| 208 | * @param key key whose mapping is to be removed from the map
|
---|
| 209 | * @param value value whose mapping is to be removed from the map
|
---|
[4668] | 210 | *
|
---|
[7275] | 211 | * @return {@code true}, if something was removed
|
---|
[4668] | 212 | */
|
---|
| 213 | public boolean remove(A key, B value) {
|
---|
| 214 | Set<B> values = get(key);
|
---|
| 215 | if (values != null) {
|
---|
| 216 | return values.remove(value);
|
---|
| 217 | }
|
---|
| 218 | return false;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /**
|
---|
[4685] | 222 | * Removes all mappings for a certain key.
|
---|
[7275] | 223 | * @param key key whose mapping is to be removed from the map
|
---|
| 224 | * @return the previous value associated with key, or {@code null} if there was no mapping for key.
|
---|
| 225 | * @see Map#remove(Object)
|
---|
[4668] | 226 | */
|
---|
[5783] | 227 | public Set<B> remove(A key) {
|
---|
[4668] | 228 | return map.remove(key);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[5783] | 231 | @Override
|
---|
[7276] | 232 | public int hashCode() {
|
---|
[9371] | 233 | return Objects.hash(map);
|
---|
[7276] | 234 | }
|
---|
| 235 |
|
---|
[9755] | 236 | /**
|
---|
| 237 | * Converts this {@code MultiMap} to a {@code Map} with {@code Set} values.
|
---|
| 238 | * @return the converted {@code Map}
|
---|
| 239 | */
|
---|
| 240 | public Map<A, Set<B>> toMap() {
|
---|
| 241 | Map<A, Set<B>> result = new HashMap<>();
|
---|
| 242 | for (Entry<A, Set<B>> e : map.entrySet()) {
|
---|
| 243 | result.put(e.getKey(), Collections.unmodifiableSet(e.getValue()));
|
---|
| 244 | }
|
---|
| 245 | return result;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[7276] | 248 | @Override
|
---|
| 249 | public boolean equals(Object obj) {
|
---|
[9371] | 250 | if (this == obj) return true;
|
---|
| 251 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
| 252 | MultiMap<?, ?> multiMap = (MultiMap<?, ?>) obj;
|
---|
| 253 | return Objects.equals(map, multiMap.map);
|
---|
[7276] | 254 | }
|
---|
| 255 |
|
---|
| 256 | @Override
|
---|
[4668] | 257 | public String toString() {
|
---|
[7005] | 258 | List<String> entries = new ArrayList<>(map.size());
|
---|
[8332] | 259 | for (Entry<A, Set<B>> entry : map.entrySet()) {
|
---|
[8846] | 260 | entries.add(entry.getKey() + "->{" + Utils.join(",", entry.getValue()) + '}');
|
---|
[4668] | 261 | }
|
---|
[8846] | 262 | return '(' + Utils.join(",", entries) + ')';
|
---|
[4668] | 263 | }
|
---|
[2702] | 264 | }
|
---|