1 /*
2  * Hibernate Validator, declare and validate application constraints
3  *
4  * License: Apache License, Version 2.0
5  * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6  */

7 package org.hibernate.validator.internal.metadata.location;
8
9 import java.lang.reflect.Type;
10
11 import org.hibernate.validator.internal.engine.path.PathImpl;
12 import org.hibernate.validator.internal.properties.Property;
13 import org.hibernate.validator.internal.properties.PropertyAccessor;
14 import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
15
16 /**
17  * An abstract property constraint location.
18  *
19  * @author Marko Bekhta
20  * @author Guillaume Smet
21  */

22 public abstract class AbstractPropertyConstraintLocation<T extends Property> implements ConstraintLocation {
23
24     /**
25      * The property the constraint was defined on.
26      */

27     private final T property;
28
29     private final PropertyAccessor propertyAccessor;
30
31     AbstractPropertyConstraintLocation(T property) {
32         this.property = property;
33         this.propertyAccessor = property.createAccessor();
34     }
35
36     @Override
37     public Class<?> getDeclaringClass() {
38         return property.getDeclaringClass();
39     }
40
41     @Override
42     public T getConstrainable() {
43         return property;
44     }
45
46     public String getPropertyName() {
47         return property.getPropertyName();
48     }
49
50     @Override
51     public Type getTypeForValidatorResolution() {
52         return property.getTypeForValidatorResolution();
53     }
54
55     @Override
56     public void appendTo(ExecutableParameterNameProvider parameterNameProvider, PathImpl path) {
57         path.addPropertyNode( property.getResolvedPropertyName() );
58     }
59
60     @Override
61     public Object getValue(Object parent) {
62         return propertyAccessor.getValueFrom( parent );
63     }
64
65     @Override
66     public String toString() {
67         return getClass().getSimpleName() + " [property=" + property + "]";
68     }
69
70     @Override
71     public boolean equals(Object o) {
72         if ( this == o ) {
73             return true;
74         }
75         if ( o == null || getClass() != o.getClass() ) {
76             return false;
77         }
78
79         AbstractPropertyConstraintLocation<?> that = (AbstractPropertyConstraintLocation<?>) o;
80
81         if ( !property.equals( that.property ) ) {
82             return false;
83         }
84
85         return true;
86     }
87
88     @Override
89     public int hashCode() {
90         return property.hashCode();
91     }
92 }
93