View Javadoc

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