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.Callable;
13 import org.hibernate.validator.internal.properties.Constrainable;
14 import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
15 import org.hibernate.validator.internal.util.ReflectionHelper;
16
17 /**
18  * Parameter constraint location.
19  *
20  * @author Hardy Ferentschik
21  * @author Gunnar Morling
22  * @author Guillaume Smet
23  */

24 public class ParameterConstraintLocation implements ConstraintLocation {
25
26     private final Callable callable;
27     private final int index;
28     private final Type typeForValidatorResolution;
29     private final ConstraintLocationKind kind;
30
31     public ParameterConstraintLocation(Callable callable, int index) {
32         this.callable = callable;
33         this.index = index;
34         this.typeForValidatorResolution = ReflectionHelper.boxedType( callable.getParameterGenericType( index ) );
35         this.kind = ConstraintLocationKind.of( callable.getConstrainedElementKind() );
36     }
37
38     @Override
39     public Class<?> getDeclaringClass() {
40         return callable.getDeclaringClass();
41     }
42
43     @Override
44     public Constrainable getConstrainable() {
45         return callable;
46     }
47
48     @Override
49     public Type getTypeForValidatorResolution() {
50         return typeForValidatorResolution;
51     }
52
53     public int getIndex() {
54         return index;
55     }
56
57     @Override
58     public void appendTo(ExecutableParameterNameProvider parameterNameProvider, PathImpl path) {
59         path.addParameterNode( callable.getParameterName( parameterNameProvider, index ), index );
60     }
61
62     @Override
63     public Object getValue(Object parent) {
64         return ( (Object[]) parent )[index];
65     }
66
67     @Override
68     public ConstraintLocationKind getKind() {
69         return kind;
70     }
71
72     @Override
73     public String toString() {
74         return "ParameterConstraintLocation [callable=" + callable + ", index=" + index + "]";
75     }
76
77     @Override
78     public int hashCode() {
79         final int prime = 31;
80         int result = 1;
81         result = prime * result + callable.hashCode();
82         result = prime * result + index;
83         return result;
84     }
85
86     @Override
87     public boolean equals(Object obj) {
88         if ( this == obj ) {
89             return true;
90         }
91         if ( obj == null ) {
92             return false;
93         }
94         if ( getClass() != obj.getClass() ) {
95             return false;
96         }
97         ParameterConstraintLocation other = (ParameterConstraintLocation) obj;
98         if ( !callable.equals( other.callable ) ) {
99             return false;
100         }
101         if ( index != other.index ) {
102             return false;
103         }
104         return true;
105     }
106 }
107