View Javadoc
1   package org.andromda.metafacades.uml14;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import java.util.LinkedHashSet;
7   import java.util.Set;
8   import org.andromda.metafacades.uml.GeneralizableElementFacade;
9   import org.andromda.metafacades.uml.GeneralizationFacade;
10  import org.andromda.metafacades.uml.ModelElementFacade;
11  import org.apache.commons.collections.CollectionUtils;
12  import org.apache.commons.collections.Transformer;
13  import org.omg.uml.foundation.core.GeneralizableElement;
14  import org.omg.uml.foundation.core.Generalization;
15  
16  /**
17   * MetafacadeLogic implementation.
18   *
19   * @see org.andromda.metafacades.uml.GeneralizableElementFacade
20   * @author Bob Fields
21   */
22  public class GeneralizableElementFacadeLogicImpl
23          extends GeneralizableElementFacadeLogic
24  {
25      private static final long serialVersionUID = 34L;
26      /**
27       * @param metaObject
28       * @param context
29       */
30      public GeneralizableElementFacadeLogicImpl(GeneralizableElement metaObject,
31                                                 String context)
32      {
33          super(metaObject, context);
34      }
35  
36      /**
37       * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getAllGeneralizations()
38       */
39      @Override
40      public Collection<GeneralizableElementFacade> handleGetAllGeneralizations()
41      {
42          final Collection<GeneralizableElementFacade> generalizations = new ArrayList<GeneralizableElementFacade>();
43          for (final Iterator<GeneralizableElementFacade> iterator = this.getGeneralizations().iterator(); iterator.hasNext();)
44          {
45              final GeneralizableElementFacade element = iterator.next();
46              generalizations.add(element);
47              generalizations.addAll(element.getAllGeneralizations());
48          }
49          return generalizations;
50      }
51  
52      // ------------- relations ------------------
53  
54      /**
55       * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getGeneralization()
56       */
57      @Override
58      public GeneralizableElement handleGetGeneralization()
59      {
60          GeneralizableElement parent = null;
61          Collection<Generalization> generalizations = metaObject.getGeneralization();
62          if (generalizations != null)
63          {
64              Iterator<Generalization> iterator = generalizations.iterator();
65              if (iterator.hasNext())
66              {
67                  parent = iterator.next().getParent();
68              }
69          }
70          return parent;
71      }
72  
73      /**
74       * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getGeneralizations()
75       */
76      @Override
77      protected Collection<GeneralizableElementFacade> handleGetGeneralizations()
78      {
79          Collection<GeneralizableElement> parents = new LinkedHashSet<GeneralizableElement>();
80          Collection<Generalization> generalizations = metaObject.getGeneralization();
81          if (generalizations != null && !generalizations.isEmpty())
82          {
83              for (Generalization generalization : generalizations)
84              {
85                  parents.add(generalization.getParent());
86              }
87          }
88          return this.shieldedElements(parents);
89      }
90  
91      /**
92       * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getGeneralizationLinks()
93       */
94      @Override
95      protected Collection<GeneralizationFacade> handleGetGeneralizationLinks()
96      {
97          return this.shieldedElements(metaObject.getGeneralization());
98      }
99  
100     /**
101      * @see org.andromda.metafacades.uml.ClassifierFacade#getSpecializations()
102      */
103     @Override
104     public Collection<GeneralizableElement> handleGetSpecializations()
105     {
106         Collection specializations = new ArrayList(UML14MetafacadeUtils.getCorePackage().getAParentSpecialization()
107                 .getSpecialization(this.metaObject));
108         CollectionUtils.transform(specializations, new Transformer()
109         {
110             public Object transform(Object object)
111             {
112                 return ((Generalization)object).getChild();
113             }
114         });
115         return specializations;
116     }
117 
118     /**
119      * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getGeneralizationList()
120      */
121     @Override
122     protected String handleGetGeneralizationList()
123     {
124         final StringBuilder list = new StringBuilder();
125         if (this.getGeneralizations() != null)
126         {
127             for (final Iterator iterator = this.getGeneralizations().iterator(); iterator.hasNext();)
128             {
129                 list.append(((ModelElementFacade)iterator.next()).getFullyQualifiedName());
130                 if (iterator.hasNext())
131                 {
132                     list.append(", ");
133                 }
134             }
135         }
136         return list.toString();
137     }
138 
139     /**
140      * @see org.andromda.metafacades.uml.GeneralizableElementFacade#getAllSpecializations()
141      */
142     @Override
143     protected Collection<GeneralizableElementFacade> handleGetAllSpecializations()
144     {
145         final Set<GeneralizableElementFacade> allSpecializations = new LinkedHashSet<GeneralizableElementFacade>();
146         if (this.getSpecializations() != null)
147         {
148             allSpecializations.addAll(this.getSpecializations());
149             for (final Iterator iterator = this.getSpecializations().iterator(); iterator.hasNext();)
150             {
151                 final GeneralizableElementFacade element = (GeneralizableElementFacade)iterator.next();
152                 allSpecializations.addAll(element.getAllSpecializations());
153             }
154         }
155         return allSpecializations;
156     }
157 
158     /**
159      * @see org.andromda.metafacades.uml.GeneralizableElementFacade#findTaggedValue(String, boolean)
160      */
161     @Override
162     protected Object handleFindTaggedValue(final String tagName, boolean follow)
163     {
164         Object value = this.findTaggedValue(tagName);
165         if (value == null)
166         {
167             for (GeneralizableElementFacade element = this.getGeneralization();
168                  value == null && element != null; element = element.getGeneralization())
169            {
170                value = element.findTaggedValue(tagName, follow);
171            }
172         }
173         return value;
174     }
175 
176     /**
177      * @see org.andromda.metafacades.uml14.GeneralizableElementFacadeLogic#handleGetGeneralizationRoot()
178      */
179     protected GeneralizableElementFacade handleGetGeneralizationRoot()
180     {
181         return this.getGeneralization() == null
182             ? (GeneralizableElementFacade)THIS()
183             : this.getGeneralization().getGeneralizationRoot();
184     }
185 }