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.
@FunctionalInterface public interface Parser<T>
Defines a class that creates an object from aCharSequence.
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 returnnull. This means thatnullis 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:
Formatter
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <X> @NonNull Parser<X>andThen(@NonNull Function<? super T,? extends X> after)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 @NonNull Parser<Boolean>onBoolean()static @NonNull Parser<Character>onCharacter()static @NonNull Parser<Charset>onCharset()static <T> @NonNull Parser<T>onConstant(@Nullable T instance)static @NonNull Parser<Date>onDateFormat(@NonNull DateFormat dateFormat)static <T> @NonNull Parser<T>onDateTimeFormatter(@NonNull DateTimeFormatter formatter, TemporalQuery<T>... queries)static @NonNull Parser<Double>onDouble()Create aParserthat delegates its parsing toDouble.valueOf(java.lang.String).static @NonNull Parser<double[]>onDoubleArray()static <T extends Enum<T>>
@NonNull Parser<T>onEnum(@NonNull Class<T> type)static <T extends Enum<T>>
@NonNull Parser<T>onEnum(Class<T> type, ToIntFunction<T> function)static @NonNull Parser<File>onFile()static @NonNull Parser<Integer>onInteger()Create aParserthat delegates its parsing toInteger.valueOf(java.lang.String).static @NonNull Parser<Locale>onLocale()static @NonNull Parser<Long>onLong()static <T> @NonNull Parser<T>onNull()static @NonNull Parser<Number>onNumberFormat(@NonNull NumberFormat numberFormat)static @NonNull Parser<String>onString()static @NonNull Parser<String[]>onStringArray()static @NonNull Parser<List<String>>onStringList(@NonNull Function<CharSequence,Stream<String>> splitter)static @NonNull Parser<URL>onURL()default @NonNull Parser<T>orElse(@NonNull Parser<T> other)@Nullable Tparse(@Nullable CharSequence input)Parse a CharSequence to create an object.default @NonNull Optional<T>parseValue(@Nullable CharSequence input)Returns anOptionalcontaining the object that has bean created by the parsing if this parsing was possible.
-
-
-
Method Detail
-
parse
@Nullable T parse(@Nullable CharSequence input)
Parse a CharSequence to create an object.- Parameters:
input- the input used to create the object- Returns:
- a new object if possible,
nullotherwise
-
parseValue
default @NonNull Optional<T> parseValue(@Nullable CharSequence input)
Returns anOptionalcontaining the object that has bean 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
-
onDateTimeFormatter
static <T> @NonNull Parser<T> onDateTimeFormatter(@NonNull DateTimeFormatter formatter, TemporalQuery<T>... queries)
-
onDateFormat
static @NonNull Parser<Date> onDateFormat(@NonNull DateFormat dateFormat)
-
onNumberFormat
static @NonNull Parser<Number> onNumberFormat(@NonNull NumberFormat numberFormat)
-
onConstant
static <T> @NonNull Parser<T> onConstant(@Nullable T instance)
-
onNull
static <T> @NonNull Parser<T> onNull()
-
onInteger
static @NonNull Parser<Integer> onInteger()
Create aParserthat delegates its parsing toInteger.valueOf(java.lang.String).- Returns:
- a non-null parser
-
onDouble
static @NonNull Parser<Double> onDouble()
Create aParserthat delegates its parsing toDouble.valueOf(java.lang.String).- Returns:
- a non-null parser
-
onEnum
static <T extends Enum<T>> @NonNull Parser<T> onEnum(Class<T> type, ToIntFunction<T> function)
-
onDoubleArray
static @NonNull Parser<double[]> onDoubleArray()
-
onStringList
static @NonNull Parser<List<String>> onStringList(@NonNull Function<CharSequence,Stream<String>> splitter)
-
of
static <T> @NonNull Parser<T> of(@NonNull Function<? super CharSequence,? extends T> parser, @NonNull Consumer<? super Throwable> onError)
-
of
static <T> @NonNull Parser<T> of(@NonNull Function<? super CharSequence,? extends T> parser)
-
-