1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.api.config.domain.key;
17
18 import java.io.Serializable;
19
20 import static org.apache.commons.lang.StringUtils.defaultIfBlank;
21 import org.apache.commons.lang.ObjectUtils;
22
23 import de.smartics.util.lang.Arg;
24
25
26
27
28 public final class ApplicationId implements Serializable,
29 Comparable<ApplicationId>
30 {
31
32
33
34
35
36
37
38
39
40
41 private static final long serialVersionUID = 1L;
42
43
44
45
46 public static final ApplicationId ANY_APP = new ApplicationId(null, null,
47 null);
48
49
50
51
52
53
54
55
56 private final String groupId;
57
58
59
60
61
62
63 private final String artifactId;
64
65
66
67
68
69
70 private final String version;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public ApplicationId(final String groupId, final String artifactId,
87 final String version) throws IllegalArgumentException
88 {
89 this.groupId = Arg.checkNotBlankExceptNull("groupId", groupId);
90 this.artifactId = Arg.checkNotBlankExceptNull("artifactId", artifactId);
91 this.version = Arg.checkNotBlankExceptNull("version", version);
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public String getGroupId()
108 {
109 return groupId;
110 }
111
112
113
114
115
116
117 public String getArtifactId()
118 {
119 return artifactId;
120 }
121
122
123
124
125
126
127 public String getVersion()
128 {
129 return version;
130 }
131
132
133
134
135
136
137
138
139
140
141 @Override
142 public int hashCode()
143 {
144 int result = 17;
145 result = 37 * result + ObjectUtils.hashCode(artifactId);
146 result = 37 * result + ObjectUtils.hashCode(groupId);
147 result = 37 * result + ObjectUtils.hashCode(version);
148 return result;
149 }
150
151
152
153
154
155
156
157
158
159 @Override
160 public boolean equals(final Object object)
161 {
162 if (this == object)
163 {
164 return true;
165 }
166 else if (object == null || getClass() != object.getClass())
167 {
168 return false;
169 }
170
171 final ApplicationId other = (ApplicationId) object;
172
173 return (ObjectUtils.equals(artifactId, other.artifactId)
174 && ObjectUtils.equals(groupId, other.groupId) && ObjectUtils
175 .equals(version, other.version));
176 }
177
178
179
180
181
182
183 @Override
184 public int compareTo(final ApplicationId o)
185 {
186 int result = ObjectUtils.compare(artifactId, o.artifactId);
187 if (result == 0)
188 {
189 result = ObjectUtils.compare(groupId, o.groupId);
190 if (result == 0)
191 {
192 result = ObjectUtils.compare(version, o.version);
193 }
194 }
195 return result;
196 }
197
198
199
200
201
202
203
204 public String toPath()
205 {
206 final StringBuilder buffer = new StringBuilder();
207
208 if (groupId != null)
209 {
210 buffer.append(groupId);
211 }
212
213 if (artifactId != null)
214 {
215 buffer.append('/');
216 buffer.append(artifactId);
217 }
218
219 if (version != null)
220 {
221 buffer.append('/');
222 buffer.append(version);
223 }
224
225 return buffer.toString();
226 }
227
228
229
230
231
232
233
234
235
236 @Override
237 public String toString()
238 {
239 final StringBuilder buffer = new StringBuilder();
240
241 if (groupId != null)
242 {
243 buffer.append(groupId);
244 }
245 buffer.append(':');
246
247 if (artifactId != null)
248 {
249 buffer.append(artifactId);
250 }
251 buffer.append(':');
252
253 if (version != null)
254 {
255 buffer.append(version);
256 }
257
258 return buffer.toString();
259 }
260
261
262
263
264
265
266
267
268
269
270 public static ApplicationId valueOf(final String applicationString)
271 throws IllegalArgumentException
272 {
273 if (applicationString == null)
274 {
275 return ApplicationId.ANY_APP;
276 }
277 final String[] applicationParts = applicationString.split(":", -1);
278 final int length = applicationParts.length;
279
280 if (length < 3)
281 {
282 throw new IllegalArgumentException(
283 String
284 .format(
285 "Could not create an instance out of the key %s. It has too less delimiters!",
286 applicationString));
287 }
288 else if (length == 3)
289 {
290 return new ApplicationId(defaultIfBlank(applicationParts[0], null),
291 defaultIfBlank(applicationParts[1], null), defaultIfBlank(
292 applicationParts[2], null));
293 }
294 else
295 {
296 throw new IllegalArgumentException(
297 String
298 .format(
299 "Could not create an instance out of the key %s. It has too many delimiters!",
300 applicationString));
301 }
302 }
303
304 }