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:
    Parser
    • Method Detail

      • 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

        default @NonNull Optional<CharSequence> formatValue​(@Nullable T value)
        Returns an Optional containing the CharSequence that has bean 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

        default @NonNull Optional<String> formatValueAsString​(@Nullable T value)
        Returns an Optional containing the String that has bean 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

        default <Y> @NonNull Formatter<Y> compose​(@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
      • onNull

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

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

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