001package org.andromda.metafacades.uml14;
002
003import java.util.Collection;
004import org.andromda.metafacades.uml.AttributeFacade;
005import org.andromda.metafacades.uml.ClassifierFacade;
006import org.andromda.metafacades.uml.NameMasker;
007import org.andromda.metafacades.uml.UMLMetafacadeProperties;
008import org.apache.commons.collections.CollectionUtils;
009import org.apache.commons.collections.Predicate;
010import org.apache.commons.lang.BooleanUtils;
011import org.apache.commons.lang.StringUtils;
012import org.omg.uml.foundation.core.Classifier;
013
014/**
015 * MetafacadeLogic implementation for org.andromda.metafacades.uml.EnumerationFacade.
016 *
017 * @see org.andromda.metafacades.uml.EnumerationFacade
018 * @author Bob Fields
019 */
020public class EnumerationFacadeLogicImpl
021        extends EnumerationFacadeLogic
022{
023    private static final long serialVersionUID = -3790826922365025369L;
024
025    /**
026     * @param metaObject
027     * @param context
028     */
029    public EnumerationFacadeLogicImpl(Classifier metaObject, String context)
030    {
031        super(metaObject, context);
032    }
033
034    /**
035     * Overridden to provide name masking.
036     *
037     * @see org.andromda.metafacades.uml.ModelElementFacade#getName()
038     */
039    @Override
040    protected String handleGetName()
041    {
042        final String nameMask = String.valueOf(
043                this.getConfiguredProperty(UMLMetafacadeProperties.ENUMERATION_NAME_MASK));
044        return NameMasker.mask(super.handleGetName(), nameMask);
045    }
046
047    /**
048     * @see org.andromda.metafacades.uml.EnumerationFacade#getLiterals()
049     */
050    @Override
051    protected Collection<AttributeFacade> handleGetLiterals()
052    {
053        Collection<AttributeFacade> literals = this.getAttributes();
054        CollectionUtils.filter(
055            literals,
056            new Predicate()
057            {
058                public boolean evaluate(Object object)
059                {
060                    boolean isLiteral = true;
061                    final AttributeFacade attribute = (AttributeFacade)object;
062                    if (attribute.isEnumerationMember())
063                    {
064                        isLiteral = false;
065                    }
066                    return isLiteral;
067                }
068            }
069        );
070        return literals;
071    }
072
073    /**
074     * @see org.andromda.metafacades.uml.EnumerationFacade#getMemberVariables()
075     */
076    @Override
077    protected Collection<AttributeFacade> handleGetMemberVariables()
078    {
079        Collection<AttributeFacade> variables = super.getAttributes();
080        CollectionUtils.filter(
081            variables,
082            new Predicate()
083            {
084                public boolean evaluate(Object object)
085                {
086                    boolean isMember = false;
087                    final AttributeFacade attribute = (AttributeFacade)object;
088                    if (attribute.isEnumerationMember())
089                    {
090                        isMember = true;
091                    }
092                    return isMember;
093                }
094            }
095        );
096        return variables;
097    }
098
099    /**
100     * @see org.andromda.metafacades.uml.EnumerationFacade#getFromOperationSignature()
101     */
102    @Override
103    protected String handleGetFromOperationSignature()
104    {
105        final StringBuilder signature = new StringBuilder(this.getFromOperationName());
106        final ClassifierFacade type = this.getLiteralType();
107        if (type != null)
108        {
109            signature.append('(');
110            signature.append(type.getFullyQualifiedName());
111            signature.append(" value)");
112        }
113        return signature.toString();
114    }
115
116    /**
117     * @see org.andromda.metafacades.uml.EnumerationFacade#isTypeSafe()
118     */
119    @Override
120    protected boolean handleIsTypeSafe()
121    {
122        return BooleanUtils.toBoolean(
123                String.valueOf(this.getConfiguredProperty(UMLMetafacadeProperties.TYPE_SAFE_ENUMS_ENABLED)));
124    }
125
126    /**
127     * @see org.andromda.metafacades.uml.EnumerationFacade#getFromOperationName()
128     */
129    @Override
130    protected String handleGetFromOperationName()
131    {
132        final StringBuilder name = new StringBuilder("from");
133        final ClassifierFacade type = this.getLiteralType();
134        if (type != null)
135        {
136            name.append(StringUtils.capitalize(type.getName()));
137        }
138        return name.toString();
139    }
140
141    /**
142     * @see org.andromda.metafacades.uml.EnumerationFacade#getLiteralType()
143     */
144    @Override
145    protected ClassifierFacade handleGetLiteralType()
146    {
147        ClassifierFacade type = null;
148        final Collection<AttributeFacade> literals = this.getLiterals();
149        if (literals != null && !literals.isEmpty())
150        {
151            type = ((AttributeFacade)literals.iterator().next()).getType();
152            /*ModelElementFacade literal = (ModelElementFacade)literals.iterator().next();
153            if (literal instanceof AttributeFacade)
154            {
155                type = ((AttributeFacade)literals.iterator().next()).getType();
156            }*/
157        }
158        return type;
159    }
160}