View Javadoc
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   * Contains utilities for the AndroMDA meta cartridge.
16   *
17   * @author Chad Brandon
18   */
19  public class MetaCartridgeUtils
20  {
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<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       * Used to sort operations by <code>fullyQualifiedName</code>.
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           * @see java.util.Comparator#compare(Object, Object)
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       * Retrieves the fully qualified constraint name given the constraint (this includes the
74       * full name of the context element and the constraint to which it applies).
75       *
76       * @param constraint the constraint of which to retrieve the name.
77       * @return the fully qualified name.
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  }