Class Sentence

java.lang.Object
app.babylon.text.Sentence

public final class Sentence extends Object
  • Constructor Details

    • Sentence

      public Sentence()
  • Method Details

    • firstIn

      public static <T> T firstIn(SliceParser<T> parser, CharSequence sentence)
      Finds the first parsed value in a space-separated sentence.

      Parsers with a parse(CharSequence, int, int) shape bind directly to SliceParser, so callers can scan sentence words without materialising intermediate strings:

       Currency currency = Sentence.firstIn(Currencys::parse, "pay USD 120 tomorrow");
       Currency typed = Sentence.firstIn(TypeParsers.CURRENCY, "pay USD 120 tomorrow");
       

      TypeParser implementations can be used here too because they extend SliceParser.

    • lastIn

      public static <T> T lastIn(SliceParser<T> parser, String sentence)
      Finds the last parsed value in a space-separated sentence.

      For example, this finds the last currency in a sentence:

       Currency currency = Sentence.lastIn(Currencys::parse, "pay USD or EUR tomorrow");
       Currency typed = Sentence.lastIn(TypeParsers.CURRENCY, "pay USD or EUR tomorrow");
       

      The parser receives the original sentence plus each word's start and length, avoiding intermediate strings.

    • lastIn

      public static <T> T lastIn(SliceParser<T> parser, String sentence, char separator)
      Finds the last parsed value in a sentence separated by separator.

      The parser receives the original sentence plus each field's start and length, avoiding intermediate strings.

    • onlyIn

      public static <T> T onlyIn(SliceParser<T> parser, CharSequence sentence)
      Finds the only parsed value in a space-separated sentence, or null when no value or multiple values are found.

      The name is short for "find the only currency in this sentence":

       Currency currency = Sentence.onlyIn(Currencys::parse, "pay USD tomorrow");
       Currency typed = Sentence.onlyIn(TypeParsers.CURRENCY, "pay USD tomorrow");
       

      The parser receives the original sentence plus each word's start and length, avoiding intermediate strings.