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 aCharSequencefrom 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 returnnull. This means thatnullis 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 Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description 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.@Nullable CharSequenceformat(@Nullable T value)Format an object into a CharSequence.default @Nullable StringformatAsString(@Nullable T value)Format an object into a String.default @NonNull Optional<CharSequence>formatValue(@Nullable T value)Returns anOptionalcontaining the CharSequence that has bean created by the formatting if this formatting was possible.default @NonNull Optional<String>formatValueAsString(@Nullable T value)Returns anOptionalcontaining the String that has bean created by the formatting if this formatting was possible.static <T> @NonNull Formatter<T>of(@NonNull Function<? super T,? extends CharSequence> formatter)static <T> @NonNull Formatter<T>of(@NonNull Function<? super T,? extends CharSequence> formatter, @NonNull Consumer<? super Throwable> onError)static @NonNull Formatter<Boolean>onBoolean()static @NonNull Formatter<Character>onCharacter()static @NonNull Formatter<Charset>onCharset()static <T> @NonNull Formatter<T>onConstant(@Nullable CharSequence instance)static @NonNull Formatter<Date>onDateFormat(@NonNull DateFormat dateFormat)static <T extends TemporalAccessor>
@NonNull Formatter<T>onDateTimeFormatter(@NonNull DateTimeFormatter formatter)static @NonNull Formatter<Double>onDouble()static @NonNull Formatter<double[]>onDoubleArray()static <T extends Enum<T>>
@NonNull Formatter<T>onEnum()static <T extends Enum<T>>
@NonNull Formatter<T>onEnum(@NonNull ToIntFunction<T> function)static @NonNull Formatter<File>onFile()static @NonNull Formatter<Integer>onInteger()static @NonNull Formatter<Long>onLong()static <T> @NonNull Formatter<T>onNull()static @NonNull Formatter<Number>onNumberFormat(@NonNull NumberFormat numberFormat)static @NonNull Formatter<Object>onObjectToString()static @NonNull Formatter<String>onString()static @NonNull Formatter<String[]>onStringArray()static @NonNull Formatter<List<String>>onStringList(@NonNull Function<Stream<CharSequence>,String> joiner)static @NonNull Formatter<URL>onURL()
-
-
-
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,
nullotherwise
-
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,
nullotherwise
-
formatValue
default @NonNull Optional<CharSequence> formatValue(@Nullable T value)
Returns anOptionalcontaining 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 anOptionalcontaining 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
-
onDateTimeFormatter
static <T extends TemporalAccessor> @NonNull Formatter<T> onDateTimeFormatter(@NonNull DateTimeFormatter formatter)
-
onDateFormat
static @NonNull Formatter<Date> onDateFormat(@NonNull DateFormat dateFormat)
-
onNumberFormat
static @NonNull Formatter<Number> onNumberFormat(@NonNull NumberFormat numberFormat)
-
onConstant
static <T> @NonNull Formatter<T> onConstant(@Nullable CharSequence instance)
-
onNull
static <T> @NonNull Formatter<T> onNull()
-
onEnum
static <T extends Enum<T>> @NonNull Formatter<T> onEnum(@NonNull ToIntFunction<T> function)
-
onDoubleArray
static @NonNull Formatter<double[]> onDoubleArray()
-
onStringList
static @NonNull Formatter<List<String>> onStringList(@NonNull Function<Stream<CharSequence>,String> joiner)
-
of
static <T> @NonNull Formatter<T> of(@NonNull Function<? super T,? extends CharSequence> formatter, @NonNull Consumer<? super Throwable> onError)
-
of
static <T> @NonNull Formatter<T> of(@NonNull Function<? super T,? extends CharSequence> formatter)
-
-