Package nbbrd.io.text
Interface Parser<T>
- Type Parameters:
T- The type of the object to be created
- 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 an object from a
For example, you could use it to parse a String into a Date. Note that it can also be used to convert a String to a new one.
The parser must not throw Exceptions; it must swallow it and return
CharSequence.For example, you could use it to parse a String into a Date. Note that it can also be used to convert a String to a new one.
The parser 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 parser, you should use the NullObject pattern.- Author:
- Philippe Charles
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault <X> @NonNull Parser<X> static <T> @NonNull Parser<T> of(@NonNull Function<? super CharSequence, ? extends T> parser) static <T> @NonNull Parser<T> of(@NonNull Function<? super CharSequence, ? extends T> parser, @NonNull Consumer<? super Throwable> onError) static <T> @NonNull Parser<T> onConstant(@Nullable T instance) onDateFormat(@NonNull DateFormat dateFormat) static <T> @NonNull Parser<T> onDateTimeFormatter(@NonNull DateTimeFormatter formatter, @NonNull TemporalQuery<T>... queries) onDouble()Create aParserthat delegates its parsing toDouble.valueOf(java.lang.String).static @NonNull Parser<double[]> onEnum(@NonNull Class<T> type, @NonNull ToIntFunction<T> function) onFile()Create aParserthat delegates its parsing toInteger.valueOf(java.lang.String).onLocale()onLong()static <T> @NonNull Parser<T> onNull()onNumberFormat(@NonNull NumberFormat numberFormat) onString()onStringList(@NonNull Function<CharSequence, @NonNull Stream<String>> splitter) onURI()onURL()@Nullable Tparse(@Nullable CharSequence input) Parse a CharSequence to create an object.parseValue(@Nullable CharSequence input) Returns anOptionalcontaining the object that has been created by the parsing if this parsing was possible.
-
Method Details
-
parse
Parse a CharSequence to create an object.- Parameters:
input- the input used to create the object- Returns:
- a new object if possible,
nullotherwise
-
parseValue
Returns anOptionalcontaining the object that has been created by the parsing if this parsing was possible.Use this instead of
parse(java.lang.CharSequence)to increase readability and prevent NullPointerExceptions.- Parameters:
input- the input used to create the object- Returns:
- a never-null
Optional
-
orElse
- Parameters:
other-- Returns:
-
andThen
-
onDateTimeFormatter
@NonNull static <T> @NonNull Parser<T> onDateTimeFormatter(@NonNull @NonNull DateTimeFormatter formatter, @NonNull @NonNull TemporalQuery<T>... queries) -
onDateFormat
-
onNumberFormat
-
onConstant
-
onNull
-
onFile
-
onInteger
Create aParserthat delegates its parsing toInteger.valueOf(java.lang.String).- Returns:
- a non-null parser
-
onLong
-
onDouble
Create aParserthat delegates its parsing toDouble.valueOf(java.lang.String).- Returns:
- a non-null parser
-
onBoolean
-
onCharacter
-
onCharset
-
onEnum
-
onEnum
@NonNull static <T extends Enum<T>> @NonNull Parser<T> onEnum(@NonNull @NonNull Class<T> type, @NonNull @NonNull ToIntFunction<T> function) -
onString
-
onDoubleArray
-
onStringArray
-
onStringList
-
onLocale
-
onURL
-
onURI
-
of
@NonNull static <T> @NonNull Parser<T> of(@NonNull @NonNull Function<? super CharSequence, ? extends T> parser, @NonNull @NonNull Consumer<? super Throwable> onError) -
of
@NonNull static <T> @NonNull Parser<T> of(@NonNull @NonNull Function<? super CharSequence, ? extends T> parser)
-