|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| package edu.rice.cs.plt.collect; |
|
36 |
| |
|
37 |
| import java.util.Map; |
|
38 |
| import java.util.Collection; |
|
39 |
| import java.util.Set; |
|
40 |
| import java.util.Iterator; |
|
41 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
42 |
| import edu.rice.cs.plt.iter.MappedIterator; |
|
43 |
| import edu.rice.cs.plt.lambda.Lambda; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| public abstract class AbstractKeyBasedMap<K, V> implements LambdaMap<K, V> { |
|
54 |
| |
|
55 |
| public abstract V get(Object key); |
|
56 |
| public abstract PredicateSet<K> keySet(); |
|
57 |
| |
|
58 |
| |
|
59 |
0
| public V value(K key) { return get(key); }
|
|
60 |
| |
|
61 |
0
| public int size() { return keySet().size(); }
|
|
62 |
| |
|
63 |
0
| public boolean isEmpty() { return keySet().isEmpty(); }
|
|
64 |
| |
|
65 |
0
| public boolean containsKey(Object key) { return keySet().contains(key); }
|
|
66 |
| |
|
67 |
| |
|
68 |
0
| public boolean containsValue(Object val) {
|
|
69 |
0
| return IterUtil.contains(IterUtil.map(keySet(), this), val);
|
|
70 |
| } |
|
71 |
| |
|
72 |
| |
|
73 |
0
| public Collection<V> values() {
|
|
74 |
0
| return new IterableCollection<V>(IterUtil.map(keySet(), this));
|
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
0
| public Set<Entry<K, V>> entrySet() {
|
|
79 |
0
| return new EntrySet();
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
0
| public V put(K key, V val) { throw new UnsupportedOperationException(); }
|
|
84 |
| |
|
85 |
0
| public V remove(Object key) { throw new UnsupportedOperationException(); }
|
|
86 |
| |
|
87 |
0
| public void clear() { throw new UnsupportedOperationException(); }
|
|
88 |
| |
|
89 |
| |
|
90 |
0
| public void putAll(Map<? extends K, ? extends V> elts) {
|
|
91 |
0
| for (Entry<? extends K, ? extends V> entry : elts.entrySet()) {
|
|
92 |
0
| put(entry.getKey(), entry.getValue());
|
|
93 |
| } |
|
94 |
| } |
|
95 |
| |
|
96 |
0
| public String toString() { return IterUtil.toString(entrySet(), "{", ", ", "}"); }
|
|
97 |
| |
|
98 |
0
| public boolean equals(Object o) {
|
|
99 |
0
| if (this == o) { return true; }
|
|
100 |
0
| else if (!(o instanceof Map<?, ?>)) { return false; }
|
|
101 |
0
| else { return entrySet().equals(((Map<?, ?>) o).entrySet()); }
|
|
102 |
| } |
|
103 |
| |
|
104 |
0
| public int hashCode() { return entrySet().hashCode(); }
|
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| protected class EntrySet extends AbstractPredicateSet<Entry<K, V>> { |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
0
| @Override public boolean contains(Object o) {
|
|
115 |
0
| if (o instanceof Entry<?, ?>) {
|
|
116 |
0
| Entry<?, ?> entry = (Entry<?, ?>) o;
|
|
117 |
0
| Object key = entry.getKey();
|
|
118 |
0
| if (containsKey(key)) {
|
|
119 |
0
| Object val = entry.getValue();
|
|
120 |
0
| Object mapVal = get(key);
|
|
121 |
0
| return (val == null) ? (mapVal == null) : val.equals(mapVal);
|
|
122 |
| } |
|
123 |
| } |
|
124 |
0
| return false;
|
|
125 |
| } |
|
126 |
| |
|
127 |
| |
|
128 |
0
| public Iterator<Entry<K, V>> iterator() {
|
|
129 |
0
| return MappedIterator.make(keySet().iterator(), new Lambda<K, Entry<K, V>>() {
|
|
130 |
0
| public Entry<K, V> value(K key) {
|
|
131 |
0
| return mapEntryForKey(AbstractKeyBasedMap.this, key);
|
|
132 |
| } |
|
133 |
| }); |
|
134 |
| } |
|
135 |
| |
|
136 |
| |
|
137 |
0
| public boolean isInfinite() { return keySet().isInfinite(); }
|
|
138 |
| |
|
139 |
0
| public boolean hasFixedSize() { return keySet().hasFixedSize(); }
|
|
140 |
| |
|
141 |
0
| public boolean isStatic() { return false; }
|
|
142 |
| |
|
143 |
| |
|
144 |
0
| @Override public boolean isEmpty() { return AbstractKeyBasedMap.this.isEmpty(); }
|
|
145 |
| |
|
146 |
0
| @Override public int size() { return AbstractKeyBasedMap.this.size(); }
|
|
147 |
| |
|
148 |
0
| @Override public int size(int bound) { return keySet().size(bound); }
|
|
149 |
| |
|
150 |
| |
|
151 |
0
| @Override public boolean add(Entry<K, V> entry) {
|
|
152 |
0
| boolean present = contains(entry);
|
|
153 |
0
| AbstractKeyBasedMap.this.put(entry.getKey(), entry.getValue());
|
|
154 |
0
| return !present;
|
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
0
| @Override public boolean remove(Object o) {
|
|
159 |
0
| if (o instanceof Entry<?, ?>) {
|
|
160 |
0
| Entry<?, ?> entry = (Entry<?, ?>) o;
|
|
161 |
0
| boolean present = containsKey(entry.getKey());
|
|
162 |
0
| AbstractKeyBasedMap.this.remove(entry.getKey());
|
|
163 |
0
| return present;
|
|
164 |
| } |
|
165 |
0
| else { return false; }
|
|
166 |
| } |
|
167 |
| |
|
168 |
| |
|
169 |
0
| @Override public void clear() {
|
|
170 |
0
| AbstractKeyBasedMap.this.clear();
|
|
171 |
| } |
|
172 |
| |
|
173 |
| } |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
0
| protected static <K, V> Entry<K, V> mapEntryForKey(final Map<K, V> map, final K key) {
|
|
180 |
0
| return new Entry<K, V>() {
|
|
181 |
0
| public K getKey() { return key; }
|
|
182 |
0
| public V getValue() { return map.get(key); }
|
|
183 |
0
| public V setValue(V value) { return map.put(key, value); }
|
|
184 |
0
| public boolean equals(Object o) {
|
|
185 |
0
| if (this == o) { return true; }
|
|
186 |
0
| else if (!(o instanceof Entry<?, ?>)) { return false; }
|
|
187 |
| else { |
|
188 |
0
| Entry<?, ?> cast = (Entry<?, ?>) o;
|
|
189 |
0
| if (key == null ? cast.getKey() == null : key.equals(cast.getKey())) {
|
|
190 |
0
| V val = map.get(key);
|
|
191 |
0
| return val == null ? cast.getValue() == null : val.equals(cast.getValue());
|
|
192 |
| } |
|
193 |
0
| else { return false; }
|
|
194 |
| } |
|
195 |
| } |
|
196 |
0
| public int hashCode() {
|
|
197 |
0
| V val = map.get(key);
|
|
198 |
0
| return (key == null ? 0 : key.hashCode()) ^ (val == null ? 0 : val.hashCode());
|
|
199 |
| } |
|
200 |
| }; |
|
201 |
| } |
|
202 |
| |
|
203 |
| } |