Class EnhancedProperties

All Implemented Interfaces:
Copyable<EnhancedProperties>, Serializable, Cloneable, Map<Object,Object>

public class EnhancedProperties extends 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 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:
  • Constructor Details

    • EnhancedProperties

      public EnhancedProperties()
      Create a new EnhancedProperties
    • EnhancedProperties

      public EnhancedProperties(InputStream theInput) throws IOException
      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

      public EnhancedProperties(Properties theProps)
      Copy constructor
      Parameters:
      theProps - the properties to copy from
  • Method Details

    • getPropertyAsBoolean

      public boolean getPropertyAsBoolean(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(String theProperty, boolean theDefault)
      Return the value of the property as a boolean.
      Parameters:
      theProperty - the property to retrieve
      theDefault - the default value if the property does not exist
      Returns:
      the value of the property if it exists, otherwise the default value
    • getProperty

      public String getProperty(String theProp)
      Returns the value of the given property
      Overrides:
      getProperty in class Properties
      Parameters:
      theProp - the property to retrieve
      Returns:
      the value of the property, or null if one is not found
    • put

      public Object put(Object theKey, Object theValue)
      Specified by:
      put in interface Map<Object,Object>
      Overrides:
      put in class Properties
    • getPropertyAsInt

      public int getPropertyAsInt(String theProp, int theDefault) throws NumberFormatException
      Return the value of the property as an int
      Parameters:
      theProp - the property to retrieve
      theDefault - 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

      public int getPropertyAsInt(String theProp) throws 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:
      NumberFormatException - thrown if the value is not a valid integer value
    • getPropertyAsLong

      public long getPropertyAsLong(String theProperty, long theDefault) throws NumberFormatException
      Return the value of the property as a long
      Parameters:
      theProperty - the property to retrieve
      theDefault - 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

      public long getPropertyAsLong(String theProperty) throws 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:
      NumberFormatException - thrown if the value is not a value long value
    • getPropertyAsFloat

      public float getPropertyAsFloat(String theProperty, float theDefault) throws NumberFormatException
      Return the value of the property as a float
      Parameters:
      theProperty - the property to retrieve
      theDefault - 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

      public float getPropertyAsFloat(String theProperty) throws 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:
      NumberFormatException - thrown if the value is not a valid float value
    • getPropertyAsDouble

      public double getPropertyAsDouble(String theProperty) throws 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:
      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 retrieve
      theDefault - 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

      public List<String> getPropertyAsList(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 Map<String,String> getPropertyAsMap(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 interface Copyable<EnhancedProperties>
      Returns:
      a copy of the object
    • select

      public EnhancedProperties select(Predicate<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(Function<String,String> theKeyFunction)
    • transformValues

      public EnhancedProperties transformValues(Function<String,String> theValueFunction)
    • partitionByKeys

      public Iterable<EnhancedProperties> partitionByKeys(Function<String,String> thePartitionFunction)
    • containsKeyIgnoreCase

      public static boolean containsKeyIgnoreCase(Properties properties, String key)
      Returns true if the properties contains a key that matches the supplied key, ignoring case.