Class PropertiesUtil

java.lang.Object
org.gbif.utils.file.properties.PropertiesUtil

public final class PropertiesUtil extends Object
Utility class for handling properties files. TODO this class should probably be in a "properties" package at the same level as "file"
  • Field Details

    • UTF8_ENCODING

      public static final String UTF8_ENCODING
      When we encode strings, we always specify UTF8 encoding
  • Method Details

    • loadProperties

      public static Properties loadProperties(String propertiesFile) throws IOException, IllegalArgumentException
      Loads a properties file. The file should be available in the classpath, the default ClassLoader is used to load the file.
      Throws:
      IOException - Should there be an issue in loading the file
      IllegalArgumentException - If the file does not exist
    • readFromFile

      Reads a property file from an absolute filepath.
      Throws:
      IOException
      IllegalArgumentException
    • propertyAsDouble

      public static Double propertyAsDouble(Properties p, String key, boolean exceptionForNull, Double defaultValue) throws IllegalArgumentException
      Reads and casts the named property as an Double.
      Parameters:
      p - The properties file to read from.
      key - To read the value of.
      exceptionForNull - If true, and the property is not found an IAE is thrown, otherwise defaultValue is returned
      defaultValue - If the property is not found, and exceptionForNull is false, this is returned for missing properties.
      Returns:
      The property at the key as an Double
      Throws:
      IllegalArgumentException - if the property is invalid (can't be cast to a double) or not found and we are instructed to throw it.
    • propertyAsFloat

      public static Float propertyAsFloat(Properties p, String key, boolean exceptionForNull, Float defaultValue) throws IllegalArgumentException
      Reads and casts the named property as an Float.
      Parameters:
      p - The properties file to read from.
      key - To read the value of.
      exceptionForNull - If true, and the property is not found an IAE is thrown, otherwise defaultValue is returned
      defaultValue - If the property is not found, and exceptionForNull is false, this is returned for missing properties.
      Returns:
      The property at the key as an Float
      Throws:
      IllegalArgumentException - if the property is invalid (can't be cast to a float) or not found and we are instructed to throw it.
    • propertyAsInt

      public static Integer propertyAsInt(Properties p, String key, boolean exceptionForNull, Integer defaultValue) throws IllegalArgumentException
      Reads and casts the named property as an Integer.
      Parameters:
      p - The properties file to read from.
      key - To read the value of.
      exceptionForNull - If true, and the property is not found an IAE is thrown, otherwise defaultValue is returned
      defaultValue - If the property is not found, and exceptionForNull is false, this is returned for missing properties.
      Returns:
      The property at the key as an int
      Throws:
      IllegalArgumentException - if the property is invalid (can't be cast to an int) or not found and we are instructed to throw it.
    • propertyAsBool

      public static boolean propertyAsBool(Properties p, String key, boolean defaultValue)
      Reads and casts the named property as a boolean. Case insensitive values for 'true', 'on', 'yes', 't' and 'y' return true values, 'false', 'off', 'no', 'f' and 'n' return false. Otherwise or in case of a missing property the default will be used.
      Parameters:
      p - The properties file to read from.
      key - To read the value of.
      defaultValue - If the property is not found this is returned for missing properties.
      Returns:
      The property at the key as a boolean
    • propertyAsUTF8Bytes

      public static byte[] propertyAsUTF8Bytes(Properties p, String key, boolean exceptionForNull, byte[] defaultValue) throws IllegalArgumentException
      Reads and converts the named property as UTF8 bytes.
      Parameters:
      p - The properties file to read from.
      key - To read the value of.
      exceptionForNull - If true, and the property is not found an IAE is thrown, otherwise defaultValue is returned
      defaultValue - If the property is not found, and exceptionForNull is false, this is returned for missing properties.
      Returns:
      The property at the key as byte[]t
      Throws:
      IllegalArgumentException - if the property is not found and we are instructed to throw it.
    • filterProperties

      public static Properties filterProperties(Properties properties, String prefix)
      Filters and translates Properties with a prefix. The resulting Properties will only include the properties that start with the provided prefix with that prefix removed (e.g. myprefix.key1 will be returned as key1 if prefix = "myprefix.")
      Parameters:
      properties - to filter and translate
      prefix - prefix used to filter the properties. (e.g. "myprefix.")
      Returns:
      new Properties object with filtered and translated properties. Never null.
    • subsetProperties

      public static Properties subsetProperties(Properties original, String prefix)
      Returns a new Properties object that contains only the elements where the key starts by the provided prefix. The same keys will be used in the returned Properties.
      Parameters:
      original -
      prefix -
      Returns:
    • removeProperties

      public static Properties removeProperties(Properties original, String prefix)
      Remove properties from the original object and return the removed element(s) as new Properties object. The same keys will be used in the returned Properties.
      Parameters:
      original - original object in which the element will be removed if key starts with provided prefix.
      prefix -
      Returns: