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 Details

  • Method Details

    • parse

      public <P> P parse​(String name, 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 file
      parser - 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 with name doesn't exist.
    • parseOrDefault

      public <P> P parseOrDefault​(String name, Function<String,​P> parser, P defVal)
      Parses a value using the given function and returns it. It is similar to parse(String, Function), except that it returns a default value instead of throwing an IllegalArgumentException
      Type Parameters:
      P - the type of value the configuration entry is supposed to match
      Parameters:
      name - the entry name in the properties file
      parser - the function that parses the String value into the desired type
      defVal - 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 with name 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 file
      defVal - 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 with name 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 file
      defVal - 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 with name 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 file
      defVal - 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 file
      separator - 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 the String.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 as parseAsList(String, String, Function) but in case of error returns an empty list instead of throwing an IllegalArgumentException.
      Type Parameters:
      E - the type of the elements of the list
      Parameters:
      name - the name of the configuration entry in the properties file
      separator - 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 the String.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)