Class TypedProperties

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.Object,​java.lang.Object>

    public class TypedProperties
    extends java.util.Properties
    a sub-class of java.util.Properties that provides the same constructors, adds two convenient load methods to load the properties from files and, most importantly, adds getPropertyAsXXX() methods to get a property as an object of type XXX.
    Author:
    Gerald Loeffler for the IMP
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_DELIMITERS
      the default string of delimiter characters used by getAsStringList()
      • Fields inherited from class java.util.Properties

        defaults
    • Constructor Summary

      Constructors 
      Constructor Description
      TypedProperties()
      Creates an empty property list with no default values.
      TypedProperties​(java.util.Properties defaults)
      Creates an empty property list with the specified defaults.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object clone()  
      boolean equals​(java.lang.Object o)  
      java.lang.Boolean getPropertyAsBoolean​(java.lang.String key)
      Searches for the property with the specified key in this property list.
      java.lang.Double getPropertyAsDouble​(java.lang.String key)
      Searches for the property with the specified key in this property list.
      java.lang.Integer getPropertyAsInteger​(java.lang.String key)
      Searches for the property with the specified key in this property list.
      java.lang.Long getPropertyAsLong​(java.lang.String key)
      Searches for the property with the specified key in this property list.
      java.util.List getPropertyAsStringList​(java.lang.String key)
      just like getPropertyAsStringList(String key, String delims) but uses ',' (comma), ';' (semicolon) and '\t' (tab) as the possible delimiters.
      java.util.List getPropertyAsStringList​(java.lang.String key, java.lang.String delims)
      Searches for the property with the specified key in this property list.
      int hashCode()  
      void load​(java.lang.Class clazz, java.lang.String resourceName)
      Reads a property list (key and element pairs) from the given file which is interpreted as a resource of the given class.
      void load​(java.lang.String fileName)
      Reads a property list (key and element pairs) from the file with the given file name.
      java.lang.String toString()  
      • Methods inherited from class java.util.Properties

        clear, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, forEach, get, getOrDefault, getProperty, getProperty, isEmpty, keys, keySet, list, list, load, load, loadFromXML, merge, propertyNames, put, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, save, setProperty, size, store, store, storeToXML, storeToXML, storeToXML, stringPropertyNames, values
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • TypedProperties

        public TypedProperties()
        Creates an empty property list with no default values.
      • TypedProperties

        public TypedProperties​(java.util.Properties defaults)
        Creates an empty property list with the specified defaults.
        Parameters:
        defaults - the defaults.
    • Method Detail

      • load

        public void load​(java.lang.String fileName)
                  throws java.io.FileNotFoundException,
                         java.io.IOException
        Reads a property list (key and element pairs) from the file with the given file name.
        Parameters:
        fileName - the file name. Not null.
        Throws:
        java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
        java.io.IOException - if an error occurred when reading from the input stream created from the file with the given name.
      • load

        public void load​(java.lang.Class clazz,
                         java.lang.String resourceName)
                  throws java.io.IOException
        Reads a property list (key and element pairs) from the given file which is interpreted as a resource of the given class. The difference between a normal file and a resource file is the way in which the file is located: with a normal file, the filename is taken literally to load the file from the file system, whereas with a resource file, the given name is used to ask the class loader of the given class to load the file (see java.lang.Class.getResourceAsStream() and java.lang.ClassLoader.getSystemResourceAsStream()).
        Parameters:
        clazz - the class with which the resource identified by resourceName is taken to be associated with (java.lang.Class.getResourceAsStream() on this Class object is used to load the resource). If clazz is null, the resource is considered to be a system resource, and java.lang.ClassLoader.getSystemResourceAsStream() is used to load the resource.
        resourceName - the name of the resource from which to load the properties. It is a precondition that the resource with this name exists (regardless whether it is interpreted as a system resource or a class resource), otherwise an IllegalArgumentException is thrown.
        Throws:
        java.io.IOException - if an error occurred when reading from the input stream created from the given resource.
        See Also:
        Class, ClassLoader
      • getPropertyAsInteger

        public java.lang.Integer getPropertyAsInteger​(java.lang.String key)
                                               throws java.lang.NumberFormatException
        Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found. If the property is found its value is parsed as an integer and returned. If parsing the value fails, a NumberFormatException is thrown.
        Parameters:
        key - the property key.
        Returns:
        the integer value of the property with the given key or null if the given key is not associated with a property.
        Throws:
        java.lang.NumberFormatException - if the property associated with the given key does not have an integer value.
      • getPropertyAsLong

        public java.lang.Long getPropertyAsLong​(java.lang.String key)
                                         throws java.lang.NumberFormatException
        Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found. If the property is found its value is parsed as a long and returned. If parsing the value fails, a NumberFormatException is thrown.
        Parameters:
        key - the property key.
        Returns:
        the long value of the property with the given key or null if the given key is not associated with a property.
        Throws:
        java.lang.NumberFormatException - if the property associated with the given key does not have an integer value.
      • getPropertyAsDouble

        public java.lang.Double getPropertyAsDouble​(java.lang.String key)
                                             throws java.lang.NumberFormatException
        Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found. If the property is found its value is parsed as a double and returned. If parsing the value fails, a NumberFormatException is thrown.
        Parameters:
        key - the property key.
        Returns:
        the double value of the property with the given key or null if the given key is not associated with a property.
        Throws:
        java.lang.NumberFormatException - if the property associated with the given key does not have an integer value.
      • getPropertyAsBoolean

        public java.lang.Boolean getPropertyAsBoolean​(java.lang.String key)
                                               throws java.lang.RuntimeException
        Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found. If the property is found its value is parsed as an boolean and returned. If parsing the value fails, a RuntimeException is thrown.

        If the property value is equal, ignoring case, to the string "true" or "yes" then the boolean value returned from this method is true. If the property value is equal, ignoring case, to the string "false" or "no" then the boolean value returned from this method is false.

        Parameters:
        key - the property key.
        Returns:
        the boolean value of the property with the given key or null if the given key is not associated with a property.
        Throws:
        java.lang.RuntimeException - if the property associated with the given key does not have an integer value.
      • getPropertyAsStringList

        public java.util.List getPropertyAsStringList​(java.lang.String key,
                                                      java.lang.String delims)
        Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found. If the property is found its value is parsed as a list of strings and returned as a List object that contains only String objects. Parsing the property value as a list of strings can not fail and so this method does not throw an exception.

        The property value is interpreted as String objects (tokens) separated by one or more (consecutive) separator characters taken from the delims string. Any of these characters separates the tokens and can hence not be part of any token! The tokens identified in this way are put into a List in the order in which they appear in the property value. White space at the beginning and end of each token are removed before storing the token as an element of the list (this includes white space at the beginning and end of the complete property value)! Empty strings are also never added to the list, i.e. if after removal of white space from a token a token is the empty string, it is not stored in the list! All this results in a very natural conversion of the property value into a list of strings: only "real" (non-white-space, non-white-space-bounded, non-delimiter-containing) sub-strings from the property value are put as string elements into the list.

        Parameters:
        key - the property key.
        delims - the string of allowed delimiter characters (not null and not empty).
        Returns:
        the List of strings for the property with the given key or null if the given key is not associated with a property. An empty list is returned if a property with the given key exists but its value is empty or consists only of white space.
      • getPropertyAsStringList

        public java.util.List getPropertyAsStringList​(java.lang.String key)
        just like getPropertyAsStringList(String key, String delims) but uses ',' (comma), ';' (semicolon) and '\t' (tab) as the possible delimiters.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.util.Properties
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Map<java.lang.Object,​java.lang.Object>
        Overrides:
        equals in class java.util.Properties
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<java.lang.Object,​java.lang.Object>
        Overrides:
        hashCode in class java.util.Properties
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class java.util.Properties