Package nbbrd.io.text

Interface Formatter<T>

Type Parameters:
T - The type of the object to be formatted
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 Formatter<T>
Defines a class that creates a CharSequence from an object.
For example, you could use it to format a Date into a String. Note that it can also be used to convert a String to a new one.
The formatter 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 formatter, you should use the NullObject pattern.
Author:
Philippe Charles
See Also:
  • Method Details

    • format

      @Nullable CharSequence format(@Nullable T value)
      Format an object into a CharSequence.
      Parameters:
      value - the input used to create the CharSequence
      Returns:
      a new CharSequence if possible, null otherwise
    • formatAsString

      default @Nullable String formatAsString(@Nullable T value)
      Format an object into a String.
      Parameters:
      value - the input used to create the String
      Returns:
      a new String if possible, null otherwise
    • formatValue

      @NonNull default @NonNull Optional<CharSequence> formatValue(@Nullable T value)
      Returns an Optional containing the CharSequence that has been created by the formatting if this formatting was possible.

      Use this instead of format(java.lang.Object) to increase readability and prevent NullPointerExceptions.

      Parameters:
      value - the input used to create the CharSequence
      Returns:
      a never-null Optional
    • formatValueAsString

      @NonNull default @NonNull Optional<String> formatValueAsString(@Nullable T value)
      Returns an Optional containing the String that has been created by the formatting if this formatting was possible.

      Use this instead of format(java.lang.Object) to increase readability and prevent NullPointerExceptions.

      Parameters:
      value - the input used to create the String
      Returns:
      a never-null Optional
    • compose

      @NonNull default <Y> @NonNull Formatter<Y> compose(@NonNull @NonNull Function<? super Y,? extends T> before)
      Returns a formatter that applies a function on the input value before formatting its result.
      Type Parameters:
      Y -
      Parameters:
      before -
      Returns:
      a never-null formatter
    • onDateTimeFormatter

      @NonNull static <T extends TemporalAccessor> @NonNull Formatter<T> onDateTimeFormatter(@NonNull @NonNull DateTimeFormatter formatter)
    • onDateFormat

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

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

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

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

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

      @NonNull static @NonNull Formatter<Integer> onInteger()
    • onLong

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

      @NonNull static @NonNull Formatter<Double> onDouble()
    • onBoolean

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

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

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

      @NonNull static <T extends Enum<T>> @NonNull Formatter<T> onEnum()
    • onEnum

      @NonNull static <T extends Enum<T>> @NonNull Formatter<T> onEnum(@NonNull @NonNull ToIntFunction<T> function)
    • onString

      @NonNull static @NonNull Formatter<String> onString()
    • onObjectToString

      @NonNull static @NonNull Formatter<Object> onObjectToString()
    • onDoubleArray

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

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

      @NonNull static @NonNull Formatter<List<String>> onStringList(@NonNull @NonNull Function<Stream<CharSequence>,String> joiner)
    • onURL

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

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

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

      @NonNull static <T> @NonNull Formatter<T> of(@NonNull @NonNull Function<? super T,? extends CharSequence> formatter)