-
Notifications
You must be signed in to change notification settings - Fork 682
/
Copy pathPropertyValueConverter.java
113 lines (98 loc) · 3.49 KB
/
PropertyValueConverter.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
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright 2021 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 org.springframework.data.mapping.PersistentProperty;
import org.springframework.lang.Nullable;
/**
* {@link PropertyValueConverter} provides a symmetric way of converting certain properties from domain to
* store-specific values.
* <p>
* A {@link PropertyValueConverter} is, other than a {@link ReadingConverter} or {@link WritingConverter}, only applied
* to special annotated fields which allows a fine-grained conversion of certain values within a specific context.
*
* @author Christoph Strobl
* @param <A> domain specific type.
* @param <B> store native type.
* @param <C> the store specific {@link ValueConversionContext conversion context}.
* @since 2.7
*/
public interface PropertyValueConverter<A, B, C extends ValueConversionContext<? extends PersistentProperty<?>>> {
/**
* Convert the given store specific value into it's domain value representation. Typically, a {@literal read}
* operation.
*
* @param value can be {@literal null}.
* @param context never {@literal null}.
* @return the converted value. Can be {@literal null}.
*/
@Nullable
A read(@Nullable B value, C context);
/**
* Convert the given domain specific value into it's native store representation. Typically, a {@literal write}
* operation.
*
* @param value can be {@literal null}.
* @param context never {@literal null}.
* @return the converted value. Can be {@literal null}.
*/
@Nullable
B write(@Nullable A value, C context);
/**
* NoOp {@link PropertyValueConverter} implementation.
*
* @author Christoph Strobl
*/
@SuppressWarnings({ "rawtypes", "null" })
enum ObjectToObjectPropertyValueConverter implements PropertyValueConverter {
INSTANCE;
@Nullable
@Override
public Object read(@Nullable Object value, ValueConversionContext context) {
return value;
}
@Nullable
@Override
public Object write(@Nullable Object value, ValueConversionContext context) {
return value;
}
}
/**
* A {@link PropertyValueConverter} that delegates conversion to the given {@link BiFunction}s.
*
* @author Oliver Drotbohm
*/
class FunctionPropertyValueConverter<A, B, P extends PersistentProperty<P>>
implements PropertyValueConverter<A, B, ValueConversionContext<P>> {
private final BiFunction<A, ValueConversionContext<P>, B> writer;
private final BiFunction<B, ValueConversionContext<P>, A> reader;
public FunctionPropertyValueConverter(BiFunction<A, ValueConversionContext<P>, B> writer,
BiFunction<B, ValueConversionContext<P>, A> reader) {
this.writer = writer;
this.reader = reader;
}
@Nullable
@Override
public B write(@Nullable A value, ValueConversionContext<P> context) {
return writer.apply(value, context);
}
@Nullable
@Override
public A read(@Nullable B value, ValueConversionContext<P> context) {
return reader.apply(value, context);
}
}
}