Package nbbrd.io.text

Interface Parser<T>

Type Parameters:
T - The type of the object to be created
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Parser<T>
Defines a class that creates an object from a CharSequence.
For example, you could use it to parse a String into a Date. Note that it can also be used to convert a String to a new one.
The parser must not throw Exceptions; it must swallow it and return null. This means that null is not considered has a value (same as Collection API). To create a "null value" from a parser, you should use the NullObject pattern.
Author:
Philippe Charles
See Also:
  • Method Details

    • parse

      @Nullable T parse(@Nullable CharSequence input)
      Parse a CharSequence to create an object.
      Parameters:
      input - the input used to create the object
      Returns:
      a new object if possible, null otherwise
    • parseValue

      @NonNull default @NonNull Optional<T> parseValue(@Nullable CharSequence input)
      Returns an Optional containing the object that has been created by the parsing if this parsing was possible.

      Use this instead of parse(java.lang.CharSequence) to increase readability and prevent NullPointerExceptions.

      Parameters:
      input - the input used to create the object
      Returns:
      a never-null Optional
    • orElse

      @NonNull default @NonNull Parser<T> orElse(@NonNull @NonNull Parser<T> other)
      Parameters:
      other -
      Returns:
    • andThen

      @NonNull default <X> @NonNull Parser<X> andThen(@NonNull @NonNull Function<? super T,? extends X> after)
    • onDateTimeFormatter

      @NonNull static <T> @NonNull Parser<T> onDateTimeFormatter(@NonNull @NonNull DateTimeFormatter formatter, @NonNull @NonNull TemporalQuery<T>... queries)
    • onDateFormat

      @NonNull static @NonNull Parser<Date> onDateFormat(@NonNull @NonNull DateFormat dateFormat)
    • onNumberFormat

      @NonNull static @NonNull Parser<Number> onNumberFormat(@NonNull @NonNull NumberFormat numberFormat)
    • onConstant

      @NonNull static <T> @NonNull Parser<T> onConstant(@Nullable T instance)
    • onNull

      @NonNull static <T> @NonNull Parser<T> onNull()
    • onFile

      @NonNull static @NonNull Parser<File> onFile()
    • onInteger

      @NonNull static @NonNull Parser<Integer> onInteger()
      Create a Parser that delegates its parsing to Integer.valueOf(java.lang.String).
      Returns:
      a non-null parser
    • onLong

      @NonNull static @NonNull Parser<Long> onLong()
    • onDouble

      @NonNull static @NonNull Parser<Double> onDouble()
      Create a Parser that delegates its parsing to Double.valueOf(java.lang.String).
      Returns:
      a non-null parser
    • onBoolean

      @NonNull static @NonNull Parser<Boolean> onBoolean()
    • onCharacter

      @NonNull static @NonNull Parser<Character> onCharacter()
    • onCharset

      @NonNull static @NonNull Parser<Charset> onCharset()
    • onEnum

      @NonNull static <T extends Enum<T>> @NonNull Parser<T> onEnum(@NonNull @NonNull Class<T> type)
    • onEnum

      @NonNull static <T extends Enum<T>> @NonNull Parser<T> onEnum(@NonNull @NonNull Class<T> type, @NonNull @NonNull ToIntFunction<T> function)
    • onString

      @NonNull static @NonNull Parser<String> onString()
    • onDoubleArray

      @NonNull static @NonNull Parser<double[]> onDoubleArray()
    • onStringArray

      @NonNull static @NonNull Parser<String[]> onStringArray()
    • onStringList

      @NonNull static @NonNull Parser<List<String>> onStringList(@NonNull @NonNull Function<CharSequence,@NonNull Stream<String>> splitter)
    • onLocale

      @NonNull static @NonNull Parser<Locale> onLocale()
    • onURL

      @NonNull static @NonNull Parser<URL> onURL()
    • onURI

      @NonNull static @NonNull Parser<URI> onURI()
    • of

      @NonNull static <T> @NonNull Parser<T> of(@NonNull @NonNull Function<? super CharSequence,? extends T> parser, @NonNull @NonNull Consumer<? super Throwable> onError)
    • of

      @NonNull static <T> @NonNull Parser<T> of(@NonNull @NonNull Function<? super CharSequence,? extends T> parser)