View Javadoc
1   package org.andromda.cartridges.ejb;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import org.andromda.metafacades.uml.AttributeFacade;
7   import org.andromda.metafacades.uml.ModelElementFacade;
8   
9   /**
10   * Transform class for the EJB cartridge.
11   *
12   * @author Richard Kunze
13   * @author Chad Brandon
14   */
15  public class EJBScriptHelper
16  {
17      /**
18       * Create a comma separated list of attributes.
19       * <p>
20       * This method can be used to generated e.g. argument lists for constructors, method calls etc.
21       *
22       * @param attributes   a collection of {@link AttributeFacade} objects
23       * @param includeTypes if <code>true</code>, the type names of the attributes are included.
24       * @param includeNames if <code>true</code>, the names of the attributes are included
25       * @return attributes
26       */
27      public String getAttributesAsList(Collection attributes, boolean includeTypes, boolean includeNames)
28      {
29          if (!includeNames && !includeTypes || attributes == null)
30          {
31              return "";
32          }
33  
34          StringBuilder sb = new StringBuilder();
35          String separator = "";
36  
37          for (final Iterator it = attributes.iterator(); it.hasNext();)
38          {
39              AttributeFacade attr = (AttributeFacade)it.next();
40              sb.append(separator);
41              separator = ", ";
42              if (includeTypes)
43              {
44                  sb.append(attr.getType().getFullyQualifiedName());
45                  sb.append(' ');
46              }
47              if (includeNames)
48              {
49                  sb.append(attr.getName());
50              }
51          }
52          return sb.toString();
53      }
54  
55      /**
56       * Filter a list of model elements by visibility.
57       *
58       * @param list       the original list
59       * @param visibility the visibility - "public" "protected", "private" or the empty string (for package visibility)
60       * @return a list with all elements from the original list that have a matching visibility.
61       */
62      public Collection filterByVisibility(Collection list, String visibility)
63      {
64          Collection<ModelElementFacade> result = new ArrayList<ModelElementFacade>(list.size());
65          for (ModelElementFacade elem : (Iterable<ModelElementFacade>) list)
66          {
67              if (visibility.equals(elem.getVisibility()))
68              {
69                  result.add(elem);
70              }
71          }
72          return result;
73      }
74  
75  }