Skip to content

Commit 92f064d

Browse files
authored
Add CodecRegistries#withUuidRepresentation factory method (#864)
JAVA-4461
1 parent 33fe83d commit 92f064d

File tree

19 files changed

+94
-81
lines changed

19 files changed

+94
-81
lines changed

bson/src/main/org/bson/Document.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.bson.codecs.ValueCodecProvider;
3030
import org.bson.codecs.configuration.CodecRegistry;
3131
import org.bson.conversions.Bson;
32-
import org.bson.internal.CodecRegistryHelper;
3332
import org.bson.json.JsonMode;
3433
import org.bson.json.JsonReader;
3534
import org.bson.json.JsonWriter;
@@ -51,6 +50,7 @@
5150
import static org.bson.assertions.Assertions.isTrue;
5251
import static org.bson.assertions.Assertions.notNull;
5352
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
53+
import static org.bson.codecs.configuration.CodecRegistries.withUuidRepresentation;
5454

5555
/**
5656
* A representation of a document as a {@code Map}. All iterators will traverse the elements in insertion order, as with {@code
@@ -61,10 +61,8 @@
6161
*/
6262
public class Document implements Map<String, Object>, Serializable, Bson {
6363
private static final Codec<Document> DEFAULT_CODEC =
64-
CodecRegistryHelper.createRegistry(
65-
fromProviders(asList(new ValueCodecProvider(), new IterableCodecProvider(),
66-
new BsonValueCodecProvider(), new DocumentCodecProvider(), new MapCodecProvider())),
67-
UuidRepresentation.STANDARD)
64+
withUuidRepresentation(fromProviders(asList(new ValueCodecProvider(), new IterableCodecProvider(),
65+
new BsonValueCodecProvider(), new DocumentCodecProvider(), new MapCodecProvider())), UuidRepresentation.STANDARD)
6866
.get(Document.class);
6967

7068
private static final long serialVersionUID = 6297731997167536582L;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public Object transform(final Object value) {
115115

116116
@Override
117117
public Codec<Document> withUuidRepresentation(final UuidRepresentation uuidRepresentation) {
118+
if (this.uuidRepresentation.equals(uuidRepresentation)) {
119+
return this;
120+
}
118121
return new DocumentCodec(registry, bsonTypeCodecMap, idGenerator, valueTransformer, uuidRepresentation);
119122
}
120123

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public Object transform(final Object objectToTransform) {
8080

8181
@Override
8282
public Codec<Iterable> withUuidRepresentation(final UuidRepresentation uuidRepresentation) {
83+
if (this.uuidRepresentation.equals(uuidRepresentation)) {
84+
return this;
85+
}
8386
return new IterableCodec(registry, bsonTypeCodecMap, valueTransformer, uuidRepresentation);
8487
}
8588

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public Object transform(final Object value) {
101101

102102
@Override
103103
public Codec<Map<String, Object>> withUuidRepresentation(final UuidRepresentation uuidRepresentation) {
104+
if (this.uuidRepresentation.equals(uuidRepresentation)) {
105+
return this;
106+
}
104107
return new MapCodec(registry, bsonTypeCodecMap, valueTransformer, uuidRepresentation);
105108
}
106109

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public OverridableUuidRepresentationUuidCodec(final UuidRepresentation uuidRepre
4545

4646
@Override
4747
public Codec<UUID> withUuidRepresentation(final UuidRepresentation uuidRepresentation) {
48+
if (getUuidRepresentation().equals(uuidRepresentation)) {
49+
return this;
50+
}
4851
return new OverridableUuidRepresentationUuidCodec(uuidRepresentation);
4952
}
5053
}

bson/src/main/org/bson/codecs/configuration/CodecRegistries.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package org.bson.codecs.configuration;
1818

19+
import org.bson.UuidRepresentation;
1920
import org.bson.codecs.Codec;
21+
import org.bson.internal.OverridableUuidRepresentationCodecRegistry;
2022
import org.bson.internal.ProvidersCodecRegistry;
2123

2224
import java.util.List;
@@ -30,6 +32,28 @@
3032
*/
3133
public final class CodecRegistries {
3234

35+
/**
36+
* Apply given {@link UuidRepresentation} to the given {@link CodecRegistry}.
37+
*
38+
* @param codecRegistry the code registry
39+
* @param uuidRepresentation the uuid representation
40+
* @since 4.5
41+
*/
42+
public static CodecRegistry withUuidRepresentation(final CodecRegistry codecRegistry, final UuidRepresentation uuidRepresentation) {
43+
if (codecRegistry instanceof OverridableUuidRepresentationCodecRegistry) {
44+
OverridableUuidRepresentationCodecRegistry overridableUuidRepresentationCodecRegistry =
45+
(OverridableUuidRepresentationCodecRegistry) codecRegistry;
46+
if (overridableUuidRepresentationCodecRegistry.getUuidRepresentation().equals(uuidRepresentation)) {
47+
return codecRegistry;
48+
} else {
49+
return new OverridableUuidRepresentationCodecRegistry(overridableUuidRepresentationCodecRegistry.getWrapped(),
50+
uuidRepresentation);
51+
}
52+
} else {
53+
return new OverridableUuidRepresentationCodecRegistry(codecRegistry, uuidRepresentation);
54+
}
55+
}
56+
3357
/**
3458
* Creates a {@code CodecRegistry} from the provided list of {@code Codec} instances.
3559
*

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class OverridableUuidRepresentationCodecRegistry implements CycleDetectin
3030
private final CodecCache codecCache = new CodecCache();
3131
private final UuidRepresentation uuidRepresentation;
3232

33-
OverridableUuidRepresentationCodecRegistry(final CodecProvider wrapped, final UuidRepresentation uuidRepresentation) {
33+
public OverridableUuidRepresentationCodecRegistry(final CodecProvider wrapped, final UuidRepresentation uuidRepresentation) {
3434
this.uuidRepresentation = notNull("uuidRepresentation", uuidRepresentation);
3535
this.wrapped = notNull("wrapped", wrapped);
3636
}

bson/src/test/unit/org/bson/codecs/configuration/CodeRegistriesSpecification.groovy

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ import org.bson.codecs.IntegerCodec
2323
import org.bson.codecs.LongCodec
2424
import org.bson.codecs.UuidCodec
2525
import org.bson.codecs.ValueCodecProvider
26+
import org.bson.internal.OverridableUuidRepresentationCodecRegistry
2627
import org.bson.internal.ProvidersCodecRegistry
2728
import spock.lang.Specification
2829

2930
import static CodecRegistries.fromCodecs
3031
import static CodecRegistries.fromProviders
3132
import static CodecRegistries.fromRegistries
33+
import static org.bson.UuidRepresentation.STANDARD
34+
import static org.bson.UuidRepresentation.UNSPECIFIED
35+
import static org.bson.codecs.configuration.CodecRegistries.withUuidRepresentation
3236

3337
class CodeRegistriesSpecification extends Specification {
3438
def 'fromCodec should return a SingleCodecRegistry'() {
@@ -70,4 +74,31 @@ class CodeRegistriesSpecification extends Specification {
7074
registry.get(UUID).is(uuidCodec)
7175
registry.get(Integer) instanceof IntegerCodec
7276
}
77+
78+
def 'withUuidRepresentation should apply uuid representation'() {
79+
given:
80+
def registry = fromProviders(new ValueCodecProvider());
81+
82+
when:
83+
def registryWithStandard = withUuidRepresentation(registry, STANDARD)
84+
85+
then:
86+
registryWithStandard instanceof OverridableUuidRepresentationCodecRegistry
87+
(registryWithStandard as OverridableUuidRepresentationCodecRegistry).uuidRepresentation == STANDARD
88+
(registryWithStandard as OverridableUuidRepresentationCodecRegistry).wrapped == registry
89+
90+
when:
91+
def registryWithUnspecified = withUuidRepresentation(registryWithStandard, UNSPECIFIED)
92+
93+
then:
94+
registryWithUnspecified instanceof OverridableUuidRepresentationCodecRegistry
95+
(registryWithUnspecified as OverridableUuidRepresentationCodecRegistry).uuidRepresentation == UNSPECIFIED
96+
(registryWithUnspecified as OverridableUuidRepresentationCodecRegistry).wrapped == registry
97+
98+
when:
99+
def registryWithUnspecifiedTwo = withUuidRepresentation(registryWithUnspecified, UNSPECIFIED)
100+
101+
then:
102+
registryWithUnspecifiedTwo === registryWithUnspecified
103+
}
73104
}

driver-core/src/main/com/mongodb/BasicDBObject.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.bson.codecs.EncoderContext;
3030
import org.bson.codecs.configuration.CodecRegistry;
3131
import org.bson.conversions.Bson;
32-
import org.bson.internal.CodecRegistryHelper;
3332
import org.bson.io.BasicOutputBuffer;
3433
import org.bson.io.OutputBuffer;
3534
import org.bson.json.JsonMode;
@@ -46,6 +45,8 @@
4645
import java.util.Map;
4746
import java.util.TreeSet;
4847

48+
import static org.bson.codecs.configuration.CodecRegistries.withUuidRepresentation;
49+
4950
/**
5051
* A basic implementation of BSON object that is MongoDB specific. A {@code DBObject} can be created as follows, using this class:
5152
* <pre>
@@ -60,9 +61,7 @@ public class BasicDBObject extends BasicBSONObject implements DBObject, Bson {
6061
private static final long serialVersionUID = -4415279469780082174L;
6162

6263
private static final Codec<BasicDBObject> DEFAULT_CODEC =
63-
CodecRegistryHelper.createRegistry(
64-
DBObjectCodec.getDefaultRegistry(),
65-
UuidRepresentation.STANDARD)
64+
withUuidRepresentation(DBObjectCodec.getDefaultRegistry(), UuidRepresentation.STANDARD)
6665
.get(BasicDBObject.class);
6766

6867
/**

0 commit comments

Comments
 (0)