Committed by
Gerrit Code Review
Support for a java.util.Map implementation backed by ConsistentMap
Change-Id: I73240b69d0913f7534bd4006411ab217a40b254c
Showing
4 changed files
with
170 additions
and
1 deletions
... | @@ -17,6 +17,7 @@ | ... | @@ -17,6 +17,7 @@ |
17 | package org.onosproject.store.service; | 17 | package org.onosproject.store.service; |
18 | 18 | ||
19 | import java.util.Collection; | 19 | import java.util.Collection; |
20 | +import java.util.Map; | ||
20 | import java.util.Map.Entry; | 21 | import java.util.Map.Entry; |
21 | import java.util.Set; | 22 | import java.util.Set; |
22 | import java.util.function.BiFunction; | 23 | import java.util.function.BiFunction; |
... | @@ -281,4 +282,10 @@ public interface ConsistentMap<K, V> { | ... | @@ -281,4 +282,10 @@ public interface ConsistentMap<K, V> { |
281 | * @param listener listener to unregister | 282 | * @param listener listener to unregister |
282 | */ | 283 | */ |
283 | void removeListener(MapEventListener<K, V> listener); | 284 | void removeListener(MapEventListener<K, V> listener); |
284 | -} | 285 | + |
286 | + /** | ||
287 | + * Returns a java.util.Map instance backed by this ConsistentMap. | ||
288 | + * @return java.util.Map | ||
289 | + */ | ||
290 | + Map<K, V> asJavaMap(); | ||
291 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -141,4 +141,9 @@ public class ConsistentMapAdapter<K, V> implements ConsistentMap<K, V> { | ... | @@ -141,4 +141,9 @@ public class ConsistentMapAdapter<K, V> implements ConsistentMap<K, V> { |
141 | public void removeListener(MapEventListener<K, V> listener) { | 141 | public void removeListener(MapEventListener<K, V> listener) { |
142 | 142 | ||
143 | } | 143 | } |
144 | + | ||
145 | + @Override | ||
146 | + public Map<K, V> asJavaMap() { | ||
147 | + return null; | ||
148 | + } | ||
144 | } | 149 | } | ... | ... |
core/store/dist/src/main/java/org/onosproject/store/consistent/impl/ConsistentMapBackedJavaMap.java
0 → 100644
1 | +package org.onosproject.store.consistent.impl; | ||
2 | + | ||
3 | +import java.util.Collection; | ||
4 | +import java.util.Map; | ||
5 | +import java.util.Set; | ||
6 | +import java.util.function.BiConsumer; | ||
7 | +import java.util.function.BiFunction; | ||
8 | +import java.util.function.Function; | ||
9 | +import java.util.stream.Collectors; | ||
10 | + | ||
11 | +import org.onosproject.store.service.ConsistentMap; | ||
12 | +import org.onosproject.store.service.Versioned; | ||
13 | + | ||
14 | +import com.google.common.collect.Collections2; | ||
15 | +import com.google.common.collect.Maps; | ||
16 | + | ||
17 | +/** | ||
18 | + * Standard java Map backed by a ConsistentMap. | ||
19 | + * | ||
20 | + * @param <K> key type | ||
21 | + * @param <V> value type | ||
22 | + */ | ||
23 | +public final class ConsistentMapBackedJavaMap<K, V> implements Map<K, V> { | ||
24 | + | ||
25 | + private final ConsistentMap<K, V> backingMap; | ||
26 | + | ||
27 | + public ConsistentMapBackedJavaMap(ConsistentMap<K, V> backingMap) { | ||
28 | + this.backingMap = backingMap; | ||
29 | + } | ||
30 | + | ||
31 | + @Override | ||
32 | + public int size() { | ||
33 | + return backingMap.size(); | ||
34 | + } | ||
35 | + | ||
36 | + @Override | ||
37 | + public boolean isEmpty() { | ||
38 | + return backingMap.isEmpty(); | ||
39 | + } | ||
40 | + | ||
41 | + @Override | ||
42 | + public boolean containsKey(Object key) { | ||
43 | + return backingMap.containsKey((K) key); | ||
44 | + } | ||
45 | + | ||
46 | + @Override | ||
47 | + public boolean containsValue(Object value) { | ||
48 | + return backingMap.containsValue((V) value); | ||
49 | + } | ||
50 | + | ||
51 | + @Override | ||
52 | + public V get(Object key) { | ||
53 | + return Versioned.valueOrElse(backingMap.get((K) key), null); | ||
54 | + } | ||
55 | + | ||
56 | + @Override | ||
57 | + public V getOrDefault(Object key, V defaultValue) { | ||
58 | + return Versioned.valueOrElse(backingMap.get((K) key), defaultValue); | ||
59 | + } | ||
60 | + | ||
61 | + @Override | ||
62 | + public V put(K key, V value) { | ||
63 | + return Versioned.valueOrElse(backingMap.put(key, value), null); | ||
64 | + } | ||
65 | + | ||
66 | + @Override | ||
67 | + public V putIfAbsent(K key, V value) { | ||
68 | + return Versioned.valueOrElse(backingMap.putIfAbsent(key, value), null); | ||
69 | + } | ||
70 | + | ||
71 | + @Override | ||
72 | + public V remove(Object key) { | ||
73 | + return Versioned.valueOrElse(backingMap.remove((K) key), null); | ||
74 | + } | ||
75 | + | ||
76 | + @Override | ||
77 | + public boolean remove(Object key, Object value) { | ||
78 | + return backingMap.remove((K) key, (V) value); | ||
79 | + } | ||
80 | + | ||
81 | + @Override | ||
82 | + public V replace(K key, V value) { | ||
83 | + throw new UnsupportedOperationException(); | ||
84 | + } | ||
85 | + | ||
86 | + @Override | ||
87 | + public boolean replace(K key, V oldValue, V newValue) { | ||
88 | + return backingMap.replace(key, oldValue, newValue); | ||
89 | + } | ||
90 | + | ||
91 | + @Override | ||
92 | + public void putAll(Map<? extends K, ? extends V> m) { | ||
93 | + m.forEach((k, v) -> { | ||
94 | + backingMap.put(k, v); | ||
95 | + }); | ||
96 | + } | ||
97 | + | ||
98 | + @Override | ||
99 | + public void clear() { | ||
100 | + backingMap.clear(); | ||
101 | + } | ||
102 | + | ||
103 | + @Override | ||
104 | + public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { | ||
105 | + return Versioned.valueOrElse(backingMap.compute(key, remappingFunction), null); | ||
106 | + } | ||
107 | + | ||
108 | + @Override | ||
109 | + public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { | ||
110 | + return Versioned.valueOrElse(backingMap.computeIfAbsent(key, mappingFunction), null); | ||
111 | + } | ||
112 | + | ||
113 | + @Override | ||
114 | + public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { | ||
115 | + return Versioned.valueOrElse(backingMap.computeIfPresent(key, remappingFunction), null); | ||
116 | + } | ||
117 | + | ||
118 | + @Override | ||
119 | + public Set<K> keySet() { | ||
120 | + return backingMap.keySet(); | ||
121 | + } | ||
122 | + | ||
123 | + @Override | ||
124 | + public Collection<V> values() { | ||
125 | + return Collections2.transform(backingMap.values(), v -> v.value()); | ||
126 | + } | ||
127 | + | ||
128 | + @Override | ||
129 | + public Set<java.util.Map.Entry<K, V>> entrySet() { | ||
130 | + return backingMap.entrySet() | ||
131 | + .stream() | ||
132 | + .map(entry -> Maps.immutableEntry(entry.getKey(), entry.getValue().value())) | ||
133 | + .collect(Collectors.toSet()); | ||
134 | + } | ||
135 | + | ||
136 | + @Override | ||
137 | + public void forEach(BiConsumer<? super K, ? super V> action) { | ||
138 | + entrySet().forEach(e -> action.accept(e.getKey(), e.getValue())); | ||
139 | + } | ||
140 | + | ||
141 | + @Override | ||
142 | + public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { | ||
143 | + return computeIfPresent(key, (k, v) -> v == null ? value : remappingFunction.apply(v, value)); | ||
144 | + } | ||
145 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -17,6 +17,7 @@ | ... | @@ -17,6 +17,7 @@ |
17 | package org.onosproject.store.consistent.impl; | 17 | package org.onosproject.store.consistent.impl; |
18 | 18 | ||
19 | import java.util.Collection; | 19 | import java.util.Collection; |
20 | +import java.util.Map; | ||
20 | import java.util.Map.Entry; | 21 | import java.util.Map.Entry; |
21 | import java.util.concurrent.CompletableFuture; | 22 | import java.util.concurrent.CompletableFuture; |
22 | import java.util.concurrent.ExecutionException; | 23 | import java.util.concurrent.ExecutionException; |
... | @@ -44,6 +45,7 @@ public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> { | ... | @@ -44,6 +45,7 @@ public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> { |
44 | private static final int OPERATION_TIMEOUT_MILLIS = 5000; | 45 | private static final int OPERATION_TIMEOUT_MILLIS = 5000; |
45 | 46 | ||
46 | private final DefaultAsyncConsistentMap<K, V> asyncMap; | 47 | private final DefaultAsyncConsistentMap<K, V> asyncMap; |
48 | + private Map<K, V> javaMap; | ||
47 | 49 | ||
48 | public String name() { | 50 | public String name() { |
49 | return asyncMap.name(); | 51 | return asyncMap.name(); |
... | @@ -189,4 +191,14 @@ public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> { | ... | @@ -189,4 +191,14 @@ public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> { |
189 | public void removeListener(MapEventListener<K, V> listener) { | 191 | public void removeListener(MapEventListener<K, V> listener) { |
190 | asyncMap.addListener(listener); | 192 | asyncMap.addListener(listener); |
191 | } | 193 | } |
194 | + | ||
195 | + @Override | ||
196 | + public Map<K, V> asJavaMap() { | ||
197 | + synchronized (this) { | ||
198 | + if (javaMap == null) { | ||
199 | + javaMap = new ConsistentMapBackedJavaMap<>(this); | ||
200 | + } | ||
201 | + } | ||
202 | + return javaMap; | ||
203 | + } | ||
192 | } | 204 | } |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment