Class PropertyParser
java.lang.Object
com.github.alex1304.ultimategdbot.api.utils.PropertyParser
public class PropertyParser
extends java.lang.Object
Allows to parse values from properties files.
-
Constructor Summary
Constructors Constructor Description PropertyParser(java.util.Properties props)
-
Method Summary
Modifier and Type Method Description <P> P
parse(java.lang.String name, java.util.function.Function<java.lang.String,P> parser)
Parses a value using the given function and returns itint
parseAsInt(java.lang.String name)
Convenient method that is similar as doing:int
parseAsIntOrDefault(java.lang.String name, int defVal)
Convenient method that is similar as doing:<E> java.util.List<E>
parseAsList(java.lang.String name, java.lang.String separator, java.util.function.Function<java.lang.String,E> singleElementParser)
Attempts to parse the vale as a list of values, each element being separated with a separator character.<E> java.util.List<E>
parseAsListOrEmpty(java.lang.String name, java.lang.String separator, java.util.function.Function<java.lang.String,E> singleElementParser)
Same asparseAsList(String, String, Function)
but in case of error returns an empty list instead of throwing anIllegalArgumentException
.long
parseAsLong(java.lang.String name)
Convenient method that is similar as doing:long
parseAsLongOrDefault(java.lang.String name, long defVal)
Convenient method that is similar as doing:java.lang.String
parseAsString(java.lang.String name)
Convenient method that is similar as doing:java.lang.String
parseAsStringOrDefault(java.lang.String name, java.lang.String defVal)
Convenient method that is similar as doing:<P> P
parseOrDefault(java.lang.String name, java.util.function.Function<java.lang.String,P> parser, P defVal)
Parses a value using the given function and returns it.
-
Constructor Details
-
PropertyParser
public PropertyParser(java.util.Properties props)
-
-
Method Details
-
parse
public <P> P parse(java.lang.String name, java.util.function.Function<java.lang.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:
java.lang.IllegalArgumentException
- if the value could not be parsed or if the configuration entry associated withname
doesn't exist.
-
parseOrDefault
public <P> P parseOrDefault(java.lang.String name, java.util.function.Function<java.lang.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 java.lang.String parseAsString(java.lang.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:
java.lang.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 java.lang.String parseAsStringOrDefault(java.lang.String name, java.lang.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(java.lang.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:
java.lang.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(java.lang.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(java.lang.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:
java.lang.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(java.lang.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> java.util.List<E> parseAsList(java.lang.String name, java.lang.String separator, java.util.function.Function<java.lang.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:
java.lang.IllegalArgumentException
- if at least one element failed to parse, or if the entry for the given name doesn't exist.
-
parseAsListOrEmpty
public <E> java.util.List<E> parseAsListOrEmpty(java.lang.String name, java.lang.String separator, java.util.function.Function<java.lang.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)
-