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