Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A {@link Comparator} for {@link QuestionChoice} objects which orders them
- * according to their corresponding choiceIndex values in the
- * {@link ChoiceQuestionAssociation} objects of a parent {@link Question}.
- * This is necssary because {@link QuestionChoice} objects themselves have
- * no sense of order; they are ordered by their association to
- * {@link Question}s.
- *
- * @author Ben
- */
- private class ChoiceComparator implements Comparator {
- // Maps a {@link QuestionChoice} to its choiceIndex
- Map<QuestionChoice, Integer> choiceIndexMap;
- /**
- * The constructor builds the map from {@link QuestionChoice}s to their
- * indicies based on the {@link ChoiceQuestionAssociation} couplings in
- * the {@link Question}.
- *
- * @param question
- * the {@link Question} whose
- * {@link ChoiceQuestionAssociation}s define the indicies
- */
- public ChoiceComparator(Question question) {
- choiceIndexMap = new HashMap<QuestionChoice, Integer>();
- for (ChoiceQuestionAssociation coupling : question.getChoiceCouplings()) {
- choiceIndexMap.put(coupling.getChoice(), coupling.getChoiceIndex());
- }
- }
- public int compare(Object obj1, Object obj2) {
- if (!(obj1 instanceof QuestionChoice) || (!(obj2 instanceof QuestionChoice))) {
- throw new IllegalArgumentException("Can only sort QuestionChoice objects.");
- }
- QuestionChoice c1 = (QuestionChoice) obj1;
- QuestionChoice c2 = (QuestionChoice) obj2;
- return choiceIndexMap.get(c1) - choiceIndexMap.get(c2);
- }
- }
Add Comment
Please, Sign In to add comment