1515 */
1616package org .springframework .data .convert ;
1717
18- import org .springframework .data .convert .PropertyValueConverter .ValueConversionContext ;
18+ import java .util .function .BiFunction ;
19+
1920import org .springframework .data .mapping .PersistentProperty ;
20- import org .springframework .data .util .ClassTypeInformation ;
21- import org .springframework .data .util .TypeInformation ;
2221import org .springframework .lang .Nullable ;
2322
2423/**
2928 * to special annotated fields which allows a fine grained conversion of certain values within a specific context.
3029 *
3130 * @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}.
3434 * @since 2.7
3535 */
3636public interface PropertyValueConverter <A , B , C extends ValueConversionContext <? extends PersistentProperty <?>>> {
@@ -39,126 +39,71 @@ public interface PropertyValueConverter<A, B, C extends ValueConversionContext<?
3939 * Convert the given store specific value into it's domain value representation. Typically a {@literal read}
4040 * operation.
4141 *
42- * @param nativeValue can be {@literal null}.
42+ * @param value can be {@literal null}.
4343 * @param context never {@literal null}.
4444 * @return the converted value. Can be {@literal null}.
4545 */
4646 @ Nullable
47- A /* read*/ nativeToDomain (@ Nullable B nativeValue , C context );
47+ A read (@ Nullable B value , C context );
4848
4949 /**
5050 * Convert the given domain specific value into it's native store representation. Typically a {@literal write}
5151 * operation.
5252 *
53- * @param domainValue can be {@literal null}.
53+ * @param value can be {@literal null}.
5454 * @param context never {@literal null}.
5555 * @return the converted value. Can be {@literal null}.
5656 */
5757 @ Nullable
58- B /* write*/ domainToNative (@ Nullable A domainValue , C context );
58+ B write (@ Nullable A value , C context );
5959
6060 /**
61+ * NoOp {@link PropertyValueConverter} implementation.
62+ *
6163 * @author Christoph Strobl
62- * @author Oliver Drotbohm
6364 */
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 {
10767
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 ;
11869
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 ;
12973 }
13074
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 ;
14178 }
14279 }
14380
14481 /**
145- * NoOp {@link PropertyValueConverter} implementation .
82+ * A {@link PropertyValueConverter} that delegates conversion to the given {@link BiFunction}s .
14683 *
147- * @author Christoph Strobl
84+ * @author Oliver Drotbohm
14885 */
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 >> {
15188
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+ }
15398
15499 @ 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 ) ;
157102 }
158103
159104 @ 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 ) ;
162107 }
163108 }
164109}
0 commit comments