Libraryless. Click here for Pure Java version (5284L/29K).
1 | import java.util.function.BiConsumer; |
2 | import java.util.function.BiFunction; |
3 | |
4 | persistable static class SynchronizedMap<K,V> implements Map<K,V>, Serializable {
|
5 | /*final*/ Map<K,V> m; // Backing Map |
6 | /*final*/ Object mutex; // Object on which to synchronize |
7 | |
8 | SynchronizedMap(Map<K,V> m) {
|
9 | this.m = Objects.requireNonNull(m); |
10 | mutex = this; |
11 | } |
12 | |
13 | SynchronizedMap(Map<K,V> m, Object mutex) {
|
14 | this.m = m; |
15 | this.mutex = mutex; |
16 | } |
17 | |
18 | Map<K, V> innerMap() { ret m; }
|
19 | |
20 | public int size() {
|
21 | synchronized (mutex) {return m.size();}
|
22 | } |
23 | public boolean isEmpty() {
|
24 | synchronized (mutex) {return m.isEmpty();}
|
25 | } |
26 | public boolean containsKey(Object key) {
|
27 | synchronized (mutex) {return m.containsKey(key);}
|
28 | } |
29 | public boolean containsValue(Object value) {
|
30 | synchronized (mutex) {return m.containsValue(value);}
|
31 | } |
32 | public V get(Object key) {
|
33 | synchronized (mutex) {return m.get(key);}
|
34 | } |
35 | |
36 | public V put(K key, V value) {
|
37 | synchronized (mutex) {return m.put(key, value);}
|
38 | } |
39 | public V remove(Object key) {
|
40 | synchronized (mutex) {return m.remove(key);}
|
41 | } |
42 | public void putAll(Map<? extends K, ? extends V> map) {
|
43 | synchronized (mutex) {m.putAll(map);}
|
44 | } |
45 | public void clear() {
|
46 | synchronized (mutex) {m.clear();}
|
47 | } |
48 | |
49 | private transient Set<K> keySet; |
50 | private transient Set<Map.Entry<K,V>> entrySet; |
51 | private transient Collection<V> values; |
52 | |
53 | public Set<K> keySet() {
|
54 | synchronized (mutex) {
|
55 | if (keySet==null) |
56 | keySet = new SynchronizedSet<>(m.keySet(), mutex); |
57 | return keySet; |
58 | } |
59 | } |
60 | |
61 | public Set<Map.Entry<K,V>> entrySet() {
|
62 | synchronized (mutex) {
|
63 | if (entrySet==null) |
64 | entrySet = new SynchronizedSet<>(m.entrySet(), mutex); |
65 | return entrySet; |
66 | } |
67 | } |
68 | |
69 | public Collection<V> values() {
|
70 | synchronized (mutex) {
|
71 | if (values==null) |
72 | values = new SynchronizedCollection<>(m.values(), mutex); |
73 | return values; |
74 | } |
75 | } |
76 | |
77 | public boolean equals(Object o) {
|
78 | if (this == o) |
79 | return true; |
80 | synchronized (mutex) {return m.equals(o);}
|
81 | } |
82 | public int hashCode() {
|
83 | synchronized (mutex) {return m.hashCode();}
|
84 | } |
85 | public String toString() {
|
86 | synchronized (mutex) {return m.toString();}
|
87 | } |
88 | |
89 | // Override default methods in Map |
90 | @Override |
91 | public V getOrDefault(Object k, V defaultValue) {
|
92 | synchronized (mutex) {return m.getOrDefault(k, defaultValue);}
|
93 | } |
94 | @Override |
95 | public void forEach(BiConsumer<? super K, ? super V> action) {
|
96 | synchronized (mutex) {m.forEach(action);}
|
97 | } |
98 | @Override |
99 | public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
|
100 | synchronized (mutex) {m.replaceAll(function);}
|
101 | } |
102 | @Override |
103 | public V putIfAbsent(K key, V value) {
|
104 | synchronized (mutex) {return m.putIfAbsent(key, value);}
|
105 | } |
106 | @Override |
107 | public boolean remove(Object key, Object value) {
|
108 | synchronized (mutex) {return m.remove(key, value);}
|
109 | } |
110 | @Override |
111 | public boolean replace(K key, V oldValue, V newValue) {
|
112 | synchronized (mutex) {return m.replace(key, oldValue, newValue);}
|
113 | } |
114 | @Override |
115 | public V replace(K key, V value) {
|
116 | synchronized (mutex) {return m.replace(key, value);}
|
117 | } |
118 | @Override |
119 | public V computeIfAbsent(K key, |
120 | java.util.function.Function<? super K, ? extends V> mappingFunction) {
|
121 | synchronized (mutex) {return m.computeIfAbsent(key, mappingFunction);}
|
122 | } |
123 | @Override |
124 | public V computeIfPresent(K key, |
125 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
126 | synchronized (mutex) {return m.computeIfPresent(key, remappingFunction);}
|
127 | } |
128 | @Override |
129 | public V compute(K key, |
130 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
131 | synchronized (mutex) {return m.compute(key, remappingFunction);}
|
132 | } |
133 | @Override |
134 | public V merge(K key, V value, |
135 | BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
|
136 | synchronized (mutex) {return m.merge(key, value, remappingFunction);}
|
137 | } |
138 | |
139 | @java.io.Serial |
140 | private void writeObject(ObjectOutputStream s) throws IOException {
|
141 | synchronized (mutex) {s.defaultWriteObject();}
|
142 | } |
143 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1031162 |
| Snippet name: | SynchronizedMap - replacement for Collections.SynchronizedMap to circumvent reflection problem |
| Eternal ID of this version: | #1031162/8 |
| Text MD5: | fcb3c8cde3a32df4576a42be88992f8a |
| Transpilation MD5: | 9d3c6b0438eb7797d1e50489ff443b8e |
| Author: | stefan |
| Category: | |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-02-19 08:19:39 |
| Source code size: | 4645 bytes / 143 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 657 / 964 |
| Version history: | 7 change(s) |
| Referenced in: | [show references] |