15
15
*/
16
16
package org .springframework .data .convert ;
17
17
18
- import org .springframework .data .convert .PropertyValueConverter .ValueConversionContext ;
18
+ import java .util .function .BiFunction ;
19
+
19
20
import org .springframework .data .mapping .PersistentProperty ;
20
- import org .springframework .data .util .ClassTypeInformation ;
21
- import org .springframework .data .util .TypeInformation ;
22
21
import org .springframework .lang .Nullable ;
23
22
24
23
/**
29
28
* to special annotated fields which allows a fine grained conversion of certain values within a specific context.
30
29
*
31
30
* @author Christoph Strobl
32
- * @param <A> domain specific type
33
- * @param <B> store native type
31
+ * @param <A> domain specific type.
32
+ * @param <B> store native type.
33
+ * @param <C> the store specific {@link ValueConversionContext conversion context}.
34
34
* @since 2.7
35
35
*/
36
36
public interface PropertyValueConverter <A , B , C extends ValueConversionContext <? extends PersistentProperty <?>>> {
@@ -39,126 +39,71 @@ public interface PropertyValueConverter<A, B, C extends ValueConversionContext<?
39
39
* Convert the given store specific value into it's domain value representation. Typically a {@literal read}
40
40
* operation.
41
41
*
42
- * @param nativeValue can be {@literal null}.
42
+ * @param value can be {@literal null}.
43
43
* @param context never {@literal null}.
44
44
* @return the converted value. Can be {@literal null}.
45
45
*/
46
46
@ Nullable
47
- A /* read*/ nativeToDomain (@ Nullable B nativeValue , C context );
47
+ A read (@ Nullable B value , C context );
48
48
49
49
/**
50
50
* Convert the given domain specific value into it's native store representation. Typically a {@literal write}
51
51
* operation.
52
52
*
53
- * @param domainValue can be {@literal null}.
53
+ * @param value can be {@literal null}.
54
54
* @param context never {@literal null}.
55
55
* @return the converted value. Can be {@literal null}.
56
56
*/
57
57
@ Nullable
58
- B /* write*/ domainToNative (@ Nullable A domainValue , C context );
58
+ B write (@ Nullable A value , C context );
59
59
60
60
/**
61
+ * NoOp {@link PropertyValueConverter} implementation.
62
+ *
61
63
* @author Christoph Strobl
62
- * @author Oliver Drotbohm
63
64
*/
64
- interface ValueConversionContext <P extends PersistentProperty <P >> {
65
-
66
- /**
67
- * Return the {@link PersistentProperty} to be handled.
68
- *
69
- * @return will never be {@literal null}.
70
- */
71
- P getProperty ();
72
-
73
- /**
74
- * Write to whatever type is considered best for the given source.
75
- *
76
- * @param value
77
- * @return
78
- */
79
- @ Nullable
80
- default Object write (@ Nullable Object value ) {
81
- return null ;
82
- }
83
-
84
- /**
85
- * Write as the given type.
86
- *
87
- * @param value can be {@literal null}.
88
- * @param target must not be {@literal null}.
89
- * @return can be {@literal null}.
90
- */
91
- @ Nullable
92
- default <T > T write (@ Nullable Object value , Class <T > target ) {
93
- return write (value , ClassTypeInformation .from (target ));
94
- }
95
-
96
- /**
97
- * Write as the given type.
98
- *
99
- * @param value can be {@literal null}.
100
- * @param target must not be {@literal null}.
101
- * @return can be {@literal null}.
102
- */
103
- @ Nullable
104
- default <T > T write (@ Nullable Object value , TypeInformation <T > target ) {
105
- return null ;
106
- }
65
+ @ SuppressWarnings ({ "rawtypes" , "null" })
66
+ enum ObjectToObjectPropertyValueConverter implements PropertyValueConverter {
107
67
108
- /**
109
- * Reads the value into the type of the current property.
110
- *
111
- * @param value can be {@literal null}.
112
- * @return can be {@literal null}.
113
- */
114
- @ Nullable
115
- default Object read (@ Nullable Object value ) {
116
- return read (value , getProperty ().getTypeInformation ());
117
- }
68
+ INSTANCE ;
118
69
119
- /**
120
- * Reads the value as the given type.
121
- *
122
- * @param value can be {@literal null}.
123
- * @param target must not be {@literal null}.
124
- * @return can be {@literal null}.
125
- */
126
- @ Nullable
127
- default <T > T read (@ Nullable Object value , Class <T > target ) {
128
- return null ;
70
+ @ Override
71
+ public Object read (Object value , ValueConversionContext context ) {
72
+ return value ;
129
73
}
130
74
131
- /**
132
- * Reads the value as the given type.
133
- *
134
- * @param value can be {@literal null}.
135
- * @param target must not be {@literal null}.
136
- * @return can be {@literal null}.
137
- */
138
- @ Nullable
139
- default <T > T read (@ Nullable Object value , TypeInformation <T > target ) {
140
- return null ;
75
+ @ Override
76
+ public Object write (Object value , ValueConversionContext context ) {
77
+ return value ;
141
78
}
142
79
}
143
80
144
81
/**
145
- * NoOp {@link PropertyValueConverter} implementation .
82
+ * A {@link PropertyValueConverter} that delegates conversion to the given {@link BiFunction}s .
146
83
*
147
- * @author Christoph Strobl
84
+ * @author Oliver Drotbohm
148
85
*/
149
- @ SuppressWarnings ({ "rawtypes" , "null" })
150
- enum ObjectToObjectPropertyValueConverter implements PropertyValueConverter {
86
+ class FunctionPropertyValueConverter < A , B , P extends PersistentProperty < P >>
87
+ implements PropertyValueConverter < A , B , ValueConversionContext < P >> {
151
88
152
- INSTANCE ;
89
+ private final BiFunction <A , ValueConversionContext <P >, B > writer ;
90
+ private final BiFunction <B , ValueConversionContext <P >, A > reader ;
91
+
92
+ public FunctionPropertyValueConverter (BiFunction <A , ValueConversionContext <P >, B > writer ,
93
+ BiFunction <B , ValueConversionContext <P >, A > reader ) {
94
+
95
+ this .writer = writer ;
96
+ this .reader = reader ;
97
+ }
153
98
154
99
@ Override
155
- public Object nativeToDomain ( Object value , ValueConversionContext context ) {
156
- return value ;
100
+ public B write ( A value , ValueConversionContext < P > context ) {
101
+ return writer . apply ( value , context ) ;
157
102
}
158
103
159
104
@ Override
160
- public Object domainToNative ( Object value , ValueConversionContext context ) {
161
- return value ;
105
+ public A read ( B value , ValueConversionContext < P > context ) {
106
+ return reader . apply ( value , context ) ;
162
107
}
163
108
}
164
109
}
0 commit comments