Class QuestionTree

java.lang.Object
nz.org.riskscape.wizard.survey2.QuestionTree

public class QuestionTree extends Object

Imposes an ordered-tree structure on to a list of questions to centralize the logic of moving through a set of questions in the order that the questions impose with their dependencies and order in relation to each other.

  • Method Details

    • singleton

      public static QuestionTree singleton(Question question)
    • empty

      public static QuestionTree empty()
    • fromList

      public static QuestionTree fromList(List<Question> questions)

      Builds a QuestionTree by searching through the questions in order and building a tree from their dependencies. Questions that share the same dependency are ordered relative to each other in the list such that any question that is before another is also asked/considered in that order.

    • visitBfs

      public <T> T visitBfs(BiFunction<QuestionTree.Node,T,T> consumer, T data)

      Visit the tree in breadth first order

      Parameters:
      consumer - a function to call that accepts each node that should return some data of the same type to be passed to the next node
      data - some data to pass between visits, could be a list or an AtomicReference or whatever
      Returns:
      the data that was given
    • visitBfs

      public <T> T visitBfs(BiConsumer<QuestionTree.Node,T> consumer, T data)

      Simpler version of visitBfs(BiFunction, Object) that doesn't require the visitor to return data - it just returns whatever was given as the data parameter

    • find

      Find all the nodes in the tree that match the given predicate in breadth-first order

    • findOne

      public Optional<QuestionTree.Node> findOne(Predicate<QuestionTree.Node> predicate)

      Find a single node in the tree using the given predicate

    • findQuestion

      public Optional<QuestionTree.Node> findQuestion(Question question)

      Locate the node that points to the given question

    • isEmpty

      public boolean isEmpty()
      Returns:
      if there are no questions in this tree
    • getNextQuestions

      public List<Question> getNextQuestions(IncrementalBuildState buildState)

      Query the tree to find the list of questions that should be presented to the user as being asked next. The first question must be answered first, even if it is skipped (by supplying an empty response).

    • isComplete

      public boolean isComplete(IncrementalBuildState buildState)
      Returns:
      true if no more questions can be answered from this tree.
    • getSkipped

      public List<Question> getSkipped(IncrementalBuildState buildState, Question chosen)
      Returns:
      a list of questions that are being skipped if the next question to be answered is chosen. These should be skipped via the Survey2#skip(IncrementalBuildState, Question) method.
    • getRoot

      public QuestionTree.Node getRoot()

      The root of the tree - note that the question here is a placeholder and should never be asked - this is to make it simpler to support multiple questions without any dependencies - these questions are children of the root.