001: package org.andromda.metafacades.emf.uml2;
002:
003: import org.andromda.metafacades.uml.InstanceFacade;
004: import org.apache.commons.collections.CollectionUtils;
005: import org.apache.commons.collections.Predicate;
006: import org.eclipse.uml2.InstanceSpecification;
007: import org.eclipse.uml2.LiteralBoolean;
008: import org.eclipse.uml2.LiteralInteger;
009: import org.eclipse.uml2.LiteralString;
010: import org.eclipse.uml2.Slot;
011: import org.eclipse.uml2.ValueSpecification;
012:
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.Collections;
016: import java.util.List;
017:
018: /**
019: * MetafacadeLogic implementation for org.andromda.metafacades.uml.InstanceFacade.
020: *
021: * @see org.andromda.metafacades.uml.InstanceFacade
022: */
023: public class InstanceFacadeLogicImpl extends InstanceFacadeLogic {
024: /**
025: * Internal value reference in case this instance is supposed to wrap a ValueSpecificstion metaclass
026: */
027: private Object value = null;
028: private boolean valueSet = false;
029:
030: public InstanceFacadeLogicImpl(ObjectInstance metaObject,
031: String context) {
032: super (metaObject, context);
033: }
034:
035: public static InstanceFacade createInstanceFor(
036: ValueSpecification valueSpecification) {
037: final InstanceFacadeLogicImpl instance = new InstanceFacadeLogicImpl(
038: null, null);
039:
040: if (valueSpecification instanceof LiteralString) {
041: instance.value = ((LiteralString) valueSpecification)
042: .getValue();
043: } else if (valueSpecification instanceof LiteralInteger) {
044: instance.value = new Integer(
045: ((LiteralInteger) valueSpecification).getValue());
046: } else if (valueSpecification instanceof LiteralBoolean) {
047: instance.value = Boolean
048: .valueOf(((LiteralBoolean) valueSpecification)
049: .isValue());
050: } else {
051: instance.value = valueSpecification;
052: }
053:
054: instance.valueSet = true;
055: return instance;
056: }
057:
058: protected String handleGetName() {
059: return this .valueSet ? (this .value == null ? null : value
060: .toString()) : super .handleGetName();
061: }
062:
063: /**
064: * In case we wrap a value specification we just want to be able to print out that value when calling toString()
065: */
066: public String toString() {
067: return this .valueSet ? this .getName() : super .toString();
068: }
069:
070: /**
071: * @see org.andromda.metafacades.uml.InstanceFacade#getClassifiers()
072: */
073: protected java.util.Collection handleGetClassifiers() {
074: return this .metaObject.getClassifiers();
075: }
076:
077: /**
078: * @see org.andromda.metafacades.uml.InstanceFacade#getOwnedInstances()
079: */
080: protected java.util.Collection handleGetOwnedInstances() {
081: final Collection ownedElements = new ArrayList(this .metaObject
082: .getOwnedElements());
083: CollectionUtils.filter(ownedElements, new Predicate() {
084: public boolean evaluate(Object object) {
085: return object instanceof InstanceSpecification;
086: }
087: });
088: return CollectionUtils.collect(ownedElements,
089: UmlUtilities.ELEMENT_TRANSFORMER);
090: }
091:
092: /**
093: * Instances do not own Links in UML2 (unlike UML1.4+), this method always returns an empty collection.
094: *
095: * @see org.andromda.metafacades.uml.InstanceFacade#getOwnedLinks()
096: */
097: protected java.util.Collection handleGetOwnedLinks() {
098: return Collections.EMPTY_LIST;
099: }
100:
101: /**
102: * @see org.andromda.metafacades.uml.InstanceFacade#getSlots()
103: */
104: protected java.util.Collection handleGetSlots() {
105: return CollectionUtils.collect(this .metaObject.getSlots(),
106: UmlUtilities.ELEMENT_TRANSFORMER);
107: }
108:
109: /**
110: * @see org.andromda.metafacades.uml.InstanceFacade#getAttributeLinks()
111: */
112: protected Collection handleGetAttributeLinks() {
113: // collect the slots
114: final List slots = new ArrayList(this .metaObject.getSlots());
115: // only retain the slots mapping onto attributes
116: CollectionUtils.filter(slots, new Predicate() {
117: public boolean evaluate(Object object) {
118: return UmlUtilities.ELEMENT_TRANSFORMER
119: .transform(((Slot) object).getDefiningFeature()) instanceof Attribute;
120: }
121: });
122:
123: return CollectionUtils.collect(slots,
124: UmlUtilities.ELEMENT_TRANSFORMER);
125: }
126:
127: /**
128: * @see org.andromda.metafacades.uml.InstanceFacade#getLinkEnds()
129: */
130: protected java.util.Collection handleGetLinkEnds() {
131: // collect the slots
132: final List slots = new ArrayList(this .metaObject.getSlots());
133: // only retain the slots mapping onto association ends
134: CollectionUtils.filter(slots, new Predicate() {
135: public boolean evaluate(Object object) {
136: return UmlUtilities.ELEMENT_TRANSFORMER
137: .transform(((Slot) object).getDefiningFeature()) instanceof AssociationEnd;
138: }
139: });
140:
141: return CollectionUtils.collect(slots,
142: UmlUtilities.ELEMENT_TRANSFORMER);
143: }
144: }
|