View Javadoc
1   package org.andromda.metafacades.emf.uml22;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.List;
7   import org.andromda.metafacades.uml.InstanceFacade;
8   import org.andromda.metafacades.uml.LinkFacade;
9   import org.apache.commons.collections.CollectionUtils;
10  import org.apache.commons.collections.Predicate;
11  import org.eclipse.uml2.uml.Classifier;
12  import org.eclipse.uml2.uml.Element;
13  import org.eclipse.uml2.uml.InstanceSpecification;
14  import org.eclipse.uml2.uml.LiteralBoolean;
15  import org.eclipse.uml2.uml.LiteralInteger;
16  import org.eclipse.uml2.uml.LiteralString;
17  import org.eclipse.uml2.uml.Slot;
18  import org.eclipse.uml2.uml.ValueSpecification;
19  
20  /**
21   * MetafacadeLogic implementation for org.andromda.metafacades.uml.InstanceFacade.
22   *
23   * @see org.andromda.metafacades.uml.InstanceFacade
24   */
25  public class InstanceFacadeLogicImpl extends InstanceFacadeLogic
26  {
27      private static final long serialVersionUID = 34L;
28      /**
29       * Internal value reference in case this instance is supposed to wrap a ValueSpecificstion metaclass
30       */
31      private Object value = null;
32      private boolean valueSet = false;
33  
34      /**
35       * @param metaObject
36       * @param context
37       */
38      public InstanceFacadeLogicImpl(final ObjectInstance metaObject, final String context)
39      {
40          super(metaObject, context);
41      }
42  
43      /**
44       * @param valueSpecification
45       * @return LiteralInteger or LiteralBoolean or LiteralString
46       */
47      public static InstanceFacade createInstanceFor(final ValueSpecification valueSpecification)
48      {
49          final InstanceFacadeLogicImpl instance = new InstanceFacadeLogicImpl(null, null);
50  
51          if (valueSpecification instanceof LiteralString)
52          {
53              instance.value = ((LiteralString)valueSpecification).getValue();
54          }
55          else if (valueSpecification instanceof LiteralInteger)
56          {
57              instance.value = Integer.valueOf(((LiteralInteger) valueSpecification).getValue());
58          }
59          else if (valueSpecification instanceof LiteralBoolean)
60          {
61              instance.value = Boolean.valueOf(((LiteralBoolean) valueSpecification).isValue());
62          }
63          else
64          {
65              instance.value = valueSpecification;
66          }
67  
68          instance.valueSet = true;
69          return instance;
70      }
71  
72      /**
73       * @see org.andromda.metafacades.emf.uml22.ModelElementFacadeLogicImpl#handleGetName()
74       */
75      @Override
76      protected String handleGetName()
77      {
78          return this.valueSet ? (this.value == null ? null : this.value.toString()) : super.handleGetName();
79      }
80  
81      /**
82       * In case we wrap a value specification we just want to be able to print out that value when calling toString()
83       * @return String getName
84       */
85      @Override
86      public String toString()
87      {
88          return this.valueSet ? this.handleGetName() : super.toString();
89      }
90  
91      /**
92       * @see org.andromda.metafacades.uml.InstanceFacade#getClassifiers()
93       */
94      @Override
95      protected Collection<Classifier> handleGetClassifiers()
96      {
97          return this.metaObject.getClassifiers();
98      }
99  
100     /**
101      * @see org.andromda.metafacades.uml.InstanceFacade#getOwnedInstances()
102      */
103     @Override
104     protected Collection<InstanceSpecification> handleGetOwnedInstances()
105     {
106         final Collection<Element> ownedElements = new ArrayList<Element>(this.metaObject.getOwnedElements());
107         CollectionUtils.filter(ownedElements, new Predicate()
108         {
109             public boolean evaluate(final Object object)
110             {
111                 return object instanceof InstanceSpecification;
112             }
113         });
114         return CollectionUtils.collect(ownedElements, UmlUtilities.ELEMENT_TRANSFORMER);
115     }
116 
117     /**
118      * Instances do not own Links in UML2 (unlike UML1.4+), this method always returns an empty collection.
119      *
120      * @see org.andromda.metafacades.uml.InstanceFacade#getOwnedLinks()
121      */
122     @Override
123     protected Collection<LinkFacade> handleGetOwnedLinks()
124     {
125         return Collections.emptyList();
126     }
127 
128     /**
129      * @see org.andromda.metafacades.uml.InstanceFacade#getSlots()
130      */
131     @Override
132     protected Collection<Slot> handleGetSlots()
133     {
134         return CollectionUtils.collect(this.metaObject.getSlots(), UmlUtilities.ELEMENT_TRANSFORMER);
135     }
136 
137     /**
138      * @see org.andromda.metafacades.uml.InstanceFacade#getAttributeLinks()
139      */
140     @Override
141     protected Collection<Attribute> handleGetAttributeLinks()
142     {
143         // collect the slots
144         final List<Slot> slots = new ArrayList<Slot>(this.metaObject.getSlots());
145         // only retain the slots mapping onto attributes
146         CollectionUtils.filter(slots, new Predicate()
147         {
148             public boolean evaluate(final Object object)
149             {
150                 return UmlUtilities.ELEMENT_TRANSFORMER.transform(
151                     ((Slot)object).getDefiningFeature()) instanceof Attribute;
152             }
153         });
154 
155         return CollectionUtils.collect(slots, UmlUtilities.ELEMENT_TRANSFORMER);
156     }
157 
158     /**
159      * @see org.andromda.metafacades.uml.InstanceFacade#getLinkEnds()
160      */
161     @Override
162     protected Collection<AssociationEnd> handleGetLinkEnds()
163     {
164         // collect the slots
165         final List<Slot> slots = new ArrayList<Slot>(this.metaObject.getSlots());
166         // only retain the slots mapping onto association ends
167         CollectionUtils.filter(slots, new Predicate()
168         {
169             public boolean evaluate(final Object object)
170             {
171                 return UmlUtilities.ELEMENT_TRANSFORMER.transform(
172                     ((Slot)object).getDefiningFeature()) instanceof AssociationEnd;
173             }
174         });
175 
176         return CollectionUtils.collect(slots, UmlUtilities.ELEMENT_TRANSFORMER);
177     }
178 }