Package com.complexible.common.util
Class EnhancedProperties
- java.lang.Object
-
- java.util.Dictionary<K,V>
-
- java.util.Hashtable<java.lang.Object,java.lang.Object>
-
- java.util.Properties
-
- com.complexible.common.util.EnhancedProperties
-
- All Implemented Interfaces:
Copyable<EnhancedProperties>
,java.io.Serializable
,java.lang.Cloneable
,java.util.Map<java.lang.Object,java.lang.Object>
public class EnhancedProperties extends java.util.Properties implements Copyable<EnhancedProperties>
Extends the java.util.Properties stuff to provide typed accessors to get property values as boolean, int, etc. Also provides a way to get a property value as a list, or as a map of values. And it does variable substitution on property values.
Given the following property file:
{ some_boolean_property = true some_integer_property = 42 some_property = some_value some_list = one, two, three, four some_other_property = ${some_property}/foo some_map = key_a, key_b, key_c key_a = a key_b = b key_c = c }
getPropertyAsBoolean("some_boolean_property")
yields the boolean valuetrue
.
getPropertyAsInt("some_integer_property")
yields the integer42
.
getPropertyAsList("some_list")
yields aList<String>
with the values"one", "two", "three", "four"
.
getPropertyAsMap("some_map")
yields aMap<String, String>
with the key value pairs:key_a => a, key_b => b, key_c => c
.
Lastly, getting the property "some_other_property" yields the value "some_value/foo" via variable substitution.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description EnhancedProperties()
Create a new EnhancedPropertiesEnhancedProperties(java.io.InputStream theInput)
Create a new EnhancedPropertiesEnhancedProperties(java.util.Properties theProps)
Copy constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static boolean
containsKeyIgnoreCase(java.util.Properties properties, java.lang.String key)
Returnstrue
if the properties contains a key that matches the supplied key, ignoring case.EnhancedProperties
copy()
Create a deep copy of the object which does not share any references with the original.java.lang.String
getProperty(java.lang.String theProp)
Returns the value of the given propertyboolean
getPropertyAsBoolean(java.lang.String theProp)
Return the value of the property as a booleanboolean
getPropertyAsBoolean(java.lang.String theProperty, boolean theDefault)
Return the value of the property as a boolean.double
getPropertyAsDouble(java.lang.String theProperty)
Return the value of the property as a doubledouble
getPropertyAsDouble(java.lang.String theProperty, double theDefault)
Return the value of the property as a doublefloat
getPropertyAsFloat(java.lang.String theProperty)
Return the value of the property as a floatfloat
getPropertyAsFloat(java.lang.String theProperty, float theDefault)
Return the value of the property as a floatint
getPropertyAsInt(java.lang.String theProp)
Return the value of the property as an intint
getPropertyAsInt(java.lang.String theProp, int theDefault)
Return the value of the property as an intjava.util.List<java.lang.String>
getPropertyAsList(java.lang.String theProp)
Returns the value of a property as a list.long
getPropertyAsLong(java.lang.String theProperty)
Return the value of the property as a longlong
getPropertyAsLong(java.lang.String theProperty, long theDefault)
Return the value of the property as a longjava.util.Map<java.lang.String,java.lang.String>
getPropertyAsMap(java.lang.String theProp)
Returns the value of the property as a map.java.lang.Iterable<EnhancedProperties>
partitionByKeys(java.util.function.Function<java.lang.String,java.lang.String> thePartitionFunction)
java.lang.Object
put(java.lang.Object theKey, java.lang.Object theValue)
EnhancedProperties
select(java.util.function.Predicate<java.lang.String> theKeySelector)
Return a subset of this Properties object by selecting all the keys which pass the predicate filter and inserting those key-value objects into the returned value.EnhancedProperties
transformKeys(java.util.function.Function<java.lang.String,java.lang.String> theKeyFunction)
EnhancedProperties
transformValues(java.util.function.Function<java.lang.String,java.lang.String> theValueFunction)
-
Methods inherited from class java.util.Properties
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, getProperty, hashCode, isEmpty, keys, keySet, list, list, load, load, loadFromXML, merge, propertyNames, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, save, setProperty, size, store, store, storeToXML, storeToXML, storeToXML, stringPropertyNames, toString, values
-
-
-
-
Constructor Detail
-
EnhancedProperties
public EnhancedProperties()
Create a new EnhancedProperties
-
EnhancedProperties
public EnhancedProperties(java.io.InputStream theInput) throws java.io.IOException
Create a new EnhancedProperties- Parameters:
theInput
- the inputstream to load property data from- Throws:
java.io.IOException
- thrown if there is an error reading properties data
-
EnhancedProperties
public EnhancedProperties(java.util.Properties theProps)
Copy constructor- Parameters:
theProps
- the properties to copy from
-
-
Method Detail
-
getPropertyAsBoolean
public boolean getPropertyAsBoolean(java.lang.String theProp)
Return the value of the property as a boolean- Parameters:
theProp
- the property to retrieve- Returns:
- the value of the property as a boolean, or false if the property does not exist
-
getPropertyAsBoolean
public boolean getPropertyAsBoolean(java.lang.String theProperty, boolean theDefault)
Return the value of the property as a boolean.- Parameters:
theProperty
- the property to retrievetheDefault
- the default value if the property does not exist- Returns:
- the value of the property if it exists, otherwise the default value
-
getProperty
public java.lang.String getProperty(java.lang.String theProp)
Returns the value of the given property- Overrides:
getProperty
in classjava.util.Properties
- Parameters:
theProp
- the property to retrieve- Returns:
- the value of the property, or null if one is not found
-
put
public java.lang.Object put(java.lang.Object theKey, java.lang.Object theValue)
- Specified by:
put
in interfacejava.util.Map<java.lang.Object,java.lang.Object>
- Overrides:
put
in classjava.util.Properties
-
getPropertyAsInt
public int getPropertyAsInt(java.lang.String theProp, int theDefault) throws java.lang.NumberFormatException
Return the value of the property as an int- Parameters:
theProp
- the property to retrievetheDefault
- the default property value- Returns:
- the value of the property as an int
- Throws:
java.lang.NumberFormatException
- thrown if the value is not a valid integer value
-
getPropertyAsInt
public int getPropertyAsInt(java.lang.String theProp) throws java.lang.NumberFormatException
Return the value of the property as an int- Parameters:
theProp
- the property to retrieve- Returns:
- the value of the property as an int
- Throws:
java.lang.NumberFormatException
- thrown if the value is not a valid integer value
-
getPropertyAsLong
public long getPropertyAsLong(java.lang.String theProperty, long theDefault) throws java.lang.NumberFormatException
Return the value of the property as a long- Parameters:
theProperty
- the property to retrievetheDefault
- the default value if the property does not exist- Returns:
- the value of the property as a long
- Throws:
java.lang.NumberFormatException
- thrown if the value is not a value long value
-
getPropertyAsLong
public long getPropertyAsLong(java.lang.String theProperty) throws java.lang.NumberFormatException
Return the value of the property as a long- Parameters:
theProperty
- the property to retrieve- Returns:
- the value of the property as a long
- Throws:
java.lang.NumberFormatException
- thrown if the value is not a value long value
-
getPropertyAsFloat
public float getPropertyAsFloat(java.lang.String theProperty, float theDefault) throws java.lang.NumberFormatException
Return the value of the property as a float- Parameters:
theProperty
- the property to retrievetheDefault
- the default value to return if the property does not exist- Returns:
- the value of the property as a float, or the default value
- Throws:
java.lang.NumberFormatException
- thrown if the value is not a valid float value
-
getPropertyAsFloat
public float getPropertyAsFloat(java.lang.String theProperty) throws java.lang.NumberFormatException
Return the value of the property as a float- Parameters:
theProperty
- the property to retrieve- Returns:
- the value of the property as a float
- Throws:
java.lang.NumberFormatException
- thrown if the value is not a valid float value
-
getPropertyAsDouble
public double getPropertyAsDouble(java.lang.String theProperty) throws java.lang.NumberFormatException
Return the value of the property as a double- Parameters:
theProperty
- the property to retrieve- Returns:
- the value of the property as a double
- Throws:
java.lang.NumberFormatException
- thrown if the value is not a valid double value
-
getPropertyAsDouble
public double getPropertyAsDouble(java.lang.String theProperty, double theDefault) throws java.lang.NumberFormatException
Return the value of the property as a double- Parameters:
theProperty
- the property to retrievetheDefault
- the default value for the property if it does not exist- Returns:
- the value of the property, as a double, or the default value if the property does not exist
- Throws:
java.lang.NumberFormatException
- thrown if the value is not a valid double value
-
getPropertyAsList
public java.util.List<java.lang.String> getPropertyAsList(java.lang.String theProp)
Returns the value of a property as a list. The value of the property must be comma separated:mylist = one, two, three, four
Would yield a list of four elements, "one", "two", "three" and "four".- Parameters:
theProp
- the property key- Returns:
- the value as a list, or null if the key is not in the properties.
-
getPropertyAsMap
public java.util.Map<java.lang.String,java.lang.String> getPropertyAsMap(java.lang.String theProp)
Returns the value of the property as a map. The way this works is if you have some properties like this:map = foo, baz, boz foo = bar baz = biz boz = buzz
Getting the key "map" as a map will yield a map with three keys "foo", "baz", and "boz" with the values "bar", "biz" and "buzz" respectively. The keys of the map MUST be comma separated. If a key does not have a corresponding value, it is not added to the result map.- Parameters:
theProp
- the property key which has the key values of the map as its value- Returns:
- the property value as a map.
-
copy
public EnhancedProperties copy()
Create a deep copy of the object which does not share any references with the original.- Specified by:
copy
in interfaceCopyable<EnhancedProperties>
- Returns:
- a copy of the object
-
select
public EnhancedProperties select(java.util.function.Predicate<java.lang.String> theKeySelector)
Return a subset of this Properties object by selecting all the keys which pass the predicate filter and inserting those key-value objects into the returned value.- Parameters:
theKeySelector
- the filter for key values- Returns:
- an EnhancedProperties containing all key-value pairs whose keys pass the filter
-
transformKeys
public EnhancedProperties transformKeys(java.util.function.Function<java.lang.String,java.lang.String> theKeyFunction)
-
transformValues
public EnhancedProperties transformValues(java.util.function.Function<java.lang.String,java.lang.String> theValueFunction)
-
partitionByKeys
public java.lang.Iterable<EnhancedProperties> partitionByKeys(java.util.function.Function<java.lang.String,java.lang.String> thePartitionFunction)
-
containsKeyIgnoreCase
public static boolean containsKeyIgnoreCase(java.util.Properties properties, java.lang.String key)
Returnstrue
if the properties contains a key that matches the supplied key, ignoring case.
-
-