Class BaseAsker

java.lang.Object
nz.org.riskscape.wizard.ask.BaseAsker
All Implemented Interfaces:
Asker
Direct Known Subclasses:
AskAsAsker, AttributeAsker, CompatAsker, CompositeAsker, ExpressionAsker

public abstract class BaseAsker extends Object implements Asker

Base implementation of an Asker that cuts down on boiler plate and also binds the user's input (for validation purposes) before returning the unbound value back to the survey (to make sure that it will bind and the UI isn't doing some magic it's not allowed to - keep in mind that while this UI is part of the same process as the riskscape engine, the wizard has been built so that it can work with a future web UI without huge amounts of re-work)

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final nz.org.riskscape.engine.i18n.EnumTranslator
     
    protected final nz.org.riskscape.engine.i18n.TranslationContext
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    BaseAsker(nz.org.riskscape.engine.i18n.TranslationContext context)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract nz.org.riskscape.problem.ResultOrProblems<nz.org.riskscape.wizard.Answer.Response>
    ask(AskRequest input)
    Get a bound value from the user - note that the response's original input is returned from askUnbound(AskRequest).
    nz.org.riskscape.problem.ResultOrProblems<Object>
    Drive the given CliPrompter to answer the given Question.
    protected <T extends Enum<?>>
    List<CliChoice<T>>
    getChoices(Class<T> enumClass)
     
    protected Locale
     
    protected String
    getMessage(String suffix, Object... args)
     
    protected nz.org.riskscape.engine.i18n.MessageSource
     
    protected Optional<String>
    getOptionalMessage(String suffix, Object... args)
    Search for a message for this class, conventionally by prepending the class's name to the code, searching through the class hierarchy as well as looking for short class names as well as long names.
    nz.org.riskscape.wizard.Choice
    pickChoice(List<nz.org.riskscape.wizard.Choice> choices, CliPrompter prompter, nz.org.riskscape.picocli.CommandLine.Help.Ansi.Text prompt)
     
    protected <T extends Enum<?>>
    CliChoice<T>
    toChoice(nz.org.riskscape.engine.i18n.EnumTranslator.EnumEntry<T> entry)
    Allows individual Askers to potentially override how enums get displayed to the user

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface nz.org.riskscape.wizard.ask.Asker

    canAsk
  • Field Details

    • translationContext

      protected final nz.org.riskscape.engine.i18n.TranslationContext translationContext
    • enumTranslator

      protected final nz.org.riskscape.engine.i18n.EnumTranslator enumTranslator
  • Constructor Details

    • BaseAsker

      public BaseAsker(nz.org.riskscape.engine.i18n.TranslationContext context)
  • Method Details

    • getChoices

      protected <T extends Enum<?>> List<CliChoice<T>> getChoices(Class<T> enumClass)
      Returns:
      a list of (translated) CliChoices for a given enum, that the user can pick between
    • toChoice

      protected <T extends Enum<?>> CliChoice<T> toChoice(nz.org.riskscape.engine.i18n.EnumTranslator.EnumEntry<T> entry)

      Allows individual Askers to potentially override how enums get displayed to the user

    • getMessage

      protected String getMessage(String suffix, Object... args)
    • getOptionalMessage

      protected Optional<String> getOptionalMessage(String suffix, Object... args)

      Search for a message for this class, conventionally by prepending the class's name to the code, searching through the class hierarchy as well as looking for short class names as well as long names.

      Searches through wizard resources only (for now - we may fall back to help text for enums for example?)

      For example, a class com.Foo than extends org.Bar would search for messages under 'com.Foo', 'Foo', 'org.Bar', and then 'Bar'.

      Parameters:
      suffix - a message key to append to the class derived part of the code
      args - args to interpolate in to the message
      Returns:
      a message, or empty if none could be found
    • ask

      public abstract nz.org.riskscape.problem.ResultOrProblems<nz.org.riskscape.wizard.Answer.Response> ask(AskRequest input)

      Get a bound value from the user - note that the response's original input is returned from askUnbound(AskRequest). This is partly here because the original API exposed this method on the interface, and keeping this method here keeps the refactoring/change noise down to a minimum while allowing the interface to change in the way we want to preserve the right amount of encapsulation

    • askUnbound

      public nz.org.riskscape.problem.ResultOrProblems<Object> askUnbound(AskRequest input)
      Description copied from interface: Asker

      Drive the given CliPrompter to answer the given Question. Assume that Asker.canAsk(IncrementalBuildState, Question) has already been tested and returned true.

      Specified by:
      askUnbound in interface Asker
      Returns:
      an unbound object that should be able to be bound to a value of the type in the input's question, or problems if it failed
    • getLocale

      protected Locale getLocale()
    • getMessageSource

      protected nz.org.riskscape.engine.i18n.MessageSource getMessageSource()
    • pickChoice

      public nz.org.riskscape.wizard.Choice pickChoice(List<nz.org.riskscape.wizard.Choice> choices, CliPrompter prompter, nz.org.riskscape.picocli.CommandLine.Help.Ansi.Text prompt)