16
16
package org .springframework .data .mapping .model ;
17
17
18
18
import lombok .Getter ;
19
- import lombok .RequiredArgsConstructor ;
20
19
21
20
import java .beans .FeatureDescriptor ;
22
21
import java .beans .PropertyDescriptor ;
28
27
import org .springframework .util .Assert ;
29
28
30
29
/**
30
+ * Value object to abstract the concept of a property backed by a {@link Field} and / or a {@link PropertyDescriptor}.
31
+ *
31
32
* @author Oliver Gierke
32
33
* @author Christoph Strobl
33
34
*/
34
- @ RequiredArgsConstructor
35
35
public class Property {
36
36
37
37
private final @ Getter Optional <Field > field ;
@@ -45,54 +45,107 @@ private Property(Optional<Field> field, Optional<PropertyDescriptor> descriptor)
45
45
this .field = field ;
46
46
this .descriptor = descriptor ;
47
47
this .hashCode = Lazy .of (this ::computeHashCode );
48
- this .rawType = Lazy
49
- . of (( ) -> field .<Class <?>>map (Field ::getType ).orElseGet (() -> descriptor .map (PropertyDescriptor ::getPropertyType )//
48
+ this .rawType = Lazy . of (
49
+ ( ) -> field .<Class <?>> map (Field ::getType ).orElseGet (() -> descriptor .map (PropertyDescriptor ::getPropertyType )//
50
50
.orElseThrow (IllegalStateException ::new )));
51
51
}
52
52
53
+ /**
54
+ * Creates a new {@link Property} backed by the given field.
55
+ *
56
+ * @param field must not be {@literal null}.
57
+ * @return
58
+ */
53
59
public static Property of (Field field ) {
54
- return of (field , Optional .empty ());
60
+
61
+ Assert .notNull (field , "Field must not be null!" );
62
+
63
+ return new Property (Optional .of (field ), Optional .empty ());
55
64
}
56
65
57
- public static Property of (Field field , Optional <PropertyDescriptor > descriptor ) {
66
+ /**
67
+ * Creates a new {@link Property} backed by the given {@link Field} and {@link PropertyDescriptor}.
68
+ *
69
+ * @param field must not be {@literal null}.
70
+ * @param descriptor must not be {@literal null}.
71
+ * @return
72
+ */
73
+ public static Property of (Field field , PropertyDescriptor descriptor ) {
58
74
59
75
Assert .notNull (field , "Field must not be null!" );
76
+ Assert .notNull (descriptor , "PropertyDescriptor must not be null!" );
60
77
61
- return new Property (Optional .of (field ), descriptor );
78
+ return new Property (Optional .of (field ), Optional . of ( descriptor ) );
62
79
}
63
80
81
+ /**
82
+ * Creates a new {@link Property} for the given {@link PropertyDescriptor}.
83
+ *
84
+ * @param descriptor must not be {@literal null}.
85
+ * @return
86
+ */
64
87
public static Property of (PropertyDescriptor descriptor ) {
65
88
66
89
Assert .notNull (descriptor , "PropertyDescriptor must not be null!" );
90
+
67
91
return new Property (Optional .empty (), Optional .of (descriptor ));
68
92
}
69
93
94
+ /**
95
+ * Returns whether the property is backed by a field.
96
+ *
97
+ * @return
98
+ */
70
99
public boolean isFieldBacked () {
71
100
return field .isPresent ();
72
101
}
73
102
103
+ /**
104
+ * Returns the getter of the property if available and if it matches the type of the property.
105
+ *
106
+ * @return will never be {@literal null}.
107
+ */
74
108
public Optional <Method > getGetter () {
75
109
return descriptor .map (PropertyDescriptor ::getReadMethod )//
76
110
.filter (it -> getType ().isAssignableFrom (it .getReturnType ()));
77
111
}
78
112
113
+ /**
114
+ * Returns the setter of the property if available and if its first (only) parameter matches the type of the property.
115
+ *
116
+ * @return will never be {@literal null}.
117
+ */
79
118
public Optional <Method > getSetter () {
80
119
return descriptor .map (PropertyDescriptor ::getWriteMethod )//
81
120
.filter (it -> it .getParameterTypes ()[0 ].isAssignableFrom (getType ()));
82
121
}
83
122
123
+ /**
124
+ * Returns whether the property exposes a getter or a setter.
125
+ *
126
+ * @return
127
+ */
84
128
public boolean hasAccessor () {
85
129
return getGetter ().isPresent () || getSetter ().isPresent ();
86
130
}
87
131
88
132
/**
89
- * @return
133
+ * Returns the name of the property.
134
+ *
135
+ * @return will never be {@literal null}.
90
136
*/
91
137
public String getName () {
92
- return field .map (Field ::getName ).orElseGet (() -> descriptor .map (FeatureDescriptor ::getName )//
93
- .orElseThrow (IllegalStateException ::new ));
138
+
139
+ return field .map (Field ::getName )//
140
+ .orElseGet (() -> descriptor .map (FeatureDescriptor ::getName )//
141
+ .orElseThrow (IllegalStateException ::new ));
94
142
}
95
143
144
+ /**
145
+ * Returns the type of the property.
146
+ *
147
+ * @return will never be {@literal null}.
148
+ */
96
149
public Class <?> getType () {
97
150
return rawType .get ();
98
151
}
0 commit comments