|
@@ -21,77 +21,143 @@
|
21
|
21
|
*/
|
22
|
22
|
package com.dmdirc.util;
|
23
|
23
|
|
|
24
|
+import java.util.AbstractMap.SimpleEntry;
|
24
|
25
|
import java.util.ArrayList;
|
|
26
|
+import java.util.Collection;
|
25
|
27
|
import java.util.HashSet;
|
26
|
28
|
import java.util.List;
|
|
29
|
+import java.util.Map;
|
|
30
|
+import java.util.Map.Entry;
|
27
|
31
|
import java.util.Set;
|
28
|
32
|
|
29
|
33
|
/**
|
30
|
34
|
* An object that maps keys to values, and values back to keys. Currently
|
31
|
35
|
* does no checking for duplicates. Does not allow null values.
|
32
|
|
- *
|
|
36
|
+ *
|
33
|
37
|
* @param <A> The first type of data to be mapped
|
34
|
|
- * @param <B> The second type of data to be mapped
|
35
|
|
- * @author chris
|
|
38
|
+ * @param <B> The second type of data to be mapped
|
36
|
39
|
*/
|
37
|
|
-public class DoubleMap<A,B> {
|
38
|
|
-
|
|
40
|
+public class DoubleMap<A,B> implements Map<A,B> {
|
|
41
|
+
|
39
|
42
|
/** The keys in this map. */
|
40
|
43
|
protected final List<A> keys = new ArrayList<A>();
|
41
|
44
|
/** The values in this map. */
|
42
|
45
|
protected final List<B> values = new ArrayList<B>();
|
43
|
|
-
|
44
|
|
- /**
|
45
|
|
- * Adds the specified pair to this map.
|
46
|
|
- *
|
47
|
|
- * @param key The key for the map
|
48
|
|
- * @param value The value for the map
|
49
|
|
- */
|
50
|
|
- public void put(final A key, final B value) {
|
51
|
|
- if (key == null || value == null) {
|
52
|
|
- throw new NullPointerException();
|
53
|
|
- }
|
54
|
|
-
|
55
|
|
- keys.add(key);
|
56
|
|
- values.add(value);
|
57
|
|
- }
|
58
|
|
-
|
|
46
|
+
|
59
|
47
|
/**
|
60
|
48
|
* Retrieves the value associated with the specified key.
|
61
|
|
- *
|
62
|
|
- * @param key The key to search for
|
|
49
|
+ *
|
|
50
|
+ * @param key The key to search for
|
63
|
51
|
* @return The value of the specified key
|
64
|
52
|
*/
|
65
|
53
|
public B getValue(final A key) {
|
|
54
|
+ if (keys.indexOf(key) == -1) {
|
|
55
|
+ return null;
|
|
56
|
+ }
|
66
|
57
|
return values.get(keys.indexOf(key));
|
67
|
58
|
}
|
68
|
|
-
|
|
59
|
+
|
69
|
60
|
/**
|
70
|
61
|
* Retrieves the key associated with the specified value.
|
71
|
|
- *
|
|
62
|
+ *
|
72
|
63
|
* @param value The value to search for
|
73
|
64
|
* @return The key of the specified value
|
74
|
65
|
*/
|
75
|
66
|
public A getKey(final B value) {
|
|
67
|
+ if (values.indexOf(value) == -1) {
|
|
68
|
+ return null;
|
|
69
|
+ }
|
76
|
70
|
return keys.get(values.indexOf(value));
|
77
|
71
|
}
|
78
|
|
-
|
79
|
|
- /**
|
80
|
|
- * Retrieves the set of keys in this double map.
|
81
|
|
- *
|
82
|
|
- * @return This map's key set
|
83
|
|
- */
|
|
72
|
+
|
|
73
|
+ /** {@inheritDoc} */
|
|
74
|
+ @Override
|
|
75
|
+ public B put(final A key, final B value) {
|
|
76
|
+ if (key == null || value == null) {
|
|
77
|
+ throw new NullPointerException();
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ keys.add(key);
|
|
81
|
+ values.add(value);
|
|
82
|
+
|
|
83
|
+ return value;
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ /** {@inheritDoc} */
|
|
87
|
+ @Override
|
84
|
88
|
public Set<A> keySet() {
|
85
|
89
|
return new HashSet<A>(keys);
|
86
|
90
|
}
|
87
|
|
-
|
88
|
|
- /**
|
89
|
|
- * Retrieves the set of values in this double map.
|
90
|
|
- *
|
91
|
|
- * @return This map's value set
|
92
|
|
- */
|
93
|
|
- public Set<B> valueSet() {
|
94
|
|
- return new HashSet<B>(values);
|
|
91
|
+
|
|
92
|
+ /** {@inheritDoc} */
|
|
93
|
+ @Override
|
|
94
|
+ public int size() {
|
|
95
|
+ return keys.size();
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ /** {@inheritDoc} */
|
|
99
|
+ @Override
|
|
100
|
+ public boolean isEmpty() {
|
|
101
|
+ return keys.isEmpty();
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ /** {@inheritDoc} */
|
|
105
|
+ @Override
|
|
106
|
+ public boolean containsKey(final Object key) {
|
|
107
|
+ return keys.contains((A) key);
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ /** {@inheritDoc} */
|
|
111
|
+ @Override
|
|
112
|
+ public boolean containsValue(final Object value) {
|
|
113
|
+ return values.contains((B) value);
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ /** {@inheritDoc} */
|
|
117
|
+ @Override
|
|
118
|
+ public B get(final Object key) {
|
|
119
|
+ return getValue((A) key);
|
95
|
120
|
}
|
96
|
121
|
|
|
122
|
+ /** {@inheritDoc} */
|
|
123
|
+ @Override
|
|
124
|
+ public B remove(final Object key) {
|
|
125
|
+ if (keys.indexOf(key) == -1) {
|
|
126
|
+ return null;
|
|
127
|
+ }
|
|
128
|
+ final B value = values.remove(keys.indexOf(key));
|
|
129
|
+ keys.remove(keys.indexOf(key));
|
|
130
|
+ return value;
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ /** {@inheritDoc} */
|
|
134
|
+ @Override
|
|
135
|
+ public void putAll(final Map<? extends A, ? extends B> m) {
|
|
136
|
+ for (Entry<? extends A, ? extends B> entry : m.entrySet()) {
|
|
137
|
+ put(entry.getKey(), entry.getValue());
|
|
138
|
+ }
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ /** {@inheritDoc} */
|
|
142
|
+ @Override
|
|
143
|
+ public void clear() {
|
|
144
|
+ keys.clear();
|
|
145
|
+ values.clear();
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ /** {@inheritDoc} */
|
|
149
|
+ @Override
|
|
150
|
+ public Collection<B> values() {
|
|
151
|
+ return new ArrayList<B>(values);
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ /** {@inheritDoc} */
|
|
155
|
+ @Override
|
|
156
|
+ public Set<Entry<A, B>> entrySet() {
|
|
157
|
+ final HashSet<Entry<A, B>> set = new HashSet<Entry<A, B>>();
|
|
158
|
+ for (A key : keys) {
|
|
159
|
+ set.add(new SimpleEntry(key, getValue(key)));
|
|
160
|
+ }
|
|
161
|
+ return set;
|
|
162
|
+ }
|
97
|
163
|
}
|