Skip to content

Commit 51307f1

Browse files
committed
Respond to code review
1 parent 5dbf7b7 commit 51307f1

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

bson/src/main/org/bson/codecs/Parameterizable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public interface Parameterizable {
3434
* @param codecRegistry the code registry to use to resolve codecs for the generic type arguments
3535
* @param types the types that are parameterizing the containing type. The size of the list should be equal to the number of type
3636
* parameters of the class whose codec is being parameterized, e.g. for a {@link Collection} the size of the list
37-
* would be one since {@code Collection} has a single type parameter.
37+
* would be one since {@code Collection} has a single type parameter. Additionally, the size will never be 0
38+
* since there is no purpose in parameterizing a codec for a type that has no type parameters.
3839
* @return the Codec parameterized with the given types
3940
*/
4041
Codec<?> parameterize(CodecRegistry codecRegistry, List<Type> types);

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
import org.bson.codecs.configuration.CodecRegistry;
2222

2323
import java.lang.reflect.Type;
24-
import java.util.Collections;
2524
import java.util.List;
25+
import java.util.Optional;
26+
27+
import static java.lang.String.format;
28+
import static org.bson.assertions.Assertions.isTrueArgument;
29+
import static org.bson.assertions.Assertions.notNull;
2630

2731
// An implementation of CodecRegistry that is used to detect cyclic dependencies between Codecs
2832
class ChildCodecRegistry<T> implements CodecRegistry {
@@ -32,10 +36,6 @@ class ChildCodecRegistry<T> implements CodecRegistry {
3236
private final Class<T> codecClass;
3337
private final List<Type> types;
3438

35-
ChildCodecRegistry(final CycleDetectingCodecRegistry registry, final Class<T> codecClass) {
36-
this(registry, codecClass, Collections.emptyList());
37-
}
38-
3939
ChildCodecRegistry(final CycleDetectingCodecRegistry registry, final Class<T> codecClass, final List<Type> types) {
4040
this.codecClass = codecClass;
4141
this.parent = null;
@@ -54,21 +54,26 @@ public Class<T> getCodecClass() {
5454
return codecClass;
5555
}
5656

57-
public List<Type> getTypes() {
58-
return types;
57+
public Optional<List<Type>> getTypes() {
58+
return Optional.ofNullable(types);
5959
}
6060

6161
// Gets a Codec, but if it detects a cyclic dependency, return a LazyCodec which breaks the chain.
6262
public <U> Codec<U> get(final Class<U> clazz) {
6363
if (hasCycles(clazz)) {
64-
return new LazyCodec<U>(registry, clazz, types);
64+
return new LazyCodec<>(registry, clazz, null);
6565
} else {
66-
return registry.get(new ChildCodecRegistry<>(this, clazz, Collections.emptyList()));
66+
return registry.get(new ChildCodecRegistry<>(this, clazz, null));
6767
}
6868
}
6969

7070
@Override
7171
public <U> Codec<U> get(final Class<U> clazz, final List<Type> typeArguments) {
72+
notNull("typeArguments", typeArguments);
73+
isTrueArgument("typeArguments is not empty", typeArguments.size() >= 1);
74+
isTrueArgument(format("typeArguments size should equal the number of type parameters in class %s, but is %d",
75+
clazz, typeArguments.size()),
76+
clazz.getTypeParameters().length == typeArguments.size());
7277
if (hasCycles(clazz)) {
7378
return new LazyCodec<U>(registry, clazz, typeArguments);
7479
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public T decode(final BsonReader reader, final DecoderContext decoderContext) {
5555

5656
private Codec<T> getWrapped() {
5757
if (wrapped == null) {
58-
if (types.isEmpty()) {
58+
if (types == null) {
5959
wrapped = registry.get(clazz);
6060
} else {
6161
wrapped = registry.get(clazz, types);

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import java.util.ArrayList;
2727
import java.util.List;
2828

29+
import static java.lang.String.format;
2930
import static org.bson.assertions.Assertions.isTrueArgument;
31+
import static org.bson.assertions.Assertions.notNull;
3032

3133
public final class ProvidersCodecRegistry implements CodecRegistry, CycleDetectingCodecRegistry {
3234
private final List<CodecProvider> codecProviders;
@@ -39,11 +41,16 @@ public ProvidersCodecRegistry(final List<? extends CodecProvider> codecProviders
3941

4042
@Override
4143
public <T> Codec<T> get(final Class<T> clazz) {
42-
return get(new ChildCodecRegistry<T>(this, clazz));
44+
return get(new ChildCodecRegistry<T>(this, clazz, null));
4345
}
4446

4547
@Override
4648
public <T> Codec<T> get(final Class<T> clazz, final List<Type> typeArguments) {
49+
notNull("typeArguments", typeArguments);
50+
isTrueArgument("typeArguments is not empty", typeArguments.size() >= 1);
51+
isTrueArgument(format("typeArguments size should equal the number of type parameters in class %s, but is %d",
52+
clazz, typeArguments.size()),
53+
clazz.getTypeParameters().length == typeArguments.size());
4754
return get(new ChildCodecRegistry<T>(this, clazz, typeArguments));
4855
}
4956

@@ -60,13 +67,13 @@ public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
6067

6168
@SuppressWarnings({"unchecked"})
6269
public <T> Codec<T> get(final ChildCodecRegistry<T> context) {
63-
CodecCacheKey codecCacheKey = new CodecCacheKey(context.getCodecClass(), context.getTypes());
70+
CodecCacheKey codecCacheKey = new CodecCacheKey(context.getCodecClass(), context.getTypes().orElse(null));
6471
if (!codecCache.containsKey(codecCacheKey)) {
6572
for (CodecProvider provider : codecProviders) {
6673
Codec<T> codec = provider.get(context.getCodecClass(), context);
6774
if (codec != null) {
68-
if (codec instanceof Parameterizable && !context.getTypes().isEmpty()) {
69-
codec = (Codec<T>) ((Parameterizable) codec).parameterize(context, context.getTypes());
75+
if (codec instanceof Parameterizable && context.getTypes().isPresent()) {
76+
codec = (Codec<T>) ((Parameterizable) codec).parameterize(context, context.getTypes().get());
7077
}
7178
return codecCache.putIfMissing(codecCacheKey, codec);
7279
}

0 commit comments

Comments
 (0)