Package com.complexible.common.util
Class EnhancedProperties
java.lang.Object
java.util.Dictionary<Object,Object>
java.util.Hashtable<Object,Object>
java.util.Properties
com.complexible.common.util.EnhancedProperties
- All Implemented Interfaces:
Copyable<EnhancedProperties>,Serializable,Cloneable,Map<Object,Object>
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 value true.getPropertyAsInt("some_integer_property") yields the integer 42.getPropertyAsList("some_list") yields a List<String> with the values "one", "two", "three", "four".getPropertyAsMap("some_map") yields a Map<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:
-
Field Summary
Fields inherited from class java.util.Properties
defaults -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new EnhancedPropertiesEnhancedProperties(InputStream theInput) Create a new EnhancedPropertiesEnhancedProperties(Properties theProps) Copy constructor -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontainsKeyIgnoreCase(Properties properties, String key) Returnstrueif the properties contains a key that matches the supplied key, ignoring case.copy()Create a deep copy of the object which does not share any references with the original.getProperty(String theProp) Returns the value of the given propertybooleangetPropertyAsBoolean(String theProp) Return the value of the property as a booleanbooleangetPropertyAsBoolean(String theProperty, boolean theDefault) Return the value of the property as a boolean.doublegetPropertyAsDouble(String theProperty) Return the value of the property as a doubledoublegetPropertyAsDouble(String theProperty, double theDefault) Return the value of the property as a doublefloatgetPropertyAsFloat(String theProperty) Return the value of the property as a floatfloatgetPropertyAsFloat(String theProperty, float theDefault) Return the value of the property as a floatintgetPropertyAsInt(String theProp) Return the value of the property as an intintgetPropertyAsInt(String theProp, int theDefault) Return the value of the property as an intgetPropertyAsList(String theProp) Returns the value of a property as a list.longgetPropertyAsLong(String theProperty) Return the value of the property as a longlonggetPropertyAsLong(String theProperty, long theDefault) Return the value of the property as a longgetPropertyAsMap(String theProp) Returns the value of the property as a map.partitionByKeys(Function<String, String> thePartitionFunction) 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.transformKeys(Function<String, String> theKeyFunction) transformValues(Function<String, 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 Details
-
EnhancedProperties
public EnhancedProperties()Create a new EnhancedProperties -
EnhancedProperties
Create a new EnhancedProperties- Parameters:
theInput- the inputstream to load property data from- Throws:
IOException- thrown if there is an error reading properties data
-
EnhancedProperties
Copy constructor- Parameters:
theProps- the properties to copy from
-
-
Method Details
-
getPropertyAsBoolean
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
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
Returns the value of the given property- Overrides:
getPropertyin classProperties- Parameters:
theProp- the property to retrieve- Returns:
- the value of the property, or null if one is not found
-
put
-
getPropertyAsInt
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:
NumberFormatException- thrown if the value is not a valid integer value
-
getPropertyAsInt
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:
NumberFormatException- thrown if the value is not a valid integer value
-
getPropertyAsLong
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:
NumberFormatException- thrown if the value is not a value long value
-
getPropertyAsLong
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:
NumberFormatException- thrown if the value is not a value long value
-
getPropertyAsFloat
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:
NumberFormatException- thrown if the value is not a valid float value
-
getPropertyAsFloat
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:
NumberFormatException- thrown if the value is not a valid float value
-
getPropertyAsDouble
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:
NumberFormatException- thrown if the value is not a valid double value
-
getPropertyAsDouble
public double getPropertyAsDouble(String theProperty, double theDefault) throws 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:
NumberFormatException- thrown if the value is not a valid double value
-
getPropertyAsList
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
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
Create a deep copy of the object which does not share any references with the original.- Specified by:
copyin interfaceCopyable<EnhancedProperties>- Returns:
- a copy of the object
-
select
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
-
transformValues
-
partitionByKeys
-
containsKeyIgnoreCase
Returnstrueif the properties contains a key that matches the supplied key, ignoring case.
-