Skip to content

Commit 8672808

Browse files
committed
Polishing.
Reformat code. Tweak documentation wording. See #3596 Original pull request: #3982.
1 parent 29fb085 commit 8672808

File tree

6 files changed

+63
-57
lines changed

6 files changed

+63
-57
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

+20-17
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ protected ConversionContext getConversionContext(ObjectPath path) {
176176

177177
Assert.notNull(path, "ObjectPath must not be null");
178178

179-
return new ConversionContext(this, conversions, path, this::readDocument, this::readCollectionOrArray, this::readMap,
180-
this::readDBRef, this::getPotentiallyConvertedSimpleRead);
179+
return new ConversionContext(this, conversions, path, this::readDocument, this::readCollectionOrArray,
180+
this::readMap, this::readDBRef, this::getPotentiallyConvertedSimpleRead);
181181
}
182182

183183
/**
@@ -393,18 +393,18 @@ public ConversionContext forProperty(String name) {
393393

394394
EntityProjection<?, ?> property = returnedTypeDescriptor.findProperty(name);
395395
if (property == null) {
396-
return new ConversionContext(conversions, path, MappingMongoConverter.this::readDocument, collectionConverter,
396+
return new ConversionContext(sourceConverter, conversions, path, MappingMongoConverter.this::readDocument, collectionConverter,
397397
mapConverter, dbRefConverter, elementConverter);
398398
}
399399

400-
return new ProjectingConversionContext(sourceConverter, conversions, path, collectionConverter, mapConverter, dbRefConverter,
401-
elementConverter, property);
400+
return new ProjectingConversionContext(sourceConverter, conversions, path, collectionConverter, mapConverter,
401+
dbRefConverter, elementConverter, property);
402402
}
403403

404404
@Override
405405
public ConversionContext withPath(ObjectPath currentPath) {
406-
return new ProjectingConversionContext(sourceConverter, conversions, currentPath, collectionConverter, mapConverter,
407-
dbRefConverter, elementConverter, returnedTypeDescriptor);
406+
return new ProjectingConversionContext(sourceConverter, conversions, currentPath, collectionConverter,
407+
mapConverter, dbRefConverter, elementConverter, returnedTypeDescriptor);
408408
}
409409
}
410410

@@ -935,8 +935,9 @@ protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor acce
935935
TypeInformation<?> valueType = ClassTypeInformation.from(obj.getClass());
936936
TypeInformation<?> type = prop.getTypeInformation();
937937

938-
if(conversions.hasPropertyValueConverter(prop)) {
939-
accessor.put(prop, conversions.getPropertyValueConverter(prop).write(obj, new MongoConversionContext(prop, this)));
938+
if (conversions.hasPropertyValueConverter(prop)) {
939+
accessor.put(prop,
940+
conversions.getPropertyValueConverter(prop).write(obj, new MongoConversionContext(prop, this)));
940941
return;
941942
}
942943

@@ -1270,8 +1271,9 @@ private void writeSimpleInternal(@Nullable Object value, Bson bson, String key)
12701271
private void writeSimpleInternal(@Nullable Object value, Bson bson, MongoPersistentProperty property) {
12711272
DocumentAccessor accessor = new DocumentAccessor(bson);
12721273

1273-
if(conversions.hasPropertyValueConverter(property)) {
1274-
accessor.put(property, conversions.getPropertyValueConverter(property).write(value, new MongoConversionContext(property, this)));
1274+
if (conversions.hasPropertyValueConverter(property)) {
1275+
accessor.put(property,
1276+
conversions.getPropertyValueConverter(property).write(value, new MongoConversionContext(property, this)));
12751277
return;
12761278
}
12771279

@@ -1916,9 +1918,9 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19161918
return null;
19171919
}
19181920

1919-
if(context.conversions.hasPropertyValueConverter(property)) {
1920-
1921-
return (T) context.conversions.getPropertyValueConverter(property).read(value, new MongoConversionContext(property, context.sourceConverter));
1921+
if (context.conversions.hasPropertyValueConverter(property)) {
1922+
return (T) context.conversions.getPropertyValueConverter(property).read(value,
1923+
new MongoConversionContext(property, context.sourceConverter));
19221924
}
19231925

19241926
return (T) context.convert(value, property.getTypeInformation());
@@ -2148,7 +2150,8 @@ protected static class ConversionContext {
21482150
final ContainerValueConverter<DBRef> dbRefConverter;
21492151
final ValueConverter<Object> elementConverter;
21502152

2151-
ConversionContext(MongoConverter sourceConverter, org.springframework.data.convert.CustomConversions customConversions, ObjectPath path,
2153+
ConversionContext(MongoConverter sourceConverter,
2154+
org.springframework.data.convert.CustomConversions customConversions, ObjectPath path,
21522155
ContainerValueConverter<Bson> documentConverter, ContainerValueConverter<Collection<?>> collectionConverter,
21532156
ContainerValueConverter<Bson> mapConverter, ContainerValueConverter<DBRef> dbRefConverter,
21542157
ValueConverter<Object> elementConverter) {
@@ -2235,8 +2238,8 @@ public ConversionContext withPath(ObjectPath currentPath) {
22352238

22362239
Assert.notNull(currentPath, "ObjectPath must not be null");
22372240

2238-
return new ConversionContext(sourceConverter, conversions, currentPath, documentConverter, collectionConverter, mapConverter,
2239-
dbRefConverter, elementConverter);
2241+
return new ConversionContext(sourceConverter, conversions, currentPath, documentConverter, collectionConverter,
2242+
mapConverter, dbRefConverter, elementConverter);
22402243
}
22412244

22422245
public ObjectPath getPath() {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConversionContext.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ public <T> T write(@Nullable Object value, TypeInformation<T> target) {
5050

5151
@Override
5252
public <T> T read(@Nullable Object value, TypeInformation<T> target) {
53-
54-
if (!(value instanceof Bson)) {
55-
return ValueConversionContext.super.read(value, target);
56-
}
57-
58-
return mongoConverter.read(target.getType(), (Bson) value);
53+
return value instanceof Bson ? mongoConverter.read(target.getType(), (Bson) value)
54+
: ValueConversionContext.super.read(value, target);
5955
}
6056
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoCustomConversions.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.core.convert.converter.Converter;
3737
import org.springframework.core.convert.converter.ConverterFactory;
3838
import org.springframework.core.convert.converter.GenericConverter;
39+
import org.springframework.data.convert.ConverterBuilder;
3940
import org.springframework.data.convert.PropertyValueConversions;
4041
import org.springframework.data.convert.PropertyValueConverter;
4142
import org.springframework.data.convert.PropertyValueConverterFactory;
@@ -263,7 +264,8 @@ public MongoConverterConfigurationAdapter registerConverterFactory(ConverterFact
263264
}
264265

265266
/**
266-
* Add {@link Converter converters}, {@link ConverterFactory factories}, ...
267+
* Add {@link Converter converters}, {@link ConverterFactory factories}, {@link ConverterBuilder.ConverterAware
268+
* converter-aware objects}, and {@link GenericConverter generic converters}.
267269
*
268270
* @param converters must not be {@literal null} nor contain {@literal null} values.
269271
* @return this.
@@ -307,6 +309,7 @@ public MongoConverterConfigurationAdapter registerPropertyValueConverterFactory(
307309
*/
308310
public MongoConverterConfigurationAdapter setPropertyValueConversions(PropertyValueConversions valueConversions) {
309311

312+
Assert.notNull(valueConversions, "PropertyValueConversions must not be null");
310313
this.propertyValueConversions = valueConversions;
311314
return this;
312315
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoValueConverter.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
import org.springframework.data.convert.PropertyValueConverter;
1919

2020
/**
21-
* Pre typed {@link PropertyValueConverter} specific for the Data MongoDB module.
22-
*
21+
* MongoDB-specific {@link PropertyValueConverter} extension.
22+
*
2323
* @author Christoph Strobl
2424
* @since 3.4
2525
*/
26-
public interface MongoValueConverter<S, T> extends PropertyValueConverter<S, T, MongoConversionContext> {
27-
28-
}
26+
public interface MongoValueConverter<S, T> extends PropertyValueConverter<S, T, MongoConversionContext> {}

src/main/asciidoc/new-features.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
== What's New in Spring Data MongoDB 3.4
66

77
* Find and update ``Document``s via <<mongodb.repositories.queries.update,Repository method>>.
8-
* Property specific <<mongo.property-converters, value converters>>.
8+
* Property-specific <<mongo.property-converters, value converters>>.
99

1010
[[new-features.3.3]]
1111
== What's New in Spring Data MongoDB 3.3

src/main/asciidoc/reference/mongo-property-converters.adoc

+33-27
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[[mongo.property-converters]]
22
== Property Converters - Mapping specific fields
33

4-
Although to the <<mongo.custom-converters, type based conversion>> already offers means to influence the representation of certain types within the target store it has its limitations when not all potential values of that type should be considered as a conversion targets.
5-
Property based converters allow to specify conversion instructions on a per property basis either declarative, via `@ValueConverter`, or programmatic by registering a `PropertyValueConverter` for a specific field.
4+
While <<mongo.custom-converters, type-based conversion>> already offers ways to influence the conversion and representation of certain types within the target store it has its limitations when only certain values or properties of a particular type should be considered for conversion.
5+
Property-based converters allow configuring conversion rules on a per-property basis, either declarative, via `@ValueConverter`, or programmatic by registering a `PropertyValueConverter` for a specific property.
66

7-
A `PropertyValueConverter` is responsible of transforming a given value into its store representation (write) and back (read) as shown in the snippet below.
8-
Please mind the presence of the `ValueConversionContext` providing additional information, such as mapping metadata.
7+
A `PropertyValueConverter` can transform a given value into its store representation (**write**) and back (**read**) as shown in the snippet below.
8+
The additional `ValueConversionContext` provides additional information, such as mapping metadata and direct `read`/`write` methods.
99

10-
.PropertyValueConverter
10+
.A simple PropertyValueConverter
1111
====
1212
[source,java]
1313
----
@@ -26,69 +26,75 @@ class ReversingValueConverter implements PropertyValueConverter<String, String,
2626
----
2727
====
2828

29-
`PropertyValueConverter` instances can be obtained via `CustomConversions#getPropertyValueConverter(...)` delegating to `PropertyValueConversions` typically using a `PropertyValueConverterFactory` to provide the actual converter.
30-
Depending on the applications needs multiple instances of `PropertyValueConverterFactory` can be chained or decorated (eg. for caching).
31-
By default a caching implementation is used that is capable of serving types with a default constructor or enum values.
32-
A set of predefined factories is available via `PropertyValueConverterFactory`.
33-
To obtain a `PropertyValueConverter` from an `ApplicationContext` make sure to use the `PropertyValueConverterFactory.beanFactoryAware(...)` factory.
29+
`PropertyValueConverter` instances can be obtained via `CustomConversions#getPropertyValueConverter()` delegating to `PropertyValueConversions`, typically using a `PropertyValueConverterFactory` providing the actual converter.
30+
Depending on the applications needs, multiple instances of `PropertyValueConverterFactory` can be chained or decorated, for example to apply caching.
31+
By default, a caching implementation is used that is capable of serving types with a default constructor or enum values.
32+
A set of predefined factories is available through `PropertyValueConverterFactory` factory methods.
33+
Use `PropertyValueConverterFactory.beanFactoryAware(…)` to obtain a `PropertyValueConverter` instances from an `ApplicationContext`.
3434

35-
Changing the default behavior can be done via the `ConverterConfiguration`.
35+
You can change the default behavior through `ConverterConfiguration`.
3636

37+
[[mongo.property-converters.declarative]]
3738
=== Declarative Value Converter
3839

39-
The most straight forward usage of a `PropertyValueConverter` is via the `@ValueConverter` annotation referring to the target converter type.
40+
The most straight forward usage of a `PropertyValueConverter` is by annotating properties with the `@ValueConverter` annotation that defines the converter type.
4041

4142
.Declarative PropertyValueConverter
4243
====
4344
[source,java]
4445
----
45-
public class Person {
46-
// ...
46+
class Person {
47+
4748
@ValueConverter(ReversingValueConverter.class)
4849
String ssn;
4950
}
5051
----
5152
====
5253

53-
=== Programmatic Value Converter
54+
[[mongo.property-converters.programmatic]]
55+
=== Programmatic Value Converter Registration
5456

55-
Following the programmatic approach does not require to put additional annotations on the domain model but registers `PropertyValueConverter` instances for certain paths in a `PropertyValueConverterRegistrar` as shown below.
57+
Programmatic registration registers `PropertyValueConverter` instances for properties within an entity model using a `PropertyValueConverterRegistrar` as shown below.
58+
The difference to declarative registration is that programmatic registration happens entirely outside of the entity model.
59+
Such an approach is useful if you cannot or do not want to annotate the entity model.
5660

57-
.Programmatic PropertyValueConverter
61+
.Programmatic PropertyValueConverter registration
5862
====
5963
[source,java]
6064
----
6165
PropertyValueConverterRegistrar registrar = new PropertyValueConverterRegistrar();
6266
63-
registrar.registerConverter(Address.class, "street", new PropertyValueConverter() { ... }); <1>
67+
registrar.registerConverter(Address.class, "street", new PropertyValueConverter() { }); <1>
6468
6569
// type safe registration
66-
registrar.registerConverter(Person.class, Person::getSsn()) <2>
70+
registrar.registerConverter(Person.class, Person::getSsn()) <2>
6771
.writing(value -> encrypt(value))
6872
.reading(value -> decrypt(value));
6973
----
74+
7075
<1> Register a converter for the field identified by its name.
7176
<2> Type safe variant that allows to register a converter and its conversion functions.
7277
====
7378

7479
[WARNING]
7580
====
76-
Dot notation (eg. `registerConverter(Person.class, "address.street", ...)`) is *not* supported when registering converters.
81+
Dot-notation (such as `registerConverter(Person.class, "address.street", )`) nagivating across properties into subdocuments is *not* supported when registering converters.
7782
====
7883

84+
[[mongo.property-converters.value-conversions]]
7985
=== MongoDB property value conversions
8086

8187
The above sections outlined the purpose an overall structure of `PropertyValueConverters`.
8288
This section will focus on MongoDB specific aspects.
8389

84-
==== MongoValueConverter & MongoConversionContext
90+
==== MongoValueConverter and MongoConversionContext
8591

86-
The `MongoValueConverter` offers a pre typed `PropertyValueConverter` interface leveraging the `MongoConversionContext`.
92+
`MongoValueConverter` offers a pre typed `PropertyValueConverter` interface leveraging the `MongoConversionContext`.
8793

8894
==== MongoCustomConversions configuration
8995

90-
`MongoCustomConversions` are by default capable of dealing with declarative value converters depending on the configured `PropertyValueConverterFactory`.
91-
The `MongoConverterConfigurationAdapter` is there to help set up programmatic value conversions or define the `PropertyValueConverterFactory` to be used.
96+
`MongoCustomConversions` are by default capable of handling declarative value converters depending on the configured `PropertyValueConverterFactory`.
97+
`MongoConverterConfigurationAdapter` is there to help set up programmatic value conversions or define the `PropertyValueConverterFactory` to be used.
9298

9399
.Configuration Sample
94100
====
@@ -97,10 +103,10 @@ The `MongoConverterConfigurationAdapter` is there to help set up programmatic va
97103
MongoCustomConversions.create(configurationAdapter -> {
98104
99105
SimplePropertyValueConversions valueConversions = new SimplePropertyValueConversions();
100-
valueConversions.setConverterFactory(...);
106+
valueConversions.setConverterFactory();
101107
valueConversions.setValueConverterRegistry(new PropertyValueConverterRegistrar()
102-
.registerConverter(...)
103-
.buildRegistry());
108+
.registerConverter()
109+
.buildRegistry());
104110
105111
configurationAdapter.setPropertyValueConversions(valueConversions);
106112
});

0 commit comments

Comments
 (0)