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.engine.valueextraction;
8
9 import java.lang.annotation.Annotation;
10 import java.lang.reflect.AnnotatedArrayType;
11 import java.lang.reflect.AnnotatedType;
12 import java.lang.reflect.Type;
13 import java.lang.reflect.TypeVariable;
14
15 import org.hibernate.validator.internal.util.ReflectionHelper;
16
17 /**
18  * A pseudo type variable which is used to represent constraints applied to the elements of an array.
19  *
20  * @author Gunnar Morling
21  * @author Guillaume Smet
22  */

23 public class ArrayElement implements TypeVariable<Class<?>> {
24
25     private final Class<?> containerClass;
26
27     public ArrayElement(AnnotatedArrayType annotatedArrayType) {
28         Type arrayElementType = annotatedArrayType.getAnnotatedGenericComponentType().getType();
29         if ( arrayElementType == boolean.class ) {
30             containerClass = boolean[].class;
31         }
32         else if ( arrayElementType == int.class ) {
33             containerClass = int[].class;
34         }
35         else if ( arrayElementType == long.class ) {
36             containerClass = long[].class;
37         }
38         else if ( arrayElementType == double.class ) {
39             containerClass = double[].class;
40         }
41         else if ( arrayElementType == float.class ) {
42             containerClass = float[].class;
43         }
44         else if ( arrayElementType == byte.class ) {
45             containerClass = byte[].class;
46         }
47         else if ( arrayElementType == short.class ) {
48             containerClass = short[].class;
49         }
50         else if ( arrayElementType == char.class ) {
51             containerClass = char[].class;
52         }
53         else {
54             containerClass = Object[].class;
55         }
56     }
57
58     public ArrayElement(Type arrayType) {
59         Class<?> arrayClass = ReflectionHelper.getClassFromType( arrayType );
60         if ( arrayClass.getComponentType().isPrimitive() ) {
61             this.containerClass = arrayClass;
62         }
63         else {
64             this.containerClass = Object[].class;
65         }
66     }
67
68     @Override
69     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
70         throw new UnsupportedOperationException();
71     }
72
73     @Override
74     public Annotation[] getAnnotations() {
75         throw new UnsupportedOperationException();
76     }
77
78     @Override
79     public Annotation[] getDeclaredAnnotations() {
80         throw new UnsupportedOperationException();
81     }
82
83     @Override
84     public Type[] getBounds() {
85         throw new UnsupportedOperationException();
86     }
87
88     @Override
89     public Class<?> getGenericDeclaration() {
90         throw new UnsupportedOperationException();
91     }
92
93     @Override
94     public String getName() {
95         throw new UnsupportedOperationException();
96     }
97
98     @Override
99     public AnnotatedType[] getAnnotatedBounds() {
100         throw new UnsupportedOperationException();
101     }
102
103     public Class<?> getContainerClass() {
104         return containerClass;
105     }
106
107     @Override
108     public boolean equals(Object obj) {
109         if ( this == obj ) {
110             return true;
111         }
112         if ( obj == null ) {
113             return false;
114         }
115         if ( getClass() != obj.getClass() ) {
116             return false;
117         }
118         ArrayElement other = (ArrayElement) obj;
119
120         return this.containerClass.equals( other.containerClass );
121     }
122
123     @Override
124     public int hashCode() {
125         final int prime = 31;
126         int result = 1;
127         result = prime * result + containerClass.hashCode();
128         return result;
129     }
130
131     @Override
132     public String toString() {
133         StringBuilder sb = new StringBuilder();
134         sb.append( getClass().getSimpleName() )
135                 .append( "<" )
136                 .append( containerClass )
137                 .append( ">" );
138         return sb.toString();
139     }
140 }
141