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 bean created by the formatting if this formatting was possible.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 <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 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
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
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
-
onNumberFormat
-
onConstant
-
onNull
-
onFile
-
onInteger
-
onLong
-
onDouble
-
onBoolean
-
onCharacter
-
onCharset
-
onEnum
-
onEnum
-
onString
-
onObjectToString
-
onDoubleArray
-
onStringArray
-
onStringList
-
onURL
-
onURI
-
of
static <T> @NonNull Formatter<T> of(@NonNull Function<? super T, ? extends CharSequence> formatter, @NonNull Consumer<? super Throwable> onError) -
of
-