1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.testdoc.core.doc;
17
18 import java.io.Serializable;
19
20 import org.apache.commons.lang.ObjectUtils;
21 import org.apache.commons.lang.StringUtils;
22
23
24
25
26
27
28
29 public final class Type implements Serializable, Comparable<Type>
30 {
31
32
33
34
35
36
37
38 private static final long serialVersionUID = 1L;
39
40
41
42
43
44
45
46
47 private final String packageName;
48
49
50
51
52
53
54 private final String typeName;
55
56
57
58
59
60
61 private final int preCalcHashCode;
62
63
64
65
66
67
68
69
70
71
72
73
74 public Type(final String type)
75 {
76 checkArguments(type);
77 final int index = type.lastIndexOf('.');
78 if (index != -1)
79 {
80 packageName = type.substring(0, index);
81 typeName = type.substring(index + 1);
82 }
83 else
84 {
85 packageName = null;
86 typeName = type;
87 }
88
89 preCalcHashCode = calculateHashCode();
90 }
91
92
93
94
95
96
97
98 private static void checkArguments(final String type)
99 {
100 if (StringUtils.isBlank(type))
101 {
102 throw new IllegalArgumentException("The type must not be blank.");
103 }
104 if (type.endsWith("."))
105 {
106 throw new IllegalArgumentException(
107 "The type is not valid. It ends with a dot: " + type);
108 }
109 }
110
111 private int calculateHashCode()
112 {
113 int result = 17;
114 result = 37 * result + ObjectUtils.hashCode(packageName);
115 result = 37 * result + ObjectUtils.hashCode(typeName);
116
117 return result;
118 }
119
120
121
122
123
124
125
126
127 public String getPackageName()
128 {
129 return packageName;
130 }
131
132
133
134
135
136
137 public String getTypeName()
138 {
139 return typeName;
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154 @Override
155 public int compareTo(final Type other)
156 {
157 int compare = 0;
158 if (packageName != null)
159 {
160 if (other.packageName != null)
161 {
162 compare = packageName.compareTo(other.packageName);
163 }
164 else
165 {
166 compare = 1;
167 }
168 }
169 else
170 {
171 if (other.packageName != null)
172 {
173 compare = -1;
174 }
175 }
176
177 if (compare == 0)
178 {
179 compare = typeName.compareTo(other.typeName);
180 }
181
182 return compare;
183 }
184
185
186
187
188
189
190 @Override
191 public int hashCode()
192 {
193 return preCalcHashCode;
194 }
195
196
197
198
199
200
201
202
203
204 @Override
205 public boolean equals(final Object object)
206 {
207 if (this == object)
208 {
209 return true;
210 }
211 else if (object == null || getClass() != object.getClass())
212 {
213 return false;
214 }
215
216 final Type other = (Type) object;
217
218 return (ObjectUtils.equals(packageName, other.packageName) && typeName
219 .equals(other.typeName));
220 }
221
222
223
224
225
226
227 @Override
228 public String toString()
229 {
230 return (packageName != null ? packageName + '.' : "") + typeName;
231 }
232 }