public class DelimitedStringParser
extends java.lang.Object
The input format is a sequence (delimited by {keyValueDelimiter}) of key-value pairs (separated by {pairDelimiter}): "{key}{pairDelimiter}{value}{keyValueDelimiter}{key}{pairDelimiter}{value}{keyValueDelimiter}..."
The extractors are called on the {value} for the {key} that matches their own key. Call order of extractors is done in the order in which the key-value pairs are parsed out of the string. In case of a malformed string, some extractors may still be invoked until the error is discovered, at which point an exception is raised and no further extractors are invoked.
Example usage:
AtomicInteger a = new AtomicInteger();
AtomicInteger b = new AtomicLong();
AtomicBoolean c = new AtomicBoolean();
AtomicReference<String> d = new AtomicReference<>();
DelimitedStringParser.parse(",", "=")
.extractInteger("a", a::set)
.extractLong("b", b::set)
.extractBoolean("c", c::set)
.extractString("d", d::set)
.parse("a=1,b=2,c=tRuE,d=foo");
System.out.println(String.format("a=%s, b=%s, c=%s, d=%s", a, b, c, d);
// Output: "a=1, b=2, c=true, d=foo"
| Constructor and Description |
|---|
DelimitedStringParser() |
| Modifier and Type | Method and Description |
|---|---|
DelimitedStringParser |
extractBoolean(java.lang.String key,
java.util.function.Consumer<java.lang.Boolean> consumer)
Associates the given consumer with the given key.
|
DelimitedStringParser |
extractInteger(java.lang.String key,
java.util.function.Consumer<java.lang.Integer> consumer)
Associates the given consumer with the given key.
|
DelimitedStringParser |
extractLong(java.lang.String key,
java.util.function.Consumer<java.lang.Long> consumer)
Associates the given consumer with the given key.
|
DelimitedStringParser |
extractString(java.lang.String key,
java.util.function.Consumer<java.lang.String> consumer)
Associates the given consumer with the given key.
|
void |
parse(java.lang.String s)
Parses the given string using the configuration set on this parser.
|
static DelimitedStringParser |
parser(java.lang.String pairDelimiter,
java.lang.String keyValueDelimiter)
Creates a new DelimitedStringParser with the given Pair and KeyValue delimiters.
|
public static DelimitedStringParser parser(java.lang.String pairDelimiter, java.lang.String keyValueDelimiter)
pairDelimiter - A String that will be used to delimit pairs.keyValueDelimiter - A String that will be used to delimit Keys from Values (inside a pair).public DelimitedStringParser extractInteger(java.lang.String key, java.util.function.Consumer<java.lang.Integer> consumer)
key - The key for which to invoke the consumer.consumer - The consumer to invoke.public DelimitedStringParser extractLong(java.lang.String key, java.util.function.Consumer<java.lang.Long> consumer)
key - The key for which to invoke the consumer.consumer - The consumer to invoke.public DelimitedStringParser extractString(java.lang.String key, java.util.function.Consumer<java.lang.String> consumer)
key - The key for which to invoke the consumer.consumer - The consumer to invoke.public DelimitedStringParser extractBoolean(java.lang.String key, java.util.function.Consumer<java.lang.Boolean> consumer)
key - The key for which to invoke the consumer.consumer - The consumer to invoke.public void parse(java.lang.String s)
s - The string to parse.