14
14
*/
15
15
package org .jnosql .artemis .util ;
16
16
17
+ import org .jnosql .artemis .AttributeConverter ;
17
18
import org .jnosql .artemis .Converters ;
18
19
import org .jnosql .artemis .reflection .ClassRepresentation ;
19
20
import org .jnosql .artemis .reflection .FieldRepresentation ;
20
21
import org .jnosql .diana .api .Value ;
21
22
22
23
import java .lang .reflect .Field ;
24
+ import java .lang .reflect .ParameterizedType ;
25
+ import java .lang .reflect .Type ;
23
26
import java .util .Optional ;
27
+ import java .util .function .Function ;
28
+ import java .util .function .Predicate ;
24
29
25
30
public class ConverterUtil {
26
31
@@ -30,7 +35,7 @@ private ConverterUtil() {
30
35
}
31
36
32
37
/**
33
- * Converts
38
+ * Converts the value to database format
34
39
*
35
40
* @param value the value
36
41
* @param representation the class representation
@@ -48,24 +53,49 @@ public static Object getValue(Object value, ClassRepresentation representation,
48
53
}
49
54
50
55
/**
51
- * Converts the value from the field with {@link FieldRepresentation}
52
- * @param value the value to be converted
56
+ * Converts the value from the field with {@link FieldRepresentation} to database format
57
+ *
58
+ * @param value the value to be converted
53
59
* @param converters the converter
54
- * @param field the field
60
+ * @param field the field
55
61
* @return tje value converted
56
62
*/
57
63
public static Object getValue (Object value , Converters converters , FieldRepresentation field ) {
58
64
Field nativeField = field .getNativeField ();
59
65
if (!nativeField .getType ().equals (value .getClass ())) {
60
66
return field .getConverter ()
61
67
.map (converters ::get )
62
- .map (a -> a . convertToDatabaseColumn (value ))
68
+ .map (useConverter (value ))
63
69
.orElseGet (() -> Value .of (value ).get (nativeField .getType ()));
64
70
}
65
71
66
72
return field .getConverter ()
67
73
.map (converters ::get )
68
- .map (a -> a . convertToDatabaseColumn (value ))
74
+ .map (useConverter (value ))
69
75
.orElse (value );
70
76
}
77
+
78
+ private static Function <AttributeConverter , Object > useConverter (Object value ) {
79
+ return a -> {
80
+ if (isNative (value ).test (a )) {
81
+ return value ;
82
+ }
83
+ return a .convertToDatabaseColumn (value );
84
+ };
85
+ }
86
+
87
+ private static Predicate <AttributeConverter > isNative (Object value ) {
88
+ return a -> getGenericInterface (a ).getActualTypeArguments ()[1 ].equals (value .getClass ());
89
+ }
90
+
91
+
92
+ private static ParameterizedType getGenericInterface (AttributeConverter a ) {
93
+ for (Type genericInterface : a .getClass ().getGenericInterfaces ()) {
94
+ if (ParameterizedType .class .isAssignableFrom (genericInterface .getClass ()) &&
95
+ ParameterizedType .class .cast (genericInterface ).getRawType ().equals (AttributeConverter .class )) {
96
+ return (ParameterizedType ) genericInterface ;
97
+ }
98
+ }
99
+ throw new IllegalArgumentException ("It does not found AttributeConverter implementation to this converter" );
100
+ }
71
101
}
0 commit comments