Skip to content

Commit 12528a8

Browse files
committed
Cut more wobbling code down
JAVA-4751
1 parent c4f2ff6 commit 12528a8

File tree

3 files changed

+44
-152
lines changed

3 files changed

+44
-152
lines changed

bson/src/main/org/bson/internal/CodecCache.java

Lines changed: 0 additions & 81 deletions
This file was deleted.

bson/src/main/org/bson/internal/ProvidersCodecRegistry.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@
2121
import org.bson.codecs.configuration.CodecConfigurationException;
2222
import org.bson.codecs.configuration.CodecProvider;
2323
import org.bson.codecs.configuration.CodecRegistry;
24-
import org.bson.internal.CodecCache.CodecCacheKey;
2524

2625
import java.lang.reflect.Type;
2726
import java.util.ArrayList;
2827
import java.util.List;
28+
import java.util.Objects;
29+
import java.util.concurrent.ConcurrentHashMap;
30+
import java.util.concurrent.ConcurrentMap;
2931

3032
import static java.lang.String.format;
3133
import static org.bson.assertions.Assertions.isTrueArgument;
3234
import static org.bson.assertions.Assertions.notNull;
3335

3436
public final class ProvidersCodecRegistry implements CycleDetectingCodecRegistry {
3537
private final List<CodecProvider> codecProviders;
36-
private final CodecCache codecCache = new CodecCache();
38+
private final ConcurrentMap<CodecCacheKey, Codec<?>> codecCache = new ConcurrentHashMap<>();
3739

3840
public ProvidersCodecRegistry(final List<? extends CodecProvider> codecProviders) {
3941
isTrueArgument("codecProviders must not be null or empty", codecProviders != null && codecProviders.size() > 0);
@@ -68,17 +70,17 @@ public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
6870
@SuppressWarnings({"unchecked"})
6971
public <T> Codec<T> get(final ChildCodecRegistry<T> context) {
7072
CodecCacheKey codecCacheKey = new CodecCacheKey(context.getCodecClass(), context.getTypes().orElse(null));
71-
return codecCache.<T>get(codecCacheKey).orElseGet(() -> {
73+
return (Codec<T>) codecCache.computeIfAbsent(codecCacheKey, k -> {
7274
for (CodecProvider provider : codecProviders) {
73-
Codec<T> codec = provider.get(context.getCodecClass(), context);
75+
Codec<?> codec = provider.get(context.getCodecClass(), context);
7476
if (codec != null) {
7577
if (codec instanceof Parameterizable && context.getTypes().isPresent()) {
76-
codec = (Codec<T>) ((Parameterizable) codec).parameterize(context, context.getTypes().get());
78+
codec = ((Parameterizable) codec).parameterize(context, context.getTypes().get());
7779
}
78-
return codecCache.putIfAbsent(codecCacheKey, codec);
80+
return codec;
7981
}
8082
}
81-
throw new CodecConfigurationException(format("Can't find a codec for %s.", codecCacheKey));
83+
throw new CodecConfigurationException(format("Can't find a codec for %s.", k));
8284
});
8385
}
8486

@@ -114,4 +116,39 @@ public String toString() {
114116
+ "codecProviders=" + codecProviders
115117
+ '}';
116118
}
119+
120+
private static final class CodecCacheKey {
121+
private final Class<?> clazz;
122+
private final List<Type> types;
123+
124+
CodecCacheKey(final Class<?> clazz, final List<Type> types) {
125+
this.clazz = clazz;
126+
this.types = types;
127+
}
128+
129+
@Override
130+
public boolean equals(final Object o) {
131+
if (this == o) {
132+
return true;
133+
}
134+
if (o == null || getClass() != o.getClass()) {
135+
return false;
136+
}
137+
CodecCacheKey that = (CodecCacheKey) o;
138+
return clazz.equals(that.clazz) && Objects.equals(types, that.types);
139+
}
140+
141+
@Override
142+
public int hashCode() {
143+
return Objects.hash(clazz, types);
144+
}
145+
146+
@Override
147+
public String toString() {
148+
return "CodecCacheKey{"
149+
+ "clazz=" + clazz
150+
+ ", types=" + types
151+
+ '}';
152+
}
153+
}
117154
}

bson/src/test/unit/org/bson/internal/CodecCacheSpecification.groovy

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)