Class PropertyParser
- java.lang.Object
-
- com.github.alex1304.ultimategdbot.api.utils.PropertyParser
-
public class PropertyParser extends Object
Allows to parse values from properties files.
-
-
Constructor Summary
Constructors Constructor Description PropertyParser(String filename, Properties props)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <P> P
parse(String name, java.util.function.Function<String,P> parser)
Parses a value using the given function and returns itint
parseAsInt(String name)
Convenient method that is similar as doing:int
parseAsIntOrDefault(String name, int defVal)
Convenient method that is similar as doing:<E> List<E>
parseAsList(String name, String separator, java.util.function.Function<String,E> singleElementParser)
Attempts to parse the vale as a list of values, each element being separated with a separator character.<E> List<E>
parseAsListOrEmpty(String name, String separator, java.util.function.Function<String,E> singleElementParser)
Same asparseAsList(String, String, Function)
but in case of error returns an empty list instead of throwing anIllegalArgumentException
.long
parseAsLong(String name)
Convenient method that is similar as doing:long
parseAsLongOrDefault(String name, long defVal)
Convenient method that is similar as doing:String
parseAsString(String name)
Convenient method that is similar as doing:String
parseAsStringOrDefault(String name, String defVal)
Convenient method that is similar as doing:<P> P
parseOrDefault(String name, java.util.function.Function<String,P> parser, P defVal)
Parses a value using the given function and returns it.
-
-
-
Constructor Detail
-
PropertyParser
public PropertyParser(String filename, Properties props)
-
-
Method Detail
-
parse
public <P> P parse(String name, java.util.function.Function<String,P> parser)
Parses a value using the given function and returns it- Type Parameters:
P
- the type of value the configuration entry is supposed to match- Parameters:
name
- the entry name in the properties fileparser
- the function that parses the String value into the desired type- Returns:
- the parsed value
- Throws:
IllegalArgumentException
- if the value could not be parsed or if the configuration entry associated withname
doesn't exist.
-
parseOrDefault
public <P> P parseOrDefault(String name, java.util.function.Function<String,P> parser, P defVal)
Parses a value using the given function and returns it. It is similar toparse(String, Function)
, except that it returns a default value instead of throwing anIllegalArgumentException
- Type Parameters:
P
- the type of value the configuration entry is supposed to match- Parameters:
name
- the entry name in the properties fileparser
- the function that parses the String value into the desired typedefVal
- the default value to return if not found or couldn't be parsed- Returns:
- the parsed value
-
parseAsString
public String parseAsString(String name)
Convenient method that is similar as doing:parse(name, String::toString)
- Parameters:
name
- the entry name in the properties file- Returns:
- the parsed value
- Throws:
IllegalArgumentException
- if the value could not be parsed or if the configuration entry associated withname
doesn't exist.- See Also:
parse(String, Function)
-
parseAsStringOrDefault
public String parseAsStringOrDefault(String name, String defVal)
Convenient method that is similar as doing:parseOrDefault(name, String::toString, defVal)
- Parameters:
name
- the entry name in the properties filedefVal
- the default value to return if not found or couldn't be parsed- Returns:
- the parsed value
- See Also:
parseOrDefault(String, Function, Object)
-
parseAsInt
public int parseAsInt(String name)
Convenient method that is similar as doing:parse(name, Integer::parseInt)
- Parameters:
name
- the entry name in the properties file- Returns:
- the parsed value
- Throws:
IllegalArgumentException
- if the value could not be parsed or if the configuration entry associated withname
doesn't exist.- See Also:
parse(String, Function)
-
parseAsIntOrDefault
public int parseAsIntOrDefault(String name, int defVal)
Convenient method that is similar as doing:parseOrDefault(name, Integer::parseInt, defVal)
- Parameters:
name
- the entry name in the properties filedefVal
- the default value to return if not found or couldn't be parsed- Returns:
- the parsed value
- See Also:
parseOrDefault(String, Function, Object)
-
parseAsLong
public long parseAsLong(String name)
Convenient method that is similar as doing:parse(name, Long::parseLong)
- Parameters:
name
- the entry name in the properties file- Returns:
- the parsed value
- Throws:
IllegalArgumentException
- if the value could not be parsed or if the configuration entry associated withname
doesn't exist.- See Also:
parse(String, Function)
-
parseAsLongOrDefault
public long parseAsLongOrDefault(String name, long defVal)
Convenient method that is similar as doing:parseOrDefault(name, Long::parseLong, defVal)
- Parameters:
name
- the entry name in the properties filedefVal
- the default value to return if not found or couldn't be parsed- Returns:
- the parsed value
- See Also:
parseOrDefault(String, Function, Object)
-
parseAsList
public <E> List<E> parseAsList(String name, String separator, java.util.function.Function<String,E> singleElementParser)
Attempts to parse the vale as a list of values, each element being separated with a separator character. Let's say the value of the configuration entry "foo" is formatted as:bar:test:demon:hamburger
Calling this method this way:List<String> l = parseAsList("foo", ":", String::toString);
would output a List with the following contents:["bar", "test", "demon", "hamburger"]
- Type Parameters:
E
- the type of the elements of the list- Parameters:
name
- the name of the configuration entry in the properties fileseparator
- the character (or sequence of characters) that separates the elements in the raw string. Note that this is actually a regex as this parameter is directly passed to theString.split(String)
method internally. So characters like $ or | should be properly escaped.singleElementParser
- the parser function to apply on each element of the list- Returns:
- the List containing all elements
- Throws:
IllegalArgumentException
- if at least one element failed to parse, or if the entry for the given name doesn't exist.
-
parseAsListOrEmpty
public <E> List<E> parseAsListOrEmpty(String name, String separator, java.util.function.Function<String,E> singleElementParser)
Same asparseAsList(String, String, Function)
but in case of error returns an empty list instead of throwing anIllegalArgumentException
.- Type Parameters:
E
- the type of the elements of the list- Parameters:
name
- the name of the configuration entry in the properties fileseparator
- the character (or sequence of characters) that separates the elements in the raw string. Note that this is actually a regex as this parameter is directly passed to theString.split(String)
method internally. So characters like $ or | should be properly escaped.singleElementParser
- the parser function to apply on each element of the list- Returns:
- the List containing all elements, or empty if at least one element failed to parse, or if the entry for the given name doesn't exist.
- See Also:
parseAsList(String, String, Function)
-
-