forked from spring-projects/spring-data-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyValueConverterRegistrar.java
More file actions
205 lines (171 loc) · 7.4 KB
/
PropertyValueConverterRegistrar.java
File metadata and controls
205 lines (171 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright 2022-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.convert;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import org.springframework.data.convert.PropertyValueConverter.FunctionPropertyValueConverter;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.util.MethodInvocationRecorder;
import org.springframework.util.Assert;
/**
* Configuration class used to register a {@link PropertyValueConverter} with
* a {@link SimplePropertyValueConverterRegistry} that can be used in {@link PropertyValueConversions}.
* <p>
* It is possible to register type safe converters via {@link #registerConverter(Class, Function)}
*
* <pre class="code">
* registrar.registerConverter(Person.class, Person::getName) //
* .writing(StringConverter::encrypt) //
* .reading(StringConverter::decrypt);
* </pre>
*
* @author Christoph Strobl
* @author Oliver Drotbohm
* @since 2.7
*/
public class PropertyValueConverterRegistrar<P extends PersistentProperty<P>> {
private final SimplePropertyValueConverterRegistry<P> registry = new SimplePropertyValueConverterRegistry<>();
/**
* Starts a converter registration by pointing to a property of a domain type.
*
* @param <T> the domain type
* @param <S> the property type
* @param type the domain type to obtain the property from
* @param property a {@link Function} to describe the property to be referenced.
* Usually a method handle to a getter.
* @return will never be {@literal null}.
*/
public <T, S> WritingConverterRegistrationBuilder<T, S, P> registerConverter(Class<T> type, Function<T, S> property) {
String propertyName = MethodInvocationRecorder.forProxyOf(type).record(property).getPropertyPath()
.orElseThrow(() -> new IllegalArgumentException("Cannot obtain property name"));
return new WritingConverterRegistrationBuilder<>(type, propertyName, this);
}
/**
* Starts a converter registration by pointing to a property of a domain type.
*
* @param <T> the domain type
* @param <S> the property type
* @param type the domain type to obtain the property from
* @param propertyName a {@link Function} to describe the property to be referenced.
* Usually a method handle to a getter.
* @return will never be {@literal null}.
*/
public <T, S> WritingConverterRegistrationBuilder<T, S, P> registerConverter(Class<T> type, String propertyName,
@SuppressWarnings("unused") Class<S> propertyType) {
return new WritingConverterRegistrationBuilder<>(type, propertyName, this);
}
/**
* Register the given {@link PropertyValueConverter converter} for the given type and property identified by
* its name.
*
* @param type the domain type to obtain the property from
* @param path the property name.
* @param converter the {@link PropertyValueConverter converter} to apply.
* @return this.
*/
public PropertyValueConverterRegistrar<P> registerConverter(Class<?> type, String path,
PropertyValueConverter<?, ?, ? extends ValueConversionContext<?>> converter) {
registry.registerConverter(type, path,
(PropertyValueConverter<?, ?, ? extends ValueConversionContext<P>>) converter);
return this;
}
/**
* Register collected {@link PropertyValueConverter converters} within the given
* {@link ValueConverterRegistry registry}.
*
* @param target {@link ValueConverterRegistry} from which to register {@link PropertyValueConverter converters};
* must not be {@literal null}.
* @throws IllegalArgumentException if the {@link ValueConverterRegistry} is {@literal null}.
* @see ValueConverterRegistry
*/
public void registerConvertersIn(ValueConverterRegistry<P> target) {
Assert.notNull(target, "Target registry must not be null");
registry.getConverterRegistrationMap().forEach((key, value) ->
target.registerConverter(key.type, key.path, value));
}
/**
* Obtain the {@link SimplePropertyValueConverterRegistry}.
*
* @return new instance of {@link SimplePropertyValueConverterRegistry}.
*/
public ValueConverterRegistry<P> buildRegistry() {
return new SimplePropertyValueConverterRegistry<>(registry);
}
/**
* Helper class used to build up a fluent registration API starting with writing.
*
* @author Oliver Drotbohm
*/
public static class WritingConverterRegistrationBuilder<T, S, P extends PersistentProperty<P>> {
private final Consumer<PropertyValueConverter<T, S, ValueConversionContext<P>>> registration;
private final PropertyValueConverterRegistrar<P> config;
WritingConverterRegistrationBuilder(Class<T> type, String property,
PropertyValueConverterRegistrar<P> config) {
this.config = config;
this.registration = converter -> config.registerConverter(type, property, converter);
}
public ReadingConverterRegistrationBuilder<T, S, S, P> writingAsIs() {
return writing((source, context) -> source);
}
public <R> ReadingConverterRegistrationBuilder<T, S, R, P> writing(Function<S, R> writer) {
return writing((source, context) -> writer.apply(source));
}
/**
* Describes how to convert the domain property value into the database native property.
*
* @param <R> the type to be written to the database
* @param writer the property conversion to extract a value written to the database
* @return will never be {@literal null}.
*/
public <R> ReadingConverterRegistrationBuilder<T, S, R, P> writing(
BiFunction<S, ValueConversionContext<P>, R> writer) {
return new ReadingConverterRegistrationBuilder<>(this, writer);
}
}
/**
* Helper class used to build a fluent API to register how to read a database value into a domain object property.
*
* @author Oliver Drotbohm
*/
public static class ReadingConverterRegistrationBuilder<T, S, R, P extends PersistentProperty<P>> {
private final WritingConverterRegistrationBuilder<T, S, P> origin;
private final BiFunction<S, ValueConversionContext<P>, R> writer;
ReadingConverterRegistrationBuilder(WritingConverterRegistrationBuilder<T, S, P> origin,
BiFunction<S, ValueConversionContext<P>, R> writer) {
this.origin = origin;
this.writer = writer;
}
@SuppressWarnings("unchecked")
public PropertyValueConverterRegistrar<P> readingAsIs() {
return reading((source, context) -> (S) source);
}
public PropertyValueConverterRegistrar<P> reading(Function<R, S> reader) {
return reading((source, context) -> reader.apply(source));
}
/**
* Describes how to read a database value into a domain object's property value.
*
* @param reader must not be {@literal null}.
* @return the configured {@link PropertyValueConverterRegistrar}.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public PropertyValueConverterRegistrar<P> reading(BiFunction<R, ValueConversionContext<P>, S> reader) {
origin.registration.accept(new FunctionPropertyValueConverter(writer, reader));
return origin.config;
}
}
}