1 | package tudelft.healthpsychology.traumaontologies.answerstate;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.LinkedList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
8 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
9 |
|
---|
10 | import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion;
|
---|
11 | import tudelft.utilities.tree.Tree;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * A list of {@link AnswerState}s, to be answered one by one in given order.
|
---|
15 | */
|
---|
16 | public class ListOfAnswersState implements AnswerState {
|
---|
17 | private final List<AnswerState> answerStates;
|
---|
18 |
|
---|
19 | @JsonCreator
|
---|
20 | public ListOfAnswersState(
|
---|
21 | @JsonProperty("answerStates") List<AnswerState> answerStates) {
|
---|
22 | this.answerStates = answerStates;
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Override
|
---|
26 | public TypedQuestion getOptions(
|
---|
27 | Tree<String, Collection<Property>, OntologyNode> tree) {
|
---|
28 | Integer i = active(tree);
|
---|
29 | return i == null ? null : answerStates.get(i).getOptions(tree);
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public AnswerState with(String answer,
|
---|
34 | Tree<String, Collection<Property>, OntologyNode> tree) {
|
---|
35 | Integer i = active(tree);
|
---|
36 | if (i == null)
|
---|
37 | throw new IllegalStateException("State is final");
|
---|
38 |
|
---|
39 | LinkedList<AnswerState> newAnswersStates = new LinkedList<AnswerState>(
|
---|
40 | answerStates);
|
---|
41 | newAnswersStates.set(i, answerStates.get(i).with(answer, tree));
|
---|
42 | return new ListOfAnswersState(newAnswersStates);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * @return index of the active answerstate in {@link #answerStates}, or null
|
---|
47 | * if no active state.
|
---|
48 | */
|
---|
49 | private Integer active(
|
---|
50 | Tree<String, Collection<Property>, OntologyNode> tree) {
|
---|
51 | for (int i = 0; i < answerStates.size(); i++) {
|
---|
52 | if (answerStates.get(i).getOptions(tree) != null) {
|
---|
53 | return i;
|
---|
54 | }
|
---|
55 | }
|
---|
56 | return null;
|
---|
57 | }
|
---|
58 | }
|
---|