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

Last change on this file since 4668 was 4668, checked in by bastiK, 12 years ago

session support (first part, see #4029)

Idea: Save and load the current session, i.e. list of open layers and possibly more.
This change includes only support for reading session files and only for osm-data layers.

session.svg: Public Domain

  • Property svn:eol-style set to native
File size: 3.6 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, 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. Can be called multiple times with the same key, but different value.
34 */
35 public void put(A key, B value) {
36 LinkedHashSet<B> vals = map.get(key);
37 if (vals == null) {
38 vals = new LinkedHashSet<B>();
39 map.put(key, vals);
40 }
41 vals.add(value);
42 }
43
44 /**
45 * Put a key that maps to nothing. (Only if it is not already in the map)
46 * Afterwards containsKey(key) will return true and get(key) will return
47 * an empty Set instead of null.
48 */
49 public void putVoid(A key) {
50 if (map.containsKey(key))
51 return;
52 map.put(key, new LinkedHashSet<B>());
53 }
54
55 /**
56 * Get the keySet
57 */
58 public Set<A> keySet() {
59 return map.keySet();
60 }
61
62 /**
63 * Return the Set associated with the given key. Result is null if
64 * nothing has been mapped to this key. Modifications of the returned list
65 * changes the underling map, but you should better not do that.
66 */
67 public Set<B> get(A key) {
68 return map.get(key);
69 }
70
71 /**
72 * Like get, but returns an empty Set if nothing has been mapped to the key.
73 */
74 public LinkedHashSet<B> getValues(A key) {
75 if (!map.containsKey(key))
76 return new LinkedHashSet<B>();
77 return map.get(key);
78 }
79
80 public boolean isEmpty() {
81 return map.isEmpty();
82 }
83
84 public boolean containsKey(A key) {
85 return map.containsKey(key);
86 }
87
88 /**
89 * Returns true if the multimap contains a value for a key
90 * @param key The key
91 * @param value The value
92 * @return true if the key contains the value
93 */
94 public boolean contains(A key, B value) {
95 Set<B> values = get(key);
96 return (values == null) ? false : values.contains(value);
97 }
98
99 public void clear() {
100 map.clear();
101 }
102
103 public Set<Entry<A, LinkedHashSet<B>>> entrySet() {
104 return map.entrySet();
105 }
106
107 /**
108 * number of keys
109 */
110 public int size() {
111 return map.size();
112 }
113
114 /**
115 * returns a collection of all value sets
116 */
117 public Collection<LinkedHashSet<B>> values() {
118 return map.values();
119 }
120
121 /**
122 * Removes a cerain key=value mapping
123 *
124 * @return true, if something was removed
125 */
126 public boolean remove(A key, B value) {
127 Set<B> values = get(key);
128 if (values != null) {
129 return values.remove(value);
130 }
131 return false;
132 }
133
134 /**
135 * Removes all mappings for a certain key
136 */
137 public LinkedHashSet<B> remove(A key) {
138 return map.remove(key);
139 }
140
141 public String toString() {
142 List<String> entries = new ArrayList<String>(map.size());
143 for (A key : map.keySet()) {
144 entries.add(key + "->{" + Utils.join(",", map.get(key)) + "}");
145 }
146 return "(" + Utils.join(",", entries) + ")";
147 }
148}
Note: See TracBrowser for help on using the repository browser.