source: josm/trunk/src/org/openstreetmap/josm/tools/MultiMap.java@ 7206

Last change on this file since 7206 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

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