Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PropertyType |
|
|
2.2222222222222223;2,222 |
1 | /* |
|
2 | * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package de.smartics.properties.api.core.domain; |
|
17 | ||
18 | import java.io.Serializable; |
|
19 | import java.util.Collection; |
|
20 | ||
21 | import org.apache.commons.lang.ObjectUtils; |
|
22 | import org.apache.commons.lang.StringUtils; |
|
23 | ||
24 | import de.smartics.util.lang.Arg; |
|
25 | import de.smartics.util.lang.NullArgumentException; |
|
26 | ||
27 | /** |
|
28 | * Provides information about the type of a property. |
|
29 | */ |
|
30 | public final class PropertyType implements Serializable |
|
31 | { |
|
32 | // ********************************* Fields ********************************* |
|
33 | ||
34 | // --- constants ------------------------------------------------------------ |
|
35 | ||
36 | /** |
|
37 | * The class version identifier. |
|
38 | */ |
|
39 | private static final long serialVersionUID = 1L; |
|
40 | ||
41 | // --- members -------------------------------------------------------------- |
|
42 | ||
43 | /** |
|
44 | * The Java type of the property. |
|
45 | * |
|
46 | * @serial |
|
47 | */ |
|
48 | private final Class<?> type; |
|
49 | ||
50 | /** |
|
51 | * The Java type of the elements of the property if the {@link #getType() |
|
52 | * type} is a collection type. The value is derived from the value specified |
|
53 | * as {@link de.smartics.properties.api.core.annotations.PropertyElementType}. |
|
54 | * |
|
55 | * @serial |
|
56 | */ |
|
57 | private final Class<?> elementType; |
|
58 | ||
59 | // ****************************** Initializer ******************************* |
|
60 | ||
61 | // ****************************** Constructors ****************************** |
|
62 | ||
63 | /** |
|
64 | * Convenience constructor for non-collection types. |
|
65 | * |
|
66 | * @param type the Java type of the property. |
|
67 | * @throws NullArgumentException if {@code type} is <code>null</code>. |
|
68 | */ |
|
69 | public PropertyType(final Class<?> type) throws NullArgumentException |
|
70 | { |
|
71 | 27 | this(type, null); |
72 | 27 | } |
73 | ||
74 | /** |
|
75 | * Default constructor. |
|
76 | * |
|
77 | * @param type the Java type of the property. |
|
78 | * @param elementType the Java type of the elements of the property if the |
|
79 | * {@link #getType() type} is a collection type. |
|
80 | * @throws NullArgumentException if {@code type} is <code>null</code>. |
|
81 | */ |
|
82 | public PropertyType(final Class<?> type, final Class<?> elementType) |
|
83 | throws NullArgumentException |
|
84 | 98 | { |
85 | 98 | this.type = Arg.checkNotNull("type", type); |
86 | 98 | this.elementType = calcDefaultElementType(type, elementType); |
87 | 98 | } |
88 | ||
89 | // ****************************** Inner Classes ***************************** |
|
90 | ||
91 | // ********************************* Methods ******************************** |
|
92 | ||
93 | // --- init ----------------------------------------------------------------- |
|
94 | ||
95 | /** |
|
96 | * Calculates the default element type if none is specified but {@code type} |
|
97 | * is a collection. |
|
98 | * |
|
99 | * @see de.smartics.properties.api.core.annotations.PropertyElementType |
|
100 | */ |
|
101 | private static Class<?> calcDefaultElementType(final Class<?> type, |
|
102 | final Class<?> elementType) |
|
103 | { |
|
104 | 98 | if (elementType != null) |
105 | { |
|
106 | 14 | return elementType; |
107 | } |
|
108 | ||
109 | 84 | return (Collection.class.isAssignableFrom(type) ? String.class : null); |
110 | } |
|
111 | ||
112 | // --- get&set -------------------------------------------------------------- |
|
113 | ||
114 | /** |
|
115 | * Returns the Java type of the property. |
|
116 | * |
|
117 | * @return the Java type of the property. |
|
118 | */ |
|
119 | public Class<?> getType() |
|
120 | { |
|
121 | 42 | return type; |
122 | } |
|
123 | ||
124 | /** |
|
125 | * Returns the Java type of the elements of the property if the |
|
126 | * {@link #getType() type} is a collection type. The value is derived from the |
|
127 | * value specified as |
|
128 | * {@link de.smartics.properties.api.core.annotations.PropertyElementType}. |
|
129 | * |
|
130 | * @return the Java type of the elements of the property if the |
|
131 | * {@link #getType() type} is a collection type. |
|
132 | */ |
|
133 | public Class<?> getElementType() |
|
134 | { |
|
135 | 1 | return elementType; |
136 | } |
|
137 | ||
138 | // --- business ------------------------------------------------------------- |
|
139 | ||
140 | // --- object basics -------------------------------------------------------- |
|
141 | ||
142 | /** |
|
143 | * Returns the hash code of the object. |
|
144 | * |
|
145 | * @return the hash code. |
|
146 | */ |
|
147 | @Override |
|
148 | public int hashCode() |
|
149 | { |
|
150 | 114 | return type.hashCode(); |
151 | } |
|
152 | ||
153 | /** |
|
154 | * Returns <code>true</code> if the given object is semantically equal to the |
|
155 | * given object, <code>false</code> otherwise. |
|
156 | * |
|
157 | * @param object the instance to compare to. |
|
158 | * @return <code>true</code> if the given object is semantically equal to the |
|
159 | * given object, <code>false</code> otherwise. |
|
160 | */ |
|
161 | @Override |
|
162 | public boolean equals(final Object object) |
|
163 | { |
|
164 | 5629 | if (this == object) |
165 | { |
|
166 | 348 | return true; |
167 | } |
|
168 | 5281 | else if (object == null || getClass() != object.getClass()) |
169 | { |
|
170 | 3711 | return false; |
171 | } |
|
172 | ||
173 | 1570 | final PropertyType other = (PropertyType) object; |
174 | ||
175 | 1570 | return (type.equals(other.type) && ObjectUtils.equals(elementType, |
176 | other.elementType)); |
|
177 | } |
|
178 | ||
179 | /** |
|
180 | * Returns the simple (no package part) name of the type. |
|
181 | * |
|
182 | * @return the simple name of the type. |
|
183 | */ |
|
184 | public String toSimpleString() |
|
185 | { |
|
186 | 2 | return type.getSimpleName() |
187 | + (elementType != null ? '<' + elementType.getSimpleName() + '>' |
|
188 | : StringUtils.EMPTY); |
|
189 | } |
|
190 | ||
191 | /** |
|
192 | * Returns the name of the type. |
|
193 | * |
|
194 | * @return the name of the type. |
|
195 | */ |
|
196 | @Override |
|
197 | public String toString() |
|
198 | { |
|
199 | 9 | return type.getName() |
200 | + (elementType != null ? '<' + elementType.getName() + '>' |
|
201 | : StringUtils.EMPTY); |
|
202 | } |
|
203 | } |