1616package org .springframework .data .mapping .model ;
1717
1818import lombok .Getter ;
19- import lombok .RequiredArgsConstructor ;
2019
2120import java .beans .FeatureDescriptor ;
2221import java .beans .PropertyDescriptor ;
2827import org .springframework .util .Assert ;
2928
3029/**
30+ * Value object to abstract the concept of a property backed by a {@link Field} and / or a {@link PropertyDescriptor}.
31+ *
3132 * @author Oliver Gierke
3233 * @author Christoph Strobl
3334 */
34- @ RequiredArgsConstructor
3535public class Property {
3636
3737 private final @ Getter Optional <Field > field ;
@@ -45,54 +45,107 @@ private Property(Optional<Field> field, Optional<PropertyDescriptor> descriptor)
4545 this .field = field ;
4646 this .descriptor = descriptor ;
4747 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 )//
5050 .orElseThrow (IllegalStateException ::new )));
5151 }
5252
53+ /**
54+ * Creates a new {@link Property} backed by the given field.
55+ *
56+ * @param field must not be {@literal null}.
57+ * @return
58+ */
5359 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 ());
5564 }
5665
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 ) {
5874
5975 Assert .notNull (field , "Field must not be null!" );
76+ Assert .notNull (descriptor , "PropertyDescriptor must not be null!" );
6077
61- return new Property (Optional .of (field ), descriptor );
78+ return new Property (Optional .of (field ), Optional . of ( descriptor ) );
6279 }
6380
81+ /**
82+ * Creates a new {@link Property} for the given {@link PropertyDescriptor}.
83+ *
84+ * @param descriptor must not be {@literal null}.
85+ * @return
86+ */
6487 public static Property of (PropertyDescriptor descriptor ) {
6588
6689 Assert .notNull (descriptor , "PropertyDescriptor must not be null!" );
90+
6791 return new Property (Optional .empty (), Optional .of (descriptor ));
6892 }
6993
94+ /**
95+ * Returns whether the property is backed by a field.
96+ *
97+ * @return
98+ */
7099 public boolean isFieldBacked () {
71100 return field .isPresent ();
72101 }
73102
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+ */
74108 public Optional <Method > getGetter () {
75109 return descriptor .map (PropertyDescriptor ::getReadMethod )//
76110 .filter (it -> getType ().isAssignableFrom (it .getReturnType ()));
77111 }
78112
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+ */
79118 public Optional <Method > getSetter () {
80119 return descriptor .map (PropertyDescriptor ::getWriteMethod )//
81120 .filter (it -> it .getParameterTypes ()[0 ].isAssignableFrom (getType ()));
82121 }
83122
123+ /**
124+ * Returns whether the property exposes a getter or a setter.
125+ *
126+ * @return
127+ */
84128 public boolean hasAccessor () {
85129 return getGetter ().isPresent () || getSetter ().isPresent ();
86130 }
87131
88132 /**
89- * @return
133+ * Returns the name of the property.
134+ *
135+ * @return will never be {@literal null}.
90136 */
91137 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 ));
94142 }
95143
144+ /**
145+ * Returns the type of the property.
146+ *
147+ * @return will never be {@literal null}.
148+ */
96149 public Class <?> getType () {
97150 return rawType .get ();
98151 }
0 commit comments