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(Properties props) -
Method Summary
Modifier and Type Method Description <P> Pparse(String name, Function<String,P> parser)Parses a value using the given function and returns itintparseAsInt(String name)Convenient method that is similar as doing:intparseAsIntOrDefault(String name, int defVal)Convenient method that is similar as doing:<E> List<E>parseAsList(String name, String separator, 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, Function<String,E> singleElementParser)Same asparseAsList(String, String, Function)but in case of error returns an empty list instead of throwing anIllegalArgumentException.longparseAsLong(String name)Convenient method that is similar as doing:longparseAsLongOrDefault(String name, long defVal)Convenient method that is similar as doing:StringparseAsString(String name)Convenient method that is similar as doing:StringparseAsStringOrDefault(String name, String defVal)Convenient method that is similar as doing:<P> PparseOrDefault(String name, Function<String,P> parser, P defVal)Parses a value using the given function and returns it.
-
Constructor Details
-
Method Details
-
parse
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 withnamedoesn't exist.
-
parseOrDefault
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
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 withnamedoesn't exist.- See Also:
parse(String, Function)
-
parseAsStringOrDefault
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
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 withnamedoesn't exist.- See Also:
parse(String, Function)
-
parseAsIntOrDefault
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
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 withnamedoesn't exist.- See Also:
parse(String, Function)
-
parseAsLongOrDefault
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, 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, 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)
-