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.
Defines a class that creates a
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
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 Summary
Modifier and TypeMethodDescriptiondefault <Y> @NonNull Formatter<Y> Returns a formatter that applies a function on the input value before formatting its result.@Nullable CharSequenceFormat 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 been created by the formatting if this formatting was possible.formatValueAsString(@Nullable T value) Returns anOptionalcontaining the String that has been 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 <T> @NonNull Formatter<T> onConstant(@Nullable CharSequence instance) onDateFormat(@NonNull DateFormat dateFormat) static <T extends TemporalAccessor>
@NonNull Formatter<T> onDateTimeFormatter(@NonNull DateTimeFormatter formatter) onDouble()static @NonNull Formatter<double[]> onEnum()onEnum(@NonNull ToIntFunction<T> function) onFile()onLong()static <T> @NonNull Formatter<T> onNull()onNumberFormat(@NonNull NumberFormat numberFormat) onString()onStringList(@NonNull Function<Stream<CharSequence>, String> joiner) onURI()onURL()
-
Method Details
-
format
Format an object into a CharSequence.- Parameters:
value- the input used to create the CharSequence- Returns:
- a new CharSequence if possible,
nullotherwise
-
formatAsString
Format an object into a String.- Parameters:
value- the input used to create the String- Returns:
- a new String if possible,
nullotherwise
-
formatValue
Returns anOptionalcontaining 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
Returns anOptionalcontaining 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
-
onNumberFormat
@NonNull static @NonNull Formatter<Number> onNumberFormat(@NonNull @NonNull NumberFormat numberFormat) -
onConstant
-
onNull
-
onFile
-
onInteger
-
onLong
-
onDouble
-
onBoolean
-
onCharacter
-
onCharset
-
onEnum
-
onEnum
@NonNull static <T extends Enum<T>> @NonNull Formatter<T> onEnum(@NonNull @NonNull ToIntFunction<T> function) -
onString
-
onObjectToString
-
onDoubleArray
-
onStringArray
-
onStringList
-
onURL
-
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)
-