Skip to content

Commit 3b0d1e0

Browse files
committed
Polishing.
Reformat code. Tweak documentation wording. See #3596 Original pull request: #3982.
1 parent f58e462 commit 3b0d1e0

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
@@ -185,8 +185,8 @@ protected ConversionContext getConversionContext(ObjectPath path) {
185185

186186
Assert.notNull(path, "ObjectPath must not be null");
187187

188-
return new ConversionContext(this, conversions, path, this::readDocument, this::readCollectionOrArray, this::readMap,
189-
this::readDBRef, this::getPotentiallyConvertedSimpleRead);
188+
return new ConversionContext(this, conversions, path, this::readDocument, this::readCollectionOrArray,
189+
this::readMap, this::readDBRef, this::getPotentiallyConvertedSimpleRead);
190190
}
191191

192192
/**
@@ -414,18 +414,18 @@ public ConversionContext forProperty(String name) {
414414

415415
EntityProjection<?, ?> property = returnedTypeDescriptor.findProperty(name);
416416
if (property == null) {
417-
return new ConversionContext(conversions, path, MappingMongoConverter.this::readDocument, collectionConverter,
417+
return new ConversionContext(sourceConverter, conversions, path, MappingMongoConverter.this::readDocument, collectionConverter,
418418
mapConverter, dbRefConverter, elementConverter);
419419
}
420420

421-
return new ProjectingConversionContext(sourceConverter, conversions, path, collectionConverter, mapConverter, dbRefConverter,
422-
elementConverter, property);
421+
return new ProjectingConversionContext(sourceConverter, conversions, path, collectionConverter, mapConverter,
422+
dbRefConverter, elementConverter, property);
423423
}
424424

425425
@Override
426426
public ConversionContext withPath(ObjectPath currentPath) {
427-
return new ProjectingConversionContext(sourceConverter, conversions, currentPath, collectionConverter, mapConverter,
428-
dbRefConverter, elementConverter, returnedTypeDescriptor);
427+
return new ProjectingConversionContext(sourceConverter, conversions, currentPath, collectionConverter,
428+
mapConverter, dbRefConverter, elementConverter, returnedTypeDescriptor);
429429
}
430430
}
431431

@@ -964,8 +964,9 @@ protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor acce
964964
TypeInformation<?> valueType = ClassTypeInformation.from(obj.getClass());
965965
TypeInformation<?> type = prop.getTypeInformation();
966966

967-
if(conversions.hasPropertyValueConverter(prop)) {
968-
accessor.put(prop, conversions.getPropertyValueConverter(prop).write(obj, new MongoConversionContext(prop, this)));
967+
if (conversions.hasPropertyValueConverter(prop)) {
968+
accessor.put(prop,
969+
conversions.getPropertyValueConverter(prop).write(obj, new MongoConversionContext(prop, this)));
969970
return;
970971
}
971972

@@ -1299,8 +1300,9 @@ private void writeSimpleInternal(@Nullable Object value, Bson bson, String key)
12991300
private void writeSimpleInternal(@Nullable Object value, Bson bson, MongoPersistentProperty property) {
13001301
DocumentAccessor accessor = new DocumentAccessor(bson);
13011302

1302-
if(conversions.hasPropertyValueConverter(property)) {
1303-
accessor.put(property, conversions.getPropertyValueConverter(property).write(value, new MongoConversionContext(property, this)));
1303+
if (conversions.hasPropertyValueConverter(property)) {
1304+
accessor.put(property,
1305+
conversions.getPropertyValueConverter(property).write(value, new MongoConversionContext(property, this)));
13041306
return;
13051307
}
13061308

@@ -1967,9 +1969,9 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19671969
return null;
19681970
}
19691971

1970-
if(context.conversions.hasPropertyValueConverter(property)) {
1971-
1972-
return (T) context.conversions.getPropertyValueConverter(property).read(value, new MongoConversionContext(property, context.sourceConverter));
1972+
if (context.conversions.hasPropertyValueConverter(property)) {
1973+
return (T) context.conversions.getPropertyValueConverter(property).read(value,
1974+
new MongoConversionContext(property, context.sourceConverter));
19731975
}
19741976

19751977
return (T) context.convert(value, property.getTypeInformation());
@@ -2206,7 +2208,8 @@ protected static class ConversionContext {
22062208
final ContainerValueConverter<DBRef> dbRefConverter;
22072209
final ValueConverter<Object> elementConverter;
22082210

2209-
ConversionContext(MongoConverter sourceConverter, org.springframework.data.convert.CustomConversions customConversions, ObjectPath path,
2211+
ConversionContext(MongoConverter sourceConverter,
2212+
org.springframework.data.convert.CustomConversions customConversions, ObjectPath path,
22102213
ContainerValueConverter<Bson> documentConverter, ContainerValueConverter<Collection<?>> collectionConverter,
22112214
ContainerValueConverter<Bson> mapConverter, ContainerValueConverter<DBRef> dbRefConverter,
22122215
ValueConverter<Object> elementConverter) {
@@ -2293,8 +2296,8 @@ public ConversionContext withPath(ObjectPath currentPath) {
22932296

22942297
Assert.notNull(currentPath, "ObjectPath must not be null");
22952298

2296-
return new ConversionContext(sourceConverter, conversions, currentPath, documentConverter, collectionConverter, mapConverter,
2297-
dbRefConverter, elementConverter);
2299+
return new ConversionContext(sourceConverter, conversions, currentPath, documentConverter, collectionConverter,
2300+
mapConverter, dbRefConverter, elementConverter);
22982301
}
22992302

23002303
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.JodaTimeConverters;
4041
import org.springframework.data.convert.PropertyValueConversions;
4142
import org.springframework.data.convert.PropertyValueConverter;
@@ -273,7 +274,8 @@ public MongoConverterConfigurationAdapter registerConverterFactory(ConverterFact
273274
}
274275

275276
/**
276-
* Add {@link Converter converters}, {@link ConverterFactory factories}, ...
277+
* Add {@link Converter converters}, {@link ConverterFactory factories}, {@link ConverterBuilder.ConverterAware
278+
* converter-aware objects}, and {@link GenericConverter generic converters}.
277279
*
278280
* @param converters must not be {@literal null} nor contain {@literal null} values.
279281
* @return this.
@@ -317,6 +319,7 @@ public MongoConverterConfigurationAdapter registerPropertyValueConverterFactory(
317319
*/
318320
public MongoConverterConfigurationAdapter setPropertyValueConversions(PropertyValueConversions valueConversions) {
319321

322+
Assert.notNull(valueConversions, "PropertyValueConversions must not be null");
320323
this.propertyValueConversions = valueConversions;
321324
return this;
322325
}

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)