/
ConverterUtil.java
101 lines (88 loc) · 3.55 KB
/
ConverterUtil.java
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
/*
* Copyright (c) 2017 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.jnosql.artemis.util;
import org.jnosql.artemis.AttributeConverter;
import org.jnosql.artemis.Converters;
import org.jnosql.artemis.reflection.ClassMapping;
import org.jnosql.artemis.reflection.FieldMapping;
import org.jnosql.diana.api.Value;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
public class ConverterUtil {
private ConverterUtil() {
}
/**
* Converts the value to database format
*
* @param value the value
* @param mapping the class mapped
* @param name the java fieldName
* @param converters the collection of converter
* @return the value converted
*/
public static Object getValue(Object value, ClassMapping mapping, String name, Converters converters) {
Optional<FieldMapping> fieldOptional = mapping.getFieldMapping(name);
if (fieldOptional.isPresent()) {
FieldMapping field = fieldOptional.get();
return getValue(value, converters, field);
}
return value;
}
/**
* Converts the value from the field with {@link FieldMapping} to database format
*
* @param value the value to be converted
* @param converters the converter
* @param field the field
* @return tje value converted
*/
public static Object getValue(Object value, Converters converters, FieldMapping field) {
Field nativeField = field.getNativeField();
if (!nativeField.getType().equals(value.getClass())) {
return field.getConverter()
.map(converters::get)
.map(useConverter(value))
.orElseGet(() -> Value.of(value).get(nativeField.getType()));
}
return field.getConverter()
.map(converters::get)
.map(useConverter(value))
.orElse(value);
}
private static Function<AttributeConverter, Object> useConverter(Object value) {
return a -> {
if (isNative(value).test(a)) {
return value;
}
return a.convertToDatabaseColumn(value);
};
}
private static Predicate<AttributeConverter> isNative(Object value) {
return a -> getGenericInterface(a).getActualTypeArguments()[1].equals(value.getClass());
}
private static ParameterizedType getGenericInterface(AttributeConverter a) {
for (Type genericInterface : a.getClass().getGenericInterfaces()) {
if (ParameterizedType.class.isAssignableFrom(genericInterface.getClass()) &&
((ParameterizedType) genericInterface).getRawType().equals(AttributeConverter.class)) {
return (ParameterizedType) genericInterface;
}
}
throw new IllegalArgumentException("It does not found AttributeConverter implementation to this converter");
}
}