Package net.morimekta.console.util
Interface Parser<T>
-
- All Known Implementing Classes:
Parser.DirParser
,Parser.DoubleParser
,Parser.EnumParser
,Parser.FileParser
,Parser.IntegerParser
,Parser.LongParser
,Parser.OutputDirParser
,Parser.OutputFileParser
,Parser.PathParser
- 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>
Value parser interface. It converts from a string value (usually the CLI argument value) and converts it into a specific value type. Essentially this interface is meant to bridge twoConsumer
s, the target consumer (usually the value setter or adder), and the string consumer that theOption
orArgument
needs.E.g., the following code will create an option '--timestamp' that parses the argument value as a long (i64) and calls
bean.setTimestamp(long)
with that value:new Option("--timestamp", null, "The timestamp", i64(bean::setTimestamp));
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
Parser.DirParser
A converter to file instances, with validator & error message.static class
Parser.DoubleParser
A converter to double values.static class
Parser.EnumParser<E extends java.lang.Enum<E>>
A converter to enum constant values.static class
Parser.FileParser
A converter to file instances, with validator & error message.static class
Parser.IntegerParser
A converter to integer values.static class
Parser.LongParser
A converter to long values.static class
Parser.OutputDirParser
A converter to file instances, with validator & error message.static class
Parser.OutputFileParser
A converter to file instances, with validator & error message.static class
Parser.PathParser
A converter to path values.static interface
Parser.TypedPutter<T>
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default java.util.function.Consumer<java.lang.String>
andApply(java.util.function.Consumer<T> consumer)
Make a string consumer to typed value consumer out of the converter.default Property.Putter
andPut(Parser.TypedPutter<T> putter)
Make a property putter that calls a typed putter with the parsed value.default java.util.function.Consumer<java.lang.String>
andPutAs(Parser.TypedPutter<T> putter, java.lang.String key)
Make a consumer that puts a specific value with the typed putter.static Parser<java.lang.Double>
dbl()
Make a double parser.static java.util.function.Consumer<java.lang.String>
dbl(java.util.function.Consumer<java.lang.Double> target)
Make a 64-bit integer parsing consumer.static Parser<java.io.File>
dir()
Make a file parser that refers to an existing directory.static java.util.function.Consumer<java.lang.String>
dir(java.util.function.Consumer<java.io.File> target)
Make a file parsing consumer that refers to an existing directory.static Parser<java.io.File>
file()
Make a file parser that refers to an existing file.static java.util.function.Consumer<java.lang.String>
file(java.util.function.Consumer<java.io.File> target)
Make a file parsing consumer that refers to an existing file.static Parser<java.lang.Integer>
i32()
Make a 32-bit integer parser.static java.util.function.Consumer<java.lang.String>
i32(java.util.function.Consumer<java.lang.Integer> target)
Make a 32-bit integer parsing consumer.static Parser<java.lang.Long>
i64()
Make a 64-bit integer parser.static java.util.function.Consumer<java.lang.String>
i64(java.util.function.Consumer<java.lang.Long> target)
Make a 64-bit integer parsing consumer.static <E extends java.lang.Enum<E>>
Parser<E>oneOf(java.lang.Class<E> klass)
Make an enum value parsing consumer.static <E extends java.lang.Enum<E>>
java.util.function.Consumer<java.lang.String>oneOf(java.lang.Class<E> klass, java.util.function.Consumer<E> target)
Make a file parsing consumer that refers to an existing file.static Parser<java.io.File>
outputDir()
Make a parser that refers either to a non-existing entry or an existing directory, but not a file or special device.static java.util.function.Consumer<java.lang.String>
outputDir(java.util.function.Consumer<java.io.File> target)
Make a parsing consumer that refers either to a non-existing entry or an existing directory, but not a file or special device.static Parser<java.io.File>
outputFile()
Make a file parser that refers either to a non-existing entry or an existing file, but not a directory or special device.static java.util.function.Consumer<java.lang.String>
outputFile(java.util.function.Consumer<java.io.File> target)
Make a file parsing consumer that refers either to a non-existing entry or an existing file, but not a directory or special device.T
parse(java.lang.String value)
Parse the value into a typed instance.static Parser<java.nio.file.Path>
path()
Make a parser that parses a path.static java.util.function.Consumer<java.lang.String>
path(java.util.function.Consumer<java.nio.file.Path> target)
Make a parsing consumer that parses a path.static java.util.function.Consumer<java.lang.String>
putAs(Property.Putter putter, java.lang.String key)
Convenience method to put a specific value into a putter.
-
-
-
Method Detail
-
parse
T parse(java.lang.String value)
Parse the value into a typed instance.- Parameters:
value
- The string value.- Returns:
- The typed instance.
- Throws:
ArgumentException
- If the parsing failed.
-
andApply
default java.util.function.Consumer<java.lang.String> andApply(java.util.function.Consumer<T> consumer)
Make a string consumer to typed value consumer out of the converter.- Parameters:
consumer
- The consumer to wrap.- Returns:
- The string consumer.
-
andPut
default Property.Putter andPut(Parser.TypedPutter<T> putter)
Make a property putter that calls a typed putter with the parsed value.- Parameters:
putter
- The typed putter.- Returns:
- The property putter.
-
andPutAs
default java.util.function.Consumer<java.lang.String> andPutAs(Parser.TypedPutter<T> putter, java.lang.String key)
Make a consumer that puts a specific value with the typed putter.- Parameters:
putter
- the typed putter.key
- The property key.- Returns:
- The string consumer.
-
putAs
static java.util.function.Consumer<java.lang.String> putAs(Property.Putter putter, java.lang.String key)
Convenience method to put a specific value into a putter.- Parameters:
putter
- The putter.key
- The key to put.- Returns:
- The string consumer.
-
i32
static Parser<java.lang.Integer> i32()
Make a 32-bit integer parser.- Returns:
- The parser.
-
i32
static java.util.function.Consumer<java.lang.String> i32(java.util.function.Consumer<java.lang.Integer> target)
Make a 32-bit integer parsing consumer.- Parameters:
target
- The target consumer.- Returns:
- The consumer wrapper.
-
i64
static Parser<java.lang.Long> i64()
Make a 64-bit integer parser.- Returns:
- The parser.
-
i64
static java.util.function.Consumer<java.lang.String> i64(java.util.function.Consumer<java.lang.Long> target)
Make a 64-bit integer parsing consumer.- Parameters:
target
- The target consumer.- Returns:
- The consumer wrapper.
-
dbl
static Parser<java.lang.Double> dbl()
Make a double parser.- Returns:
- The parser.
-
dbl
static java.util.function.Consumer<java.lang.String> dbl(java.util.function.Consumer<java.lang.Double> target)
Make a 64-bit integer parsing consumer.- Parameters:
target
- The target consumer.- Returns:
- The consumer wrapper.
-
oneOf
static <E extends java.lang.Enum<E>> Parser<E> oneOf(java.lang.Class<E> klass)
Make an enum value parsing consumer.- Type Parameters:
E
- The enum type.- Parameters:
klass
- The enum class.- Returns:
- The parser.
-
oneOf
static <E extends java.lang.Enum<E>> java.util.function.Consumer<java.lang.String> oneOf(java.lang.Class<E> klass, java.util.function.Consumer<E> target)
Make a file parsing consumer that refers to an existing file.- Type Parameters:
E
- The enum type.- Parameters:
klass
- The enum class.target
- The target consumer.- Returns:
- The consumer wrapper.
-
file
static Parser<java.io.File> file()
Make a file parser that refers to an existing file.- Returns:
- The parser.
-
file
static java.util.function.Consumer<java.lang.String> file(java.util.function.Consumer<java.io.File> target)
Make a file parsing consumer that refers to an existing file.- Parameters:
target
- The target consumer.- Returns:
- The consumer wrapper.
-
dir
static Parser<java.io.File> dir()
Make a file parser that refers to an existing directory.- Returns:
- The consumer wrapper.
-
dir
static java.util.function.Consumer<java.lang.String> dir(java.util.function.Consumer<java.io.File> target)
Make a file parsing consumer that refers to an existing directory.- Parameters:
target
- The target consumer.- Returns:
- The parser.
-
outputFile
static Parser<java.io.File> outputFile()
Make a file parser that refers either to a non-existing entry or an existing file, but not a directory or special device.- Returns:
- The parser.
-
outputFile
static java.util.function.Consumer<java.lang.String> outputFile(java.util.function.Consumer<java.io.File> target)
Make a file parsing consumer that refers either to a non-existing entry or an existing file, but not a directory or special device.- Parameters:
target
- The target consumer.- Returns:
- The consumer wrapper.
-
outputDir
static Parser<java.io.File> outputDir()
Make a parser that refers either to a non-existing entry or an existing directory, but not a file or special device.- Returns:
- The parser.
-
outputDir
static java.util.function.Consumer<java.lang.String> outputDir(java.util.function.Consumer<java.io.File> target)
Make a parsing consumer that refers either to a non-existing entry or an existing directory, but not a file or special device.- Parameters:
target
- The target consumer.- Returns:
- The consumer wrapper.
-
path
static Parser<java.nio.file.Path> path()
Make a parser that parses a path.- Returns:
- The parser.
-
path
static java.util.function.Consumer<java.lang.String> path(java.util.function.Consumer<java.nio.file.Path> target)
Make a parsing consumer that parses a path.- Parameters:
target
- The target consumer.- Returns:
- The consumer wrapper.
-
-