001package org.andromda.andromdapp;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005
006/**
007 * Represents a prompt "condition".  That is, a prompt
008 * value will be set if the condition is satisfied.
009 *
010 * @author Chad Brandon
011 */
012public class Condition
013{
014    /**
015     * The id of the prompt to which this condition applies.
016     */
017    private String id;
018
019    /**
020     * Gets the id of the prompt to which this condition applies.
021     *
022     * @return Returns the id.
023     */
024    public String getId()
025    {
026        return id;
027    }
028
029    /**
030     * Sets the id of the prompt to which this condition applies.
031     *
032     * @param id The id to set.
033     */
034    public void setId(final String id)
035    {
036        this.id = id;
037    }
038
039    /**
040     * Stores the properties to set if the condition is true.
041     */
042    private final Map<String, Object> properties = new LinkedHashMap<String, Object>();
043
044    /**
045     * Sets the value of the property in the template context
046     * with the given <code>id</code> to have the given <code>value</code>
047     * if this condition is true.
048     *
049     * @param id the identifier of the prompt.
050     * @param value the value to give the prompt.
051     * @param type the fully qualified type name.
052     */
053    public void setProperty(
054        final String id,
055        final String value,
056        final String type)
057    {
058        this.properties.put(
059            id,
060            AndroMDAppUtils.convert(
061                value,
062                type));
063    }
064
065    /**
066     * Gets all properties to set for this condition.
067     *
068     * @return the prompt values.
069     */
070    public Map<String, Object> getProperties()
071    {
072        return this.properties;
073    }
074
075    /**
076     * The value of which the condition must be equal.
077     */
078    private String equal;
079
080    /**
081     * Gets the value of which the condition must be equal.
082     *
083     * @return Returns the equal.
084     */
085    public String getEqual()
086    {
087        return equal;
088    }
089
090    /**
091     * Sets the value of which the condition must be equal.
092     *
093     * @param equal The equal to set.
094     */
095    public void setEqual(final String equal)
096    {
097        this.equal = equal;
098    }
099
100    /**
101     * The value of which the condition must not be equal.
102     */
103    private String notEqual;
104
105    /**
106     * Gets the value of which the condition must <strong>not</strong> be equal.
107     *
108     * @return Returns the notEqual.
109     */
110    public String getNotEqual()
111    {
112        return notEqual;
113    }
114
115    /**
116     * Sets the value of which the condition must <strong>not</strong> be equal.
117     *
118     * @param notEqual The notEqual to set.
119     */
120    public void setNotEqual(final String notEqual)
121    {
122        this.notEqual = notEqual;
123    }
124
125    /**
126     * The value of which the condition must be present.
127     */
128    private Boolean present;
129
130    /**
131     * Sets whether or not the condition must be present.
132     *
133     * @param present The present to set.
134     */
135    public void setPresent(final boolean present)
136    {
137        this.present = Boolean.valueOf(present);
138    }
139
140    /**
141     * Evalutes whether or not the value is valid according to this condition.
142     *
143     * @param value the value to evaluate.
144     * @return true/false
145     */
146    public boolean evaluate(Object value)
147    {
148        boolean valid = true;
149        if (this.present != null)
150        {
151            // - if the condition must be present, very that it is
152            if (this.present.booleanValue())
153            {
154                valid = value != null;
155            }
156            else if (!this.present.booleanValue())
157            {
158                // - otherwise verify that the condition is not present (if it shouldn't be)
159                valid = value == null;
160            }
161        }
162        if (valid)
163        {
164            final String equal = this.getEqual();
165            final String notEqual = this.getNotEqual();
166            final boolean equalConditionPresent = equal != null;
167            final boolean notEqualConditionPresent = notEqual != null;
168            value = String.valueOf(value);
169            if (equalConditionPresent && equal != null)
170            {
171                valid = equal.equals(value);
172            }
173            else if (notEqualConditionPresent && notEqual != null)
174            {
175                valid ^= notEqual.equals(value);
176            }
177        }
178        return valid;
179    }
180
181    /**
182     * @see Object#toString()
183     */
184    public String toString()
185    {
186        return super.toString() + "[id=" + this.id + ", equal=" + this.equal + ", notEqual=" + this.notEqual + ']';
187    }
188}