source: TraumaOntologies/src/main/java/tudelft/healthpsychology/traumaontologies/answerstate/ListOfAnswersState.java

Last change on this file was 5, checked in by Bart Vastenhouw, 5 years ago

Intermediate update

File size: 1.7 KB
Line 
1package tudelft.healthpsychology.traumaontologies.answerstate;
2
3import java.util.Collection;
4import java.util.LinkedList;
5import java.util.List;
6
7import com.fasterxml.jackson.annotation.JsonCreator;
8import com.fasterxml.jackson.annotation.JsonProperty;
9
10import tudelft.healthpsychology.traumaontologies.questiontypes.TypedQuestion;
11import tudelft.utilities.tree.Tree;
12
13/**
14 * A list of {@link AnswerState}s, to be answered one by one in given order.
15 */
16public 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}
Note: See TracBrowser for help on using the repository browser.