| 1 | // License: GPL. See LICENSE file for details. |
|---|
| 2 | package org.openstreetmap.josm.tools; |
|---|
| 3 | |
|---|
| 4 | import java.util.ArrayList; |
|---|
| 5 | import java.util.Collection; |
|---|
| 6 | import java.util.HashMap; |
|---|
| 7 | import java.util.LinkedHashSet; |
|---|
| 8 | import java.util.List; |
|---|
| 9 | import java.util.Map; |
|---|
| 10 | import java.util.Map.Entry; |
|---|
| 11 | import java.util.Set; |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * MultiMap - maps keys to multiple values |
|---|
| 15 | * |
|---|
| 16 | * Corresponds to Google guava LinkedHashMultimap and Apache Collections MultiValueMap |
|---|
| 17 | * but it is an independent (simple) implementation. |
|---|
| 18 | * |
|---|
| 19 | */ |
|---|
| 20 | public class MultiMap<A, B> { |
|---|
| 21 | |
|---|
| 22 | private final Map<A, LinkedHashSet<B>> map; |
|---|
| 23 | |
|---|
| 24 | public MultiMap() { |
|---|
| 25 | map = new HashMap<A, LinkedHashSet<B>>(); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | public MultiMap(int capacity) { |
|---|
| 29 | map = new HashMap<A, LinkedHashSet<B>>(capacity); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * Map a key to a value. |
|---|
| 34 | * |
|---|
| 35 | * Can be called multiple times with the same key, but different value. |
|---|
| 36 | */ |
|---|
| 37 | public void put(A key, B value) { |
|---|
| 38 | LinkedHashSet<B> vals = map.get(key); |
|---|
| 39 | if (vals == null) { |
|---|
| 40 | vals = new LinkedHashSet<B>(); |
|---|
| 41 | map.put(key, vals); |
|---|
| 42 | } |
|---|
| 43 | vals.add(value); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Put a key that maps to nothing. (Only if it is not already in the map) |
|---|
| 48 | * |
|---|
| 49 | * Afterwards containsKey(key) will return true and get(key) will return |
|---|
| 50 | * an empty Set instead of null. |
|---|
| 51 | */ |
|---|
| 52 | public void putVoid(A key) { |
|---|
| 53 | if (map.containsKey(key)) |
|---|
| 54 | return; |
|---|
| 55 | map.put(key, new LinkedHashSet<B>()); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * Map the key to all the given values. |
|---|
| 60 | * |
|---|
| 61 | * Adds to the mappings that are already there. |
|---|
| 62 | */ |
|---|
| 63 | public void putAll(A key, Collection<B> values) { |
|---|
| 64 | LinkedHashSet<B> vals = map.get(key); |
|---|
| 65 | if (vals == null) { |
|---|
| 66 | vals = new LinkedHashSet<B>(values); |
|---|
| 67 | map.put(key, vals); |
|---|
| 68 | } |
|---|
| 69 | vals.addAll(values); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * Get the keySet. |
|---|
| 74 | */ |
|---|
| 75 | public Set<A> keySet() { |
|---|
| 76 | return map.keySet(); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * Returns the Set associated with the given key. Result is null if |
|---|
| 81 | * nothing has been mapped to this key. |
|---|
| 82 | * |
|---|
| 83 | * Modifications of the returned list changes the underling map, |
|---|
| 84 | * but you should better not do that. |
|---|
| 85 | */ |
|---|
| 86 | public Set<B> get(A key) { |
|---|
| 87 | return map.get(key); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * Like get, but returns an empty Set if nothing has been mapped to the key. |
|---|
| 92 | */ |
|---|
| 93 | public LinkedHashSet<B> getValues(A key) { |
|---|
| 94 | if (!map.containsKey(key)) |
|---|
| 95 | return new LinkedHashSet<B>(); |
|---|
| 96 | return map.get(key); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public boolean isEmpty() { |
|---|
| 100 | return map.isEmpty(); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | public boolean containsKey(A key) { |
|---|
| 104 | return map.containsKey(key); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * Returns true if the multimap contains a value for a key. |
|---|
| 109 | * |
|---|
| 110 | * @param key The key |
|---|
| 111 | * @param value The value |
|---|
| 112 | * @return true if the key contains the value |
|---|
| 113 | */ |
|---|
| 114 | public boolean contains(A key, B value) { |
|---|
| 115 | Set<B> values = get(key); |
|---|
| 116 | return (values == null) ? false : values.contains(value); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | public void clear() { |
|---|
| 120 | map.clear(); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | public Set<Entry<A, LinkedHashSet<B>>> entrySet() { |
|---|
| 124 | return map.entrySet(); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Returns the number of keys. |
|---|
| 129 | */ |
|---|
| 130 | public int size() { |
|---|
| 131 | return map.size(); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * Returns a collection of all value sets. |
|---|
| 136 | */ |
|---|
| 137 | public Collection<LinkedHashSet<B>> values() { |
|---|
| 138 | return map.values(); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | * Removes a cerain key=value mapping. |
|---|
| 143 | * |
|---|
| 144 | * @return true, if something was removed |
|---|
| 145 | */ |
|---|
| 146 | public boolean remove(A key, B value) { |
|---|
| 147 | Set<B> values = get(key); |
|---|
| 148 | if (values != null) { |
|---|
| 149 | return values.remove(value); |
|---|
| 150 | } |
|---|
| 151 | return false; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * Removes all mappings for a certain key. |
|---|
| 156 | */ |
|---|
| 157 | public LinkedHashSet<B> remove(A key) { |
|---|
| 158 | return map.remove(key); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | public String toString() { |
|---|
| 162 | List<String> entries = new ArrayList<String>(map.size()); |
|---|
| 163 | for (A key : map.keySet()) { |
|---|
| 164 | entries.add(key + "->{" + Utils.join(",", map.get(key)) + "}"); |
|---|
| 165 | } |
|---|
| 166 | return "(" + Utils.join(",", entries) + ")"; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|