Skip to content

Commit 9449d9b

Browse files
author
Jed Wesley-Smith
committed
now has a secondary lazily populated cache that can be cleared when mutated
1 parent 1a252c5 commit 9449d9b

1 file changed

Lines changed: 24 additions & 44 deletions

File tree

src/main/org/bson/util/ClassMap.java

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818

1919
package org.bson.util;
2020

21-
import java.util.Collection;
2221
import java.util.List;
2322
import java.util.Map;
24-
import java.util.Set;
2523

2624
import org.bson.util.concurrent.ComputingMap;
25+
import org.bson.util.concurrent.CopyOnWriteMap;
2726
import org.bson.util.concurrent.Function;
2827

2928
/**
@@ -42,7 +41,7 @@
4241
*
4342
* (assuming Dog.class < Animal.class)
4443
*/
45-
public class ClassMap<T> implements Map<Class<?>, T> {
44+
public class ClassMap<T> {
4645

4746
/**
4847
* Walks superclass and interface graph, superclasses first, then
@@ -58,69 +57,50 @@ private final class ComputeFunction implements Function<Class<?>, T> {
5857
@Override
5958
public T apply(Class<?> a) {
6059
for (Class<?> cls : getAncestry(a)) {
61-
if (a != cls) {
62-
return get(cls);
60+
T result = map.get(cls);
61+
System.out.println("trying: " + a + " and " + cls + " : " + result);
62+
if (result != null) {
63+
return result;
6364
}
6465
}
6566
return null;
6667
}
6768
};
6869

69-
private final Map<Class<?>, T> map = ComputingMap.create(new ComputeFunction());
70+
private final Map<Class<?>, T> map = CopyOnWriteMap.newHashMap();
71+
private final Map<Class<?>, T> cache = ComputingMap.create(new ComputeFunction());
7072

71-
public int size() {
72-
return map.size();
73-
}
74-
75-
public boolean isEmpty() {
76-
return map.isEmpty();
77-
}
78-
79-
public boolean containsKey(Object key) {
80-
return map.containsKey(key);
81-
}
82-
83-
public boolean containsValue(Object value) {
84-
return map.containsValue(value);
85-
}
8673

8774
public T get(Object key) {
88-
return map.get(key);
75+
return cache.get(key);
8976
}
9077

9178
public T put(Class<?> key, T value) {
92-
return map.put(key, value);
79+
try {
80+
return map.put(key, value);
81+
} finally {
82+
cache.clear();
83+
}
9384
}
9485

9586
public T remove(Object key) {
96-
return map.remove(key);
97-
}
98-
99-
public void putAll(Map<? extends Class<?>, ? extends T> m) {
100-
map.putAll(m);
87+
try {
88+
return map.remove(key);
89+
} finally {
90+
cache.clear();
91+
}
10192
}
10293

10394
public void clear() {
10495
map.clear();
96+
cache.clear();
10597
}
10698

107-
public Set<Class<?>> keySet() {
108-
return map.keySet();
109-
}
110-
111-
public Collection<T> values() {
112-
return map.values();
113-
}
114-
115-
public Set<java.util.Map.Entry<Class<?>, T>> entrySet() {
116-
return map.entrySet();
117-
}
118-
119-
public boolean equals(Object o) {
120-
return map.equals(o);
99+
public int size() {
100+
return map.size();
121101
}
122102

123-
public int hashCode() {
124-
return map.hashCode();
103+
public boolean isEmpty() {
104+
return map.isEmpty();
125105
}
126106
}

0 commit comments

Comments
 (0)