1 package org.andromda.metafacades.uml;
2
3 import java.security.MessageDigest;
4 import java.security.NoSuchAlgorithmException;
5 import java.text.Collator;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.List;
10 import org.apache.commons.collections.CollectionUtils;
11 import org.apache.commons.collections.Predicate;
12 import org.apache.commons.lang.StringUtils;
13
14
15
16
17
18
19
20
21 public class MetafacadeUtils
22 {
23
24
25
26
27
28
29
30
31 public static Object getElementAsType(
32 final Object element,
33 final Class type)
34 {
35 Object elementAsType = null;
36 if (element != null && type != null)
37 {
38 final Class elementClass = element.getClass();
39 if (type.isAssignableFrom(elementClass))
40 {
41 elementAsType = element;
42 }
43 }
44 return elementAsType;
45 }
46
47
48
49
50
51
52
53
54
55 public static void filterByStereotype(
56 final Collection modelElements,
57 final String stereotype)
58 {
59
60 if (StringUtils.isNotBlank(stereotype))
61 {
62 CollectionUtils.filter(
63 modelElements,
64 new Predicate()
65 {
66 public boolean evaluate(Object object)
67 {
68 return ((ModelElementFacade)object).hasStereotype(stereotype);
69 }
70 });
71 }
72 }
73
74
75
76
77
78
79
80
81 public static void filterByType(
82 final Collection modelElements,
83 final Class type)
84 {
85 if (type != null)
86 {
87 CollectionUtils.filter(
88 modelElements,
89 new Predicate()
90 {
91 public boolean evaluate(Object object)
92 {
93 return type.isAssignableFrom(object.getClass());
94 }
95 });
96 }
97 }
98
99
100
101
102
103
104
105
106 public static void filterByNotType(
107 final Collection modelElements,
108 final Class type)
109 {
110 if (type != null)
111 {
112 CollectionUtils.filter(
113 modelElements,
114 new Predicate()
115 {
116 public boolean evaluate(Object object)
117 {
118 return !type.isAssignableFrom(object.getClass());
119 }
120 });
121 }
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 public static String toRelationName(
138 final String roleName,
139 final String targetRoleName,
140 final String separator)
141 {
142 if (roleName.compareTo(targetRoleName) <= 0)
143 {
144 return (roleName + separator + targetRoleName);
145 }
146 return (targetRoleName + separator + roleName);
147 }
148
149
150
151
152
153
154 public static void sortByFullyQualifiedName(final List metafacades)
155 {
156 Collections.sort(
157 metafacades,
158 new FullyQualifiedNameComparator());
159 }
160
161
162
163
164 private static final class FullyQualifiedNameComparator
165 implements Comparator
166 {
167 private final Collator collator = Collator.getInstance();
168
169
170 FullyQualifiedNameComparator()
171 {
172 collator.setStrength(Collator.PRIMARY);
173 }
174
175 public int compare(
176 final Object objectA,
177 final Object objectB)
178 {
179 final ModelElementFacade a = (ModelElementFacade)objectA;
180 final ModelElementFacade b = (ModelElementFacade)objectB;
181 return collator.compare(
182 a.getFullyQualifiedName() != null ? a.getFullyQualifiedName() : "",
183 b.getFullyQualifiedName() != null ? b.getFullyQualifiedName() : "");
184 }
185 }
186
187
188
189
190
191
192
193
194
195
196 public static String getTypedArgumentList(
197 final Collection<ParameterFacade> arguments,
198 final boolean withArgumentNames,
199 final String modifier)
200 {
201 final StringBuilder buffer = new StringBuilder();
202 boolean commaNeeded = false;
203 for (ParameterFacade parameter : arguments)
204 {
205 String type = null;
206 ClassifierFacade classifier = parameter.getType();
207 if (classifier != null)
208 {
209
210 type = parameter.getGetterSetterTypeName();
211 }
212
213 if (commaNeeded)
214 {
215 buffer.append(", ");
216 }
217 if (StringUtils.isNotBlank(modifier))
218 {
219 buffer.append(modifier);
220 buffer.append(' ');
221 }
222 buffer.append(type);
223 if (withArgumentNames)
224 {
225 buffer.append(' ');
226 buffer.append(parameter.getName());
227 }
228 commaNeeded = true;
229 }
230 return buffer.toString();
231 }
232
233
234
235
236
237
238
239
240
241
242
243 public static String getSignature(
244 final String name,
245 Collection<ParameterFacade> arguments,
246 final boolean withArgumentNames,
247 final String argumentModifier)
248 {
249 final StringBuilder signature = new StringBuilder(name);
250 signature.append('(');
251 signature.append(getTypedArgumentList(
252 arguments,
253 withArgumentNames,
254 argumentModifier));
255 signature.append(')');
256 return signature.toString();
257 }
258
259 private static final String at = "@";
260 private static final char period = '.';
261 private static final char underscore = '_';
262
263
264
265
266
267
268
269
270 public static String getEmfTaggedValue(String name)
271 {
272 if (name==null)
273 {
274 return name;
275 }
276 if (name.startsWith(at))
277 {
278 name = name.substring(1);
279 }
280 name = name.replace(period, underscore);
281 return name;
282 }
283
284
285
286
287
288
289
290
291
292 public static String getUml14TaggedValue(String name)
293 {
294 if (name==null)
295 {
296 return name;
297 }
298 if (!name.startsWith(at))
299 {
300 name = at+name;
301 }
302 name = name.replace(underscore, period);
303 return name;
304 }
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319 public static long calculateDefaultSUID(ClassifierFacade object)
320 {
321
322 StringBuilder buffer = new StringBuilder(object.getName());
323
324
325 for (GeneralizableElementFacade generalizableElementFacade : object.getAllGeneralizations())
326 {
327 ClassifierFacade classifier = (ClassifierFacade) generalizableElementFacade;
328 buffer.append(classifier.getName());
329 }
330
331
332 for (AttributeFacade attribute : object.getAttributes())
333 {
334 buffer.append(attribute.getName());
335 buffer.append(attribute.getVisibility());
336 buffer.append(attribute.getType().getName());
337 }
338
339
340 for (OperationFacade operation : object.getOperations())
341 {
342 buffer.append(operation.getName());
343 buffer.append(operation.getVisibility());
344 buffer.append(operation.getReturnType().getName());
345 for (final ParameterFacade parameter : operation.getArguments())
346 {
347 buffer.append(parameter.getName());
348 buffer.append(parameter.getType().getName());
349 }
350 }
351 final String signature = buffer.toString();
352
353 long serialVersionUID = 0L;
354 try
355 {
356 MessageDigest md = MessageDigest.getInstance("SHA");
357 byte[] hashBytes = md.digest(signature.getBytes());
358
359 long hash = 0;
360 for (int ctr = Math.min(hashBytes.length, 8) - 1; ctr >= 0; ctr--)
361 {
362 hash = (hash << 8) | (hashBytes[ctr] & 0xFF);
363 }
364 serialVersionUID = hash;
365 }
366 catch (final NoSuchAlgorithmException ignore)
367 {
368
369 }
370
371 return serialVersionUID;
372 }
373 }