Class 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 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:
    Serialized Form
    • Field Summary

      • Fields inherited from class java.util.Properties

        defaults
    • 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)
      Returns true 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 property
      boolean getPropertyAsBoolean​(java.lang.String theProp)
      Return the value of the property as a boolean
      boolean 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 double
      double getPropertyAsDouble​(java.lang.String theProperty, double theDefault)
      Return the value of the property as a double
      float getPropertyAsFloat​(java.lang.String theProperty)
      Return the value of the property as a float
      float getPropertyAsFloat​(java.lang.String theProperty, float theDefault)
      Return the value of the property as a float
      int getPropertyAsInt​(java.lang.String theProp)
      Return the value of the property as an int
      int getPropertyAsInt​(java.lang.String theProp, int theDefault)
      Return the value of the property as an int
      java.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 long
      long getPropertyAsLong​(java.lang.String theProperty, long theDefault)
      Return the value of the property as a long
      java.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
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • 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 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 java.lang.String getProperty​(java.lang.String theProp)
        Returns the value of the given property
        Overrides:
        getProperty in class java.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 interface java.util.Map<java.lang.Object,​java.lang.Object>
        Overrides:
        put in class java.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 retrieve
        theDefault - 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 retrieve
        theDefault - 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 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:
        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 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:
        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.
      • 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)
        Returns true if the properties contains a key that matches the supplied key, ignoring case.