Guest User

Untitled

a guest
Jun 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. /**
  2. * A {@link Comparator} for {@link QuestionChoice} objects which orders them
  3. * according to their corresponding choiceIndex values in the
  4. * {@link ChoiceQuestionAssociation} objects of a parent {@link Question}.
  5. * This is necssary because {@link QuestionChoice} objects themselves have
  6. * no sense of order; they are ordered by their association to
  7. * {@link Question}s.
  8. *
  9. * @author Ben
  10. */
  11. private class ChoiceComparator implements Comparator {
  12.  
  13. // Maps a {@link QuestionChoice} to its choiceIndex
  14. Map<QuestionChoice, Integer> choiceIndexMap;
  15.  
  16. /**
  17. * The constructor builds the map from {@link QuestionChoice}s to their
  18. * indicies based on the {@link ChoiceQuestionAssociation} couplings in
  19. * the {@link Question}.
  20. *
  21. * @param question
  22. * the {@link Question} whose
  23. * {@link ChoiceQuestionAssociation}s define the indicies
  24. */
  25. public ChoiceComparator(Question question) {
  26. choiceIndexMap = new HashMap<QuestionChoice, Integer>();
  27. for (ChoiceQuestionAssociation coupling : question.getChoiceCouplings()) {
  28. choiceIndexMap.put(coupling.getChoice(), coupling.getChoiceIndex());
  29. }
  30. }
  31.  
  32. public int compare(Object obj1, Object obj2) {
  33. if (!(obj1 instanceof QuestionChoice) || (!(obj2 instanceof QuestionChoice))) {
  34. throw new IllegalArgumentException("Can only sort QuestionChoice objects.");
  35. }
  36. QuestionChoice c1 = (QuestionChoice) obj1;
  37. QuestionChoice c2 = (QuestionChoice) obj2;
  38. return choiceIndexMap.get(c1) - choiceIndexMap.get(c2);
  39. }
  40. }
Add Comment
Please, Sign In to add comment