1 package org.andromda.andromdapp;
2
3 import java.util.LinkedHashMap;
4 import java.util.Map;
5
6 /**
7 * Represents a prompt "condition". That is, a prompt
8 * value will be set if the condition is satisfied.
9 *
10 * @author Chad Brandon
11 */
12 public class Condition
13 {
14 /**
15 * The id of the prompt to which this condition applies.
16 */
17 private String id;
18
19 /**
20 * Gets the id of the prompt to which this condition applies.
21 *
22 * @return Returns the id.
23 */
24 public String getId()
25 {
26 return id;
27 }
28
29 /**
30 * Sets the id of the prompt to which this condition applies.
31 *
32 * @param id The id to set.
33 */
34 public void setId(final String id)
35 {
36 this.id = id;
37 }
38
39 /**
40 * Stores the properties to set if the condition is true.
41 */
42 private final Map<String, Object> properties = new LinkedHashMap<String, Object>();
43
44 /**
45 * Sets the value of the property in the template context
46 * with the given <code>id</code> to have the given <code>value</code>
47 * if this condition is true.
48 *
49 * @param id the identifier of the prompt.
50 * @param value the value to give the prompt.
51 * @param type the fully qualified type name.
52 */
53 public void setProperty(
54 final String id,
55 final String value,
56 final String type)
57 {
58 this.properties.put(
59 id,
60 AndroMDAppUtils.convert(
61 value,
62 type));
63 }
64
65 /**
66 * Gets all properties to set for this condition.
67 *
68 * @return the prompt values.
69 */
70 public Map<String, Object> getProperties()
71 {
72 return this.properties;
73 }
74
75 /**
76 * The value of which the condition must be equal.
77 */
78 private String equal;
79
80 /**
81 * Gets the value of which the condition must be equal.
82 *
83 * @return Returns the equal.
84 */
85 public String getEqual()
86 {
87 return equal;
88 }
89
90 /**
91 * Sets the value of which the condition must be equal.
92 *
93 * @param equal The equal to set.
94 */
95 public void setEqual(final String equal)
96 {
97 this.equal = equal;
98 }
99
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 }