01: package org.andromda.cartridges.meta;
02:
03: import java.text.Collator;
04:
05: import java.util.ArrayList;
06: import java.util.Collection;
07: import java.util.Collections;
08: import java.util.Comparator;
09: import java.util.List;
10:
11: import org.andromda.core.metafacade.MetafacadeConstants;
12: import org.andromda.metafacades.uml.ConstraintFacade;
13: import org.andromda.metafacades.uml.ModelElementFacade;
14:
15: /**
16: * Contains utilities for the AndroMDA meta cartridge.
17: *
18: * @author Chad Brandon
19: */
20: public class MetaCartridgeUtils {
21: /**
22: * Sorts model elements by their fully qualified name.
23: *
24: * @param modelElements the collection of model elements to sort.
25: * @return the sorted collection.
26: */
27: public static Collection sortByFullyQualifiedName(
28: Collection modelElements) {
29: List sortedElements = null;
30: if (modelElements != null) {
31: sortedElements = new ArrayList(modelElements);
32: Collections.sort(sortedElements,
33: new FullyQualifiedNameComparator());
34: }
35: return sortedElements;
36: }
37:
38: /**
39: * Used to sort operations by <code>fullyQualifiedName</code>.
40: */
41: final static class FullyQualifiedNameComparator implements
42: Comparator {
43: private final Collator collator = Collator.getInstance();
44:
45: FullyQualifiedNameComparator() {
46: collator.setStrength(Collator.PRIMARY);
47: }
48:
49: public int compare(final Object objectA, final Object objectB) {
50: ModelElementFacade a = (ModelElementFacade) objectA;
51: ModelElementFacade b = (ModelElementFacade) objectB;
52:
53: return collator.compare(a.getFullyQualifiedName(), b
54: .getFullyQualifiedName());
55: }
56: }
57:
58: /**
59: * Retrieves the fully qualified constraint name given the constraint (this includes the
60: * full name of the context element and the constraint to which it applies).
61: *
62: * @param constraint the constraint of which to retrieve the name.
63: * @return the fully qualified name.
64: */
65: public static String getFullyQualifiedConstraintName(
66: final ConstraintFacade constraint) {
67: final StringBuffer name = new StringBuffer();
68: if (constraint != null) {
69: final ModelElementFacade contextElement = constraint
70: .getContextElement();
71: final String contextElementName = contextElement != null ? contextElement
72: .getFullyQualifiedName(true)
73: : null;
74: if (contextElementName != null
75: && contextElementName.trim().length() > 0) {
76: name.append(contextElementName.trim());
77: name
78: .append(MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR);
79: }
80: name.append(constraint.getName());
81: }
82: return name.toString();
83: }
84: }
|