1 package org.andromda.cartridges.meta;
2
3 import java.text.Collator;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.List;
9 import org.andromda.core.metafacade.MetafacadeConstants;
10 import org.andromda.metafacades.uml.ConstraintFacade;
11 import org.andromda.metafacades.uml.ModelElementFacade;
12 import org.apache.commons.lang.StringUtils;
13
14
15
16
17
18
19 public class MetaCartridgeUtils
20 {
21
22
23
24
25
26
27 public static Collection<ModelElementFacade> sortByFullyQualifiedName(Collection<ModelElementFacade> modelElements)
28 {
29 List<ModelElementFacade> sortedElements = null;
30 if (modelElements != null)
31 {
32 sortedElements = new ArrayList<ModelElementFacade>(modelElements);
33 Collections.sort(
34 sortedElements,
35 new FullyQualifiedNameComparator());
36 }
37 return sortedElements;
38 }
39
40
41
42
43 static final class FullyQualifiedNameComparator
44 implements Comparator
45 {
46 private final Collator collator = Collator.getInstance();
47
48
49
50
51 FullyQualifiedNameComparator()
52 {
53 this.collator.setStrength(Collator.PRIMARY);
54 }
55
56
57
58
59 public int compare(
60 final Object objectA,
61 final Object objectB)
62 {
63 ModelElementFacade a = (ModelElementFacade)objectA;
64 ModelElementFacade b = (ModelElementFacade)objectB;
65
66 return this.collator.compare(
67 a.getFullyQualifiedName(),
68 b.getFullyQualifiedName());
69 }
70 }
71
72
73
74
75
76
77
78
79 public static String getFullyQualifiedConstraintName(final ConstraintFacade constraint)
80 {
81 final StringBuilder name = new StringBuilder();
82 if (constraint != null)
83 {
84 final ModelElementFacade contextElement = constraint.getContextElement();
85 final String contextElementName =
86 contextElement != null ? contextElement.getFullyQualifiedName(true) : null;
87 if (StringUtils.isNotBlank(contextElementName) && contextElementName != null)
88 {
89 name.append(contextElementName.trim());
90 name.append(MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR);
91 }
92 name.append(constraint.getName());
93 }
94 return name.toString();
95 }
96 }