public final class PropertiesUtil extends Object
Modifier and Type | Field and Description |
---|---|
static String |
UTF8_ENCODING
When we encode strings, we always specify UTF8 encoding
|
Modifier and Type | Method and Description |
---|---|
static Properties |
filterProperties(Properties properties,
String prefix)
Filters and translates Properties with a prefix.
|
static Properties |
loadProperties(String propertiesFile)
Loads a properties file.
|
static boolean |
propertyAsBool(Properties p,
String key,
boolean defaultValue)
Reads and casts the named property as a boolean.
|
static Double |
propertyAsDouble(Properties p,
String key,
boolean exceptionForNull,
Double defaultValue)
Reads and casts the named property as an Double.
|
static Float |
propertyAsFloat(Properties p,
String key,
boolean exceptionForNull,
Float defaultValue)
Reads and casts the named property as an Float.
|
static Integer |
propertyAsInt(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 Properties |
readFromFile(String filepath)
Reads a property file from an absolute filepath.
|
static Properties |
removeProperties(Properties original,
String prefix)
Remove properties from the original object and return the removed element(s) as new Properties object.
|
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.
|
public static final String UTF8_ENCODING
public static Properties loadProperties(String propertiesFile) throws IOException, IllegalArgumentException
ClassLoader
is used to load the file.IOException
- Should there be an issue in loading the fileIllegalArgumentException
- If the file does not existpublic static Properties readFromFile(String filepath) throws IOException, IllegalArgumentException
IOException
IllegalArgumentException
public static Double propertyAsDouble(Properties p, String key, boolean exceptionForNull, Double defaultValue) throws IllegalArgumentException
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.IllegalArgumentException
- if the property is invalid (can't be cast to a double) or not found and we are
instructed to throw it.public static Float propertyAsFloat(Properties p, String key, boolean exceptionForNull, Float defaultValue) throws IllegalArgumentException
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.IllegalArgumentException
- if the property is invalid (can't be cast to a float) or not found and we are
instructed to throw it.public static Integer propertyAsInt(Properties p, String key, boolean exceptionForNull, Integer defaultValue) throws IllegalArgumentException
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.IllegalArgumentException
- if the property is invalid (can't be cast to an int) or not found and we are
instructed to throw it.public static boolean propertyAsBool(Properties p, String key, boolean defaultValue)
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.public static byte[] propertyAsUTF8Bytes(Properties p, String key, boolean exceptionForNull, byte[] defaultValue) throws IllegalArgumentException
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.IllegalArgumentException
- if the property is not found and we are instructed to throw it.public static Properties filterProperties(Properties properties, String prefix)
properties
- to filter and translateprefix
- prefix used to filter the properties. (e.g. "myprefix.")public static Properties subsetProperties(Properties original, String prefix)
original
- prefix
- public static Properties removeProperties(Properties original, String prefix)
original
- original object in which the element will be removed if key starts with provided prefix.prefix
- Copyright © 2024 Global Biodiversity Information Facility (GBIF). All rights reserved.