Package org.gbif.utils.file.properties
Class PropertiesUtil
java.lang.Object
org.gbif.utils.file.properties.PropertiesUtil
Utility class for handling properties files.
TODO this class should probably be in a "properties" package at the same level as "file"
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringWhen we encode strings, we always specify UTF8 encoding -
Method Summary
Modifier and TypeMethodDescriptionstatic PropertiesfilterProperties(Properties properties, String prefix) Filters and translates Properties with a prefix.static PropertiesloadProperties(String propertiesFile) Loads a properties file.static booleanpropertyAsBool(Properties p, String key, boolean defaultValue) Reads and casts the named property as a boolean.static DoublepropertyAsDouble(Properties p, String key, boolean exceptionForNull, Double defaultValue) Reads and casts the named property as an Double.static FloatpropertyAsFloat(Properties p, String key, boolean exceptionForNull, Float defaultValue) Reads and casts the named property as an Float.static IntegerpropertyAsInt(Properties p, String key, boolean exceptionForNull, Integer defaultValue) Reads and casts the named property as an Integer.static byte[]propertyAsUTF8Bytes(Properties p, String key, boolean exceptionForNull, byte[] defaultValue) Reads and converts the named property as UTF8 bytes.static PropertiesreadFromFile(String filepath) Reads a property file from an absolute filepath.static PropertiesremoveProperties(Properties original, String prefix) Remove properties from the original object and return the removed element(s) as new Properties object.static PropertiessubsetProperties(Properties original, String prefix) Returns a new Properties object that contains only the elements where the key starts by the provided prefix.
-
Field Details
-
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 defaultClassLoaderis used to load the file.- Throws:
IOException- Should there be an issue in loading the fileIllegalArgumentException- If the file does not exist
-
readFromFile
Reads a property file from an absolute filepath.- Throws:
IOExceptionIllegalArgumentException
-
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 returneddefaultValue- 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 returneddefaultValue- 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 returneddefaultValue- 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
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 returneddefaultValue- 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
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 translateprefix- prefix used to filter the properties. (e.g. "myprefix.")- Returns:
- new Properties object with filtered and translated properties. Never null.
-
subsetProperties
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
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:
-