001: package com.xoetrope.survey;
002:
003: import java.util.Vector;
004:
005: /**
006: * Condition contains a question and a set of answers for it.
007: *
008: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
009: * the GNU Public License (GPL), please see license.txt for more details. If
010: * you make commercial use of this software you must purchase a commercial
011: * license from Xoetrope.</p>
012: * <p> $Revision: 1.5 $</p>
013: */
014: public class Condition {
015: protected Question question;
016: protected Vector options;
017: protected Vector answers;
018:
019: /**
020: * Creates a new instance of the condition.
021: * @param q question
022: */
023: public Condition(Question q) {
024: question = q;
025: options = new Vector();
026: answers = new Vector();
027: }
028:
029: /**
030: * Gets the question of this condition
031: * @return the Question instance
032: */
033: public Question getQuestion() {
034: return question;
035: }
036:
037: /**
038: * Gest the question options of this condition
039: * @return Vector containing options.
040: */
041: public Vector getOptions() {
042: return options;
043: }
044:
045: /**
046: * Gets the answers for the options
047: * of this condition.
048: * @return Vector containing answers
049: */
050: public Vector getAnswers() {
051: return answers;
052: }
053:
054: /**
055: * Removes the answer for the given option
056: * @param option option, the answer for which
057: * is to be removed
058: */
059: public void removeAnswer(Option option) {
060: int optionIdx = -1;
061: for (int i = 0; i < options.size(); i++) {
062: Option o = (Option) options.get(i);
063: if (o.equals(option))
064: optionIdx = i;
065: }
066: if (optionIdx >= 0) {
067: options.remove(optionIdx);
068: answers.remove(optionIdx);
069: }
070: }
071:
072: /**
073: * Gets the answer for the given option
074: * @return the answer
075: */
076: public String getAnswer(Option option) {
077: String answer = "";
078: int optionIdx = -1;
079: for (int i = 0; i < options.size() && optionIdx == -1; i++) {
080: Option o = (Option) options.get(i);
081: if (o.equals(option))
082: optionIdx = i;
083: }
084: if (optionIdx >= 0)
085: answer = (String) answers.get(optionIdx);
086: return answer;
087: }
088:
089: /**
090: * Adds a new answer for the specified option
091: * @param option, the answer for which is about
092: * to be added
093: * @param answer for the specified option
094: */
095: public void addAnswer(Option option, String answer) {
096: options.add(option);
097: answers.add(answer);
098: }
099:
100: public boolean equals(Object o) {
101: if (!(o instanceof Condition))
102: return false;
103: Condition condition = (Condition) o;
104:
105: if (!question.equals(condition.getQuestion()))
106: return false;
107:
108: if (options.size() != condition.getOptions().size())
109: return false;
110:
111: for (int i = 0; i < options.size(); i++) {
112: Option option1 = (Option) options.get(i);
113: Option option2 = (Option) condition.getOptions().get(i);
114: if (!option1.equals(option2))
115: return false;
116: }
117:
118: if (answers.size() != condition.getAnswers().size())
119: return false;
120:
121: for (int i = 0; i < answers.size(); i++) {
122: String answer1 = (String) answers.get(i);
123: String answer2 = (String) answers.get(i);
124: if (!answer1.equals(answer2))
125: return false;
126: }
127:
128: return true;
129: }
130: }
|