Skip to content

Commit bd52222

Browse files
committed
Create DEFAULT_BSON_TYPE_CODEC_MAP for DocumentCodec and BsonDocumentCodec
JAVA-3770
1 parent fbd8d2f commit bd52222

File tree

6 files changed

+28
-16
lines changed

6 files changed

+28
-16
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.List;
3131
import java.util.Map;
3232

33+
import static org.bson.assertions.Assertions.notNull;
3334
import static org.bson.codecs.BsonValueCodecProvider.getBsonTypeClassMap;
3435
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
3536

@@ -41,6 +42,7 @@
4142
public class BsonDocumentCodec implements CollectibleCodec<BsonDocument> {
4243
private static final String ID_FIELD_NAME = "_id";
4344
private static final CodecRegistry DEFAULT_REGISTRY = fromProviders(new BsonValueCodecProvider());
45+
private static final BsonTypeCodecMap DEFAULT_BSON_TYPE_CODEC_MAP = new BsonTypeCodecMap(getBsonTypeClassMap(), DEFAULT_REGISTRY);
4446

4547
private final CodecRegistry codecRegistry;
4648
private final BsonTypeCodecMap bsonTypeCodecMap;
@@ -49,7 +51,7 @@ public class BsonDocumentCodec implements CollectibleCodec<BsonDocument> {
4951
* Creates a new instance with a default codec registry that uses the {@link BsonValueCodecProvider}.
5052
*/
5153
public BsonDocumentCodec() {
52-
this(DEFAULT_REGISTRY);
54+
this(DEFAULT_REGISTRY, DEFAULT_BSON_TYPE_CODEC_MAP);
5355
}
5456

5557
/**
@@ -58,11 +60,12 @@ public BsonDocumentCodec() {
5860
* @param codecRegistry the {@code CodecRegistry} to use to look up the codecs for encoding and decoding to/from BSON
5961
*/
6062
public BsonDocumentCodec(final CodecRegistry codecRegistry) {
61-
if (codecRegistry == null) {
62-
throw new IllegalArgumentException("Codec registry can not be null");
63-
}
64-
this.codecRegistry = codecRegistry;
65-
this.bsonTypeCodecMap = new BsonTypeCodecMap(getBsonTypeClassMap(), codecRegistry);
63+
this(codecRegistry, new BsonTypeCodecMap(getBsonTypeClassMap(), codecRegistry));
64+
}
65+
66+
private BsonDocumentCodec(final CodecRegistry codecRegistry, final BsonTypeCodecMap bsonTypeCodecMap) {
67+
this.codecRegistry = notNull("Codec registry", codecRegistry);
68+
this.bsonTypeCodecMap = notNull("bsonTypeCodecMap", bsonTypeCodecMap);
6669
}
6770

6871
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
* @since 3.0
7171
*/
7272
public class BsonTypeClassMap {
73+
static final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap();
7374
private final Map<BsonType, Class<?>> map = new HashMap<BsonType, Class<?>>();
7475

7576
/**

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import static java.util.Arrays.asList;
3636
import static org.bson.assertions.Assertions.notNull;
37+
import static org.bson.codecs.BsonTypeClassMap.DEFAULT_BSON_TYPE_CLASS_MAP;
3738
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
3839

3940
/**
@@ -48,7 +49,8 @@ public class DocumentCodec implements CollectibleCodec<Document>, OverridableUui
4849
private static final CodecRegistry DEFAULT_REGISTRY = fromProviders(asList(new ValueCodecProvider(),
4950
new BsonValueCodecProvider(),
5051
new DocumentCodecProvider()));
51-
private static final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap();
52+
private static final BsonTypeCodecMap DEFAULT_BSON_TYPE_CODEC_MAP = new BsonTypeCodecMap(DEFAULT_BSON_TYPE_CLASS_MAP, DEFAULT_REGISTRY);
53+
private static final IdGenerator DEFAULT_ID_GENERATOR = new ObjectIdGenerator();
5254

5355
private final BsonTypeCodecMap bsonTypeCodecMap;
5456
private final CodecRegistry registry;
@@ -60,7 +62,7 @@ public class DocumentCodec implements CollectibleCodec<Document>, OverridableUui
6062
* Construct a new instance with a default {@code CodecRegistry}.
6163
*/
6264
public DocumentCodec() {
63-
this(DEFAULT_REGISTRY);
65+
this(DEFAULT_REGISTRY, DEFAULT_BSON_TYPE_CODEC_MAP, null);
6466
}
6567

6668
/**
@@ -93,8 +95,11 @@ public DocumentCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTy
9395
* @param valueTransformer the value transformer to use as a final step when decoding the value of any field in the document
9496
*/
9597
public DocumentCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap, final Transformer valueTransformer) {
96-
this(registry, new BsonTypeCodecMap(notNull("bsonTypeClassMap", bsonTypeClassMap), registry),
97-
new ObjectIdGenerator(), valueTransformer, UuidRepresentation.UNSPECIFIED);
98+
this(registry, new BsonTypeCodecMap(notNull("bsonTypeClassMap", bsonTypeClassMap), registry), valueTransformer);
99+
}
100+
101+
private DocumentCodec(final CodecRegistry registry, final BsonTypeCodecMap bsonTypeCodecMap, final Transformer valueTransformer) {
102+
this(registry, bsonTypeCodecMap, DEFAULT_ID_GENERATOR, valueTransformer, UuidRepresentation.UNSPECIFIED);
98103
}
99104

100105
private DocumentCodec(final CodecRegistry registry, final BsonTypeCodecMap bsonTypeCodecMap, final IdGenerator idGenerator,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.bson.types.CodeWithScope;
2424

2525
import static org.bson.assertions.Assertions.notNull;
26+
import static org.bson.codecs.BsonTypeClassMap.DEFAULT_BSON_TYPE_CLASS_MAP;
2627

2728
/**
2829
* A {@code CodecProvider} for the Document class and all the default Codec implementations on which it depends.
@@ -37,7 +38,7 @@ public class DocumentCodecProvider implements CodecProvider {
3738
* Construct a new instance with a default {@code BsonTypeClassMap}.
3839
*/
3940
public DocumentCodecProvider() {
40-
this(new BsonTypeClassMap());
41+
this(DEFAULT_BSON_TYPE_CLASS_MAP);
4142
}
4243

4344
/**
@@ -48,7 +49,7 @@ public DocumentCodecProvider() {
4849
* @see org.bson.codecs.DocumentCodec#DocumentCodec(org.bson.codecs.configuration.CodecRegistry, BsonTypeClassMap, org.bson.Transformer)
4950
*/
5051
public DocumentCodecProvider(final Transformer valueTransformer) {
51-
this(new BsonTypeClassMap(), valueTransformer);
52+
this(DEFAULT_BSON_TYPE_CLASS_MAP, valueTransformer);
5253
}
5354

5455
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.bson.codecs.configuration.CodecRegistry;
2222

2323
import static org.bson.assertions.Assertions.notNull;
24+
import static org.bson.codecs.BsonTypeClassMap.DEFAULT_BSON_TYPE_CLASS_MAP;
2425

2526
/**
2627
* A {@code CodecProvider} for classes than implement the {@code Iterable} interface.
@@ -35,7 +36,7 @@ public class IterableCodecProvider implements CodecProvider {
3536
* Construct a new instance with a default {@code BsonTypeClassMap} and no {@code Transformer}.
3637
*/
3738
public IterableCodecProvider() {
38-
this(new BsonTypeClassMap());
39+
this(DEFAULT_BSON_TYPE_CLASS_MAP);
3940
}
4041

4142
/**
@@ -45,7 +46,7 @@ public IterableCodecProvider() {
4546
* @param valueTransformer the value transformer for decoded values
4647
*/
4748
public IterableCodecProvider(final Transformer valueTransformer) {
48-
this(new BsonTypeClassMap(), valueTransformer);
49+
this(DEFAULT_BSON_TYPE_CLASS_MAP, valueTransformer);
4950
}
5051

5152
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424

2525
import static org.bson.assertions.Assertions.notNull;
26+
import static org.bson.codecs.BsonTypeClassMap.DEFAULT_BSON_TYPE_CLASS_MAP;
2627

2728
/**
2829
* A {@code CodecProvider} for the Map class and all the default Codec implementations on which it depends.
@@ -37,7 +38,7 @@ public class MapCodecProvider implements CodecProvider {
3738
* Construct a new instance with a default {@code BsonTypeClassMap}.
3839
*/
3940
public MapCodecProvider() {
40-
this(new BsonTypeClassMap());
41+
this(DEFAULT_BSON_TYPE_CLASS_MAP);
4142
}
4243

4344
/**
@@ -57,7 +58,7 @@ public MapCodecProvider(final BsonTypeClassMap bsonTypeClassMap) {
5758
* @param valueTransformer the value transformer for decoded values
5859
*/
5960
public MapCodecProvider(final Transformer valueTransformer) {
60-
this(new BsonTypeClassMap(), valueTransformer);
61+
this(DEFAULT_BSON_TYPE_CLASS_MAP, valueTransformer);
6162
}
6263

6364
/**

0 commit comments

Comments
 (0)