edu.rit.survey
Class SurveyState

java.lang.Object
  extended by edu.rit.survey.SurveyState
All Implemented Interfaces:
Externalizable, Serializable

public class SurveyState
extends Object
implements Externalizable

Class SurveyState encapsulates the state of a survey. The survey state consists of the name; the question; four answers; and the number of votes for each answer.

The survey state's name, question, and answers cannot be changed once constructed. Survey state objects are compared for equality based on the name, question, and answers.

To deal with the lack of a central server in an ad hoc collaborative application, the survey state object keeps track of each participant's vote individually using an instance of class SurveyVote. Each participant is identified by a unique Eoid, which maps to the participant's vote. A timestamp (logical clock value) is associated with each vote as well. The merge() operation combines two survey state objects, retaining the highest-timestamped vote for each participant.

See Also:
Serialized Form

Field Summary
static int NUM_ANSWERS
          The number of answers to a survey question (4).
 
Constructor Summary
SurveyState()
          Construct a new empty survey state object.
SurveyState(String theName, String theQuestion, String[] theAnswer)
          Construct a new survey state object.
 
Method Summary
 boolean equals(Object obj)
          Determine if this survey state is equal to the given object.
 String getAnswer(int i)
          Get one of this survey state's answers.
 String getName()
          Get this survey state's name.
 String getQuestion()
          Get this survey state's question.
 SurveyVote getSurveyVote(Eoid id)
          Get the survey vote object for the given participant ID in this survey state object.
 int getVoteCount(int i)
          Get one of this survey state's vote counts.
 int hashCode()
          Returns a hash code for this survey state.
 void merge(SurveyState theSurveyState)
          Merge the given survey state object into this survey state object.
 void readExternal(ObjectInput in)
          Read this survey state from the given object input stream.
 void recalculate()
          Recalculate the vote counts in this survey state object.
 void setSurveyVote(Eoid id, SurveyVote theSurveyVote)
          Set the survey vote object for the given participant ID in this survey state object.
 String toString()
          Returns a string version of this survey state.
 void writeExternal(ObjectOutput out)
          Write this survey state to the given object output stream.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NUM_ANSWERS

public static final int NUM_ANSWERS
The number of answers to a survey question (4).

See Also:
Constant Field Values
Constructor Detail

SurveyState

public SurveyState()
Construct a new empty survey state object. This constructor is for use only during object deserialization.


SurveyState

public SurveyState(String theName,
                   String theQuestion,
                   String[] theAnswer)
Construct a new survey state object. Initially, the name, question, and answers are given by the arguments and the vote counts are 0.

Parameters:
theName - Survey name.
theQuestion - Survey question.
theAnswer - Array of survey answers.
Throws:
NullPointerException - (unchecked exception) Thrown if theName, theQuestion, theAnswer, or any element of theAnswer is null.
IllegalArgumentException - (unchecked exception) Thrown if theAnswer does not have the correct number of elements.
Method Detail

getName

public String getName()
Get this survey state's name.

Returns:
Name.

getQuestion

public String getQuestion()
Get this survey state's question.

Returns:
Question.

getAnswer

public String getAnswer(int i)
Get one of this survey state's answers.

Parameters:
i - Answer index (0-3).
Returns:
Answer.

getVoteCount

public int getVoteCount(int i)
Get one of this survey state's vote counts.

Parameters:
i - Answer index (0-3).
Returns:
Vote count.

getSurveyVote

public SurveyVote getSurveyVote(Eoid id)
Get the survey vote object for the given participant ID in this survey state object.

Parameters:
id - Participant ID.
Returns:
Survey vote object, or null if there is none.

setSurveyVote

public void setSurveyVote(Eoid id,
                          SurveyVote theSurveyVote)
Set the survey vote object for the given participant ID in this survey state object.

Parameters:
id - Participant ID.
theSurveyVote - Survey vote object.
Throws:
NullPointerException - (unchecked exception) Thrown if theSurveyVote is null.

recalculate

public void recalculate()
Recalculate the vote counts in this survey state object.


merge

public void merge(SurveyState theSurveyState)
Merge the given survey state object into this survey state object. Afterwards, this survey state object reflects the most recent vote for each participant.

Parameters:
theSurveyState - Survey state object to merge.

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Write this survey state to the given object output stream.

Specified by:
writeExternal in interface Externalizable
Parameters:
out - Object output stream.
Throws:
IOException - Thrown if an I/O error occurred.

readExternal

public void readExternal(ObjectInput in)
                  throws IOException
Read this survey state from the given object input stream.

Specified by:
readExternal in interface Externalizable
Parameters:
in - Object input stream.
Throws:
IOException - Thrown if an I/O error occurred.

equals

public boolean equals(Object obj)
Determine if this survey state is equal to the given object.

Overrides:
equals in class Object
Parameters:
obj - Object to test.
Returns:
True if this survey state is equal to obj, false otherwise.

hashCode

public int hashCode()
Returns a hash code for this survey state.

Overrides:
hashCode in class Object
Returns:
Hash code.

toString

public String toString()
Returns a string version of this survey state. The string is just the name of this survey state.

Overrides:
toString in class Object
Returns:
String version.


Copyright © 2001-2006 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.