Package io.pravega.common.util
Class DelimitedStringParser
- java.lang.Object
-
- io.pravega.common.util.DelimitedStringParser
-
public class DelimitedStringParser extends java.lang.ObjectParses Strings as a sequence of key-value pairs and allows extracting values for specific keys.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"
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DelimitedStringParserextractBoolean(java.lang.String key, java.util.function.Consumer<java.lang.Boolean> consumer)Associates the given consumer with the given key.DelimitedStringParserextractInteger(java.lang.String key, java.util.function.Consumer<java.lang.Integer> consumer)Associates the given consumer with the given key.DelimitedStringParserextractLong(java.lang.String key, java.util.function.Consumer<java.lang.Long> consumer)Associates the given consumer with the given key.DelimitedStringParserextractString(java.lang.String key, java.util.function.Consumer<java.lang.String> consumer)Associates the given consumer with the given key.voidparse(java.lang.String s)Parses the given string using the configuration set on this parser.static DelimitedStringParserparser(java.lang.String pairDelimiter, java.lang.String keyValueDelimiter)Creates a new DelimitedStringParser with the given Pair and KeyValue delimiters.
-
-
-
Method Detail
-
parser
public static DelimitedStringParser parser(java.lang.String pairDelimiter, java.lang.String keyValueDelimiter)
Creates a new DelimitedStringParser with the given Pair and KeyValue delimiters.- Parameters:
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).- Returns:
- A new instance of the DelimitedStringParser class.
-
extractInteger
public DelimitedStringParser extractInteger(java.lang.String key, java.util.function.Consumer<java.lang.Integer> consumer)
Associates the given consumer with the given key. This consumer will be invoked every time a Key-Value pair with the given key is encountered (argument is the Value of the pair). Note that this may be invoked multiple times or not at all, based on the given input.- Parameters:
key- The key for which to invoke the consumer.consumer- The consumer to invoke.- Returns:
- This object instance.
-
extractLong
public DelimitedStringParser extractLong(java.lang.String key, java.util.function.Consumer<java.lang.Long> consumer)
Associates the given consumer with the given key. This consumer will be invoked every time a Key-Value pair with the given key is encountered (argument is the Value of the pair). Note that this may be invoked multiple times or not at all, based on the given input.- Parameters:
key- The key for which to invoke the consumer.consumer- The consumer to invoke.- Returns:
- This object instance.
-
extractString
public DelimitedStringParser extractString(java.lang.String key, java.util.function.Consumer<java.lang.String> consumer)
Associates the given consumer with the given key. This consumer will be invoked every time a Key-Value pair with the given key is encountered (argument is the Value of the pair). Note that this may be invoked multiple times or not at all, based on the given input.- Parameters:
key- The key for which to invoke the consumer.consumer- The consumer to invoke.- Returns:
- This object instance.
-
extractBoolean
public DelimitedStringParser extractBoolean(java.lang.String key, java.util.function.Consumer<java.lang.Boolean> consumer)
Associates the given consumer with the given key. This consumer will be invoked every time a Key-Value pair with the given key is encountered (argument is the Value of the pair). Note that this may be invoked multiple times or not at all, based on the given input.- Parameters:
key- The key for which to invoke the consumer.consumer- The consumer to invoke.- Returns:
- This object instance.
-
parse
public void parse(java.lang.String s)
Parses the given string using the configuration set on this parser.- Parameters:
s- The string to parse.
-
-