Class PropertiesHelper

java.lang.Object
com.trafficparrot.sdk.PropertiesHelper

public class PropertiesHelper extends Object
Reads .properties files and comma-separated property values, for an extension that keeps its own settings in a file.

 Properties settings = PropertiesHelper.readPropertiesFile(Paths.get("my-extension.properties"));
 List<String> hosts = PropertiesHelper.parseList(settings.getProperty("hosts"));
 
  • Constructor Details

    • PropertiesHelper

      public PropertiesHelper()
      Not needed, since every method is static. Declared so that code which creates an instance keeps working.
  • Method Details

    • parseList

      public static List<String> parseList(String propertyValue)
      Splits a comma-separated property value into its items.

      Each item is returned as written, so "a, b" gives "a" and " b": trim the items if the value may carry spaces. An empty item at the start or in the middle is kept (",a,,b" gives four items); empty items at the end are dropped.

      Parameters:
      propertyValue - the value to split; may be null
      Returns:
      the items in order, or an empty list when the value is null, empty or only whitespace
    • readPropertiesFile

      public static Properties readPropertiesFile(String propertiesResource)
      Reads a .properties resource from the classpath, through the class loader that loaded the SDK.
      Parameters:
      propertiesResource - the resource's name on the classpath, such as "my-extension.properties"
      Returns:
      the properties the resource holds
      Throws:
      IllegalArgumentException - if the resource cannot be read
      NullPointerException - if the classpath has no resource with this name
    • readPropertiesFile

      public static Properties readPropertiesFile(Path propertiesFile)
      Reads a .properties file from disk.
      Parameters:
      propertiesFile - the file to read
      Returns:
      the properties the file holds
      Throws:
      IllegalArgumentException - if the file does not exist, is a directory or cannot be read