Package adams.core

Class Properties

    • Constructor Detail

      • Properties

        public Properties()
        default constructor.
      • Properties

        public Properties​(Properties defaults)
        Creates an empty property list with the specified defaults.
        Parameters:
        defaults - the default properties
      • Properties

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

      • initialize

        protected void initialize()
        Initializes member variables.
      • setDefaults

        public void setDefaults​(Properties value)
        Sets the default properties.
        Parameters:
        value - the default properties
      • read

        public static Properties read​(String name)
                               throws Exception
        Reads properties that inherit from several locations. Properties are first defined in the system resource location (i.e. in the CLASSPATH). These default properties must exist. Not to be confused with load(String). Also looks for environment variables with a semi-colon separated list of key=value pairs to override the values loaded from the .props file(s). E.g., for "adams/core/io/FileUtils.props" the environment variables "adams_core_io_FileUtils" or "ADAMS_CORE_IO_FILEUTILS" can be used.
        Parameters:
        name - the location of the resource that should be loaded. e.g.: "adams/gui/Something.props".
        Returns:
        the Properties
        Throws:
        Exception - if no default properties are defined, or if an error occurs reading the properties files.
        See Also:
        load(String)
      • getPlatformDependentExtension

        public static String getPlatformDependentExtension()
        Determines the platform-specific extension.
        Returns:
        the extension (incl dot), null if failed to determine
      • loadURLs

        protected static Properties loadURLs​(Properties parent,
                                             String path)
        Loads the properties from URLs associated with the path.
        Parameters:
        parent - the parent Properties object, null if first call
        path - the path of the properties to load
        Returns:
        the properties
      • propertiesExist

        protected static boolean propertiesExist​(String path)
        Checks whether the properties file exists.
        Parameters:
        path - the file name
        Returns:
        true if it exists
      • read

        public static Properties read​(String name,
                                      List<String> dirs)
                               throws Exception
        Reads properties that inherit from several locations. Properties are first defined in the system resource location (i.e. in the CLASSPATH). These default properties must exist. Not to be confused with load(String). Also looks for environment variables with a semi-colon separated list of key=value pairs to override the values loaded from the .props file(s). E.g., for "adams/core/io/FileUtils.props" the environment variables "adams_core_io_FileUtils" or "ADAMS_CORE_IO_FILEUTILS" can be used.
        Parameters:
        name - the location of the resource that should be loaded. e.g.: "adams/gui/Something.props".
        dirs - the directories to search for this properties file.
        Returns:
        the Properties
        Throws:
        Exception - if no default properties are defined, or if an error occurs reading the properties files.
        See Also:
        load(String)
      • expandPlaceHolders

        protected String expandPlaceHolders​(String s)
        Expands the placeholders in the string. Supported placeholders:
         %t - the temp directory
         %h - the user's home directory (also $HOME and %USERHOME%)
         %p - the project's home directory (if such is available)
         %c - the current directory
         %% - gets replaced by a single percentage sign
         
        Parameters:
        s - the string to process
        Returns:
        the processed string
      • fixDir

        protected String fixDir​(String dir)
        Ensures that the directory ends with a separator, e.g. "/".
        Parameters:
        dir - the directory to fix
        Returns:
        the fixed directory
      • collapsePlaceHolders

        protected String collapsePlaceHolders​(String s)
        Collapses the paths to placeholders in the string. Supported placeholders (from highest precedence to lowest):
         %c - the current directory
         %p - the project's home directory (if such is available)
         %h - the user's home directory
         %t - the temp directory
         
        Parameters:
        s - the string to process
        Returns:
        the processed string
      • keySetAll

        public Set<String> keySetAll()
        Returns a set of all the keys.
        Returns:
        a set over all the keys
      • keySetAll

        public Set<String> keySetAll​(String prefix)
        Returns a set of all the keys that have the specified prefix.
        Parameters:
        prefix - the prefix that the keys must have
        Returns:
        a set over all the keys
      • keySetAll

        public Set<String> keySetAll​(BaseRegExp regexp)
        Returns a set of all the keys that match the specified regular expression.
        Parameters:
        regexp - the expression to match the keys against
        Returns:
        a set over all the keys
      • propertyNames

        public Enumeration<String> propertyNames​(String regExp)
        Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list, as long as the key matches the provided prefix.
        Parameters:
        regExp - the regular expression that the property keys must match
        Returns:
        an enumeration of all the keys in this property list, including the keys in the default property list, that match the prefix.
      • load

        public boolean load​(String filename)
        Loads the properties from the given file - not to be confused with read(String).
        Parameters:
        filename - the file to load the properties from
        Returns:
        true if successfully loaded
        See Also:
        read(String)
      • save

        public boolean save​(String filename)
        Saves the properties to the given file (unsorted).
        Parameters:
        filename - the file to save the properties to
        Returns:
        true if successfully written
      • save

        public boolean save​(String filename,
                            boolean sorted)
        Saves the properties to the given file.
        Parameters:
        filename - the file to save the properties to
        sorted - whether to sort the content
        Returns:
        true if successfully written
      • save

        public boolean save​(String filename,
                            String comment)
        Saves the properties to the given file (unsorted).
        Parameters:
        filename - the file to save the properties to
        comment - the comment to use, can be null
        Returns:
        true if successfully written
      • save

        public boolean save​(String filename,
                            String comment,
                            boolean sorted)
        Saves the properties to the given file.
        Parameters:
        filename - the file to save the properties to
        comment - the comment to use, can be null
        sorted - whether to sort the content
        Returns:
        true if successfully written
      • toStringSimple

        public String toStringSimple()
        Outputs a single line representation of the full properties stored in the Properties object (and its defaults).
        Returns:
        the single-line representation
      • toString

        public String toString()
        Outputs the properties as they would be written to a file (unsorted).
        Overrides:
        toString in class Properties
        Returns:
        the generated output or null in case of an error
      • toString

        public String toString​(String comment)
        Outputs the properties as they would be written to a file (unsorted).
        Parameters:
        comment - the comment to output
        Returns:
        the generated output or null in case of an error
      • toString

        public String toString​(boolean sorted)
        Outputs the properties as they would be written to a file.
        Parameters:
        sorted - whether to store the output
        Returns:
        the generated output or null in case of an error
      • toString

        public String toString​(String comment,
                               boolean sorted)
        Outputs the properties as they would be written to a file.
        Parameters:
        comment - the comment to output
        sorted - whether to store the output
        Returns:
        the generated output or null in case of an error
      • toComment

        public String toComment()
        Outputs the properties as they would be written to a file (unsorted). In addition, all lines will be output as comment:
         # key1=value1
         # key2=value2
         # ...
         
        Returns:
        the generated output or null in case of an error
        See Also:
        COMMENT
      • toComment

        public String toComment​(boolean sorted)
        Outputs the properties as they would be written to a file. In addition, all lines will be output as comment:
         # key1=value1
         # key2=value2
         # ...
         
        Parameters:
        sorted - whether to sort the output
        Returns:
        the generated output or null in case of an error
        See Also:
        COMMENT
      • add

        public void add​(Properties props)
        Adds the key-value pairs stored in the specified properties to itself. The property names are not prefixed. Already existing properties will be overwritten.
        Parameters:
        props - the properties to add
      • add

        public void add​(Properties props,
                        String prefix)
        Adds the key-value pairs stored in the specified properties to itself. Already existing properties will be overwritten.
        Parameters:
        props - the properties to add
        prefix - the prefix for the property names, use null to ignore
      • subset

        public Properties subset​(String prefix)
        Returns a subset of all the keys that match the given prefix.
        Parameters:
        prefix - the prefix for the property names
        Returns:
        the subset
      • subset

        public Properties subset​(BaseRegExp regexp)
        Returns a subset of all the keys that match the given regular expression.
        Parameters:
        regexp - the regular expression
        Returns:
        the subset
      • removeKey

        public void removeKey​(String key)
        Removes the entry with the specified key, if available.
        Parameters:
        key - the key to remove
      • removeKeyRecursive

        public void removeKeyRecursive​(String key)
        Removes the entry with the specified key, if available. Processes the defaults recursively.
        Parameters:
        key - the key to remove
      • removeKeys

        public void removeKeys​(String regexp)
        Removes the entries that match the regular expression.
        Parameters:
        regexp - the regular expression that the keys must match
      • removeKeysRecursive

        public void removeKeysRecursive​(String regexp)
        Removes the entries that match the specified regular expression. Processes the defaults recursively.
        Parameters:
        regexp - the regular expression that the keys must match
      • hasKey

        public boolean hasKey​(String key)
        Checks whether the given property exists.
        Parameters:
        key - the property to check
        Returns:
        true if the property exists
      • setInteger

        public void setInteger​(String key,
                               Integer value)
        Sets the integer value.
        Parameters:
        key - the property to store the value under
        value - the value to store
      • getInteger

        public Integer getInteger​(String key)
        Returns the integer value associated with the key, or null if not found or not parseable.
        Parameters:
        key - the property to look for
        Returns:
        the value for the key
      • getInteger

        public Integer getInteger​(String key,
                                  Integer defValue)
        Returns the integer value associated with the key, or the default value if not found or not parseable.
        Parameters:
        key - the property to look for
        defValue - the default value in case key is not found or value cannot be parsed
        Returns:
        the value for the key
      • setLong

        public void setLong​(String key,
                            Long value)
        Sets the long value.
        Parameters:
        key - the property to store the value under
        value - the value to store
      • getLong

        public Long getLong​(String key)
        Returns the long value associated with the key, or null if not found or not parseable.
        Parameters:
        key - the property to look for
        Returns:
        the value for the key
      • getLong

        public Long getLong​(String key,
                            Long defValue)
        Returns the long value associated with the key, or the default value if not found or not parseable.
        Parameters:
        key - the property to look for
        defValue - the default value in case key is not found or value cannot be parsed
        Returns:
        the value for the key
      • setDouble

        public void setDouble​(String key,
                              Double value)
        Sets the double value.
        Parameters:
        key - the property to store the value under
        value - the value to store
      • getDouble

        public Double getDouble​(String key)
        Returns the double value associated with the key, or null if not found or not parseable.
        Parameters:
        key - the property to look for
        Returns:
        the value for the key
      • getDouble

        public Double getDouble​(String key,
                                Double defValue)
        Returns the double value associated with the key, or the default value if not found or not parseable.
        Parameters:
        key - the property to look for
        defValue - the default value in case key is not found or value cannot be parsed
        Returns:
        the value for the key
      • setBoolean

        public void setBoolean​(String key,
                               Boolean value)
        Sets the boolean value.
        Parameters:
        key - the property to store the value under
        value - the value to store
      • getBoolean

        public Boolean getBoolean​(String key)
        Returns the boolean value associated with the key, or null if not found or not parseable.
        Parameters:
        key - the property to look for
        Returns:
        the value for the key
      • getBoolean

        public Boolean getBoolean​(String key,
                                  Boolean defValue)
        Returns the boolean value associated with the key, or the default value if not found or not parseable.
        Parameters:
        key - the property to look for
        defValue - the default value in case key is not found or value cannot be parsed
        Returns:
        the value for the key
      • setTime

        public void setTime​(String key,
                            Time value)
        Sets the time value.
        Parameters:
        key - the property to store the value under
        value - the value to store
      • getTime

        public Time getTime​(String key)
        Returns the time value associated with the key, or null if not found or not parseable.
        Parameters:
        key - the property to look for
        Returns:
        the value for the key
      • getTime

        public Time getTime​(String key,
                            Time defValue)
        Returns the time value associated with the key, or the default value if not found or not parseable.
        Parameters:
        key - the property to look for
        defValue - the default value in case key is not found or value cannot be parsed
        Returns:
        the value for the key
      • setDate

        public void setDate​(String key,
                            Date value)
        Sets the date value.
        Parameters:
        key - the property to store the value under
        value - the value to store
      • getDate

        public Date getDate​(String key)
        Returns the date value associated with the key, or null if not found or not parseable.
        Parameters:
        key - the property to look for
        Returns:
        the value for the key
      • getDate

        public Date getDate​(String key,
                            Date defValue)
        Returns the date value associated with the key, or the default value if not found or not parseable.
        Parameters:
        key - the property to look for
        defValue - the default value in case key is not found or value cannot be parsed
        Returns:
        the value for the key
      • setDateTime

        public void setDateTime​(String key,
                                DateTime value)
        Sets the date/time value.
        Parameters:
        key - the property to store the value under
        value - the value to store
      • getDateTime

        public DateTime getDateTime​(String key)
        Returns the date/time value associated with the key, or null if not found or not parseable.
        Parameters:
        key - the property to look for
        Returns:
        the value for the key
      • getDateTime

        public DateTime getDateTime​(String key,
                                    DateTime defValue)
        Returns the date/time value associated with the key, or the default value if not found or not parseable.
        Parameters:
        key - the property to look for
        defValue - the default value in case key is not found or value cannot be parsed
        Returns:
        the value for the key
      • setPath

        public void setPath​(String key,
                            String value)
        Sets the path value.
        Parameters:
        key - the property to store the value under
        value - the value to store
        See Also:
        collapsePlaceHolders(String)
      • getPath

        public String getPath​(String key)
        Returns the path value associated with the key, or null if not found.
        Parameters:
        key - the property to look for
        Returns:
        the value for the key
        See Also:
        expandPlaceHolders(String)
      • getPath

        public String getPath​(String key,
                              String defValue)
        Returns the path value associated with the key, or the default value if not found.
        Parameters:
        key - the property to look for
        defValue - the default value in case key is not found
        Returns:
        the value for the key
        See Also:
        expandPlaceHolders(String)
      • getColor

        public Color getColor​(String key)
        Returns the color associated with the string.
        Parameters:
        key - the property to look for
        Returns:
        the value or null if not found
      • getColor

        public Color getColor​(String key,
                              Color defValue)
        Returns the color associated with the string.
        Parameters:
        key - the property to look for
        defValue - the default value
        Returns:
        the value or defValue if not found
      • setColor

        public void setColor​(String key,
                             Color value)
        Stores the color as hex string under the specified property key.
        Parameters:
        key - the property to store the color under
        value - the color to store
      • getPassword

        public BasePassword getPassword​(String key)
        Returns the password associated with the string.
        Parameters:
        key - the property to look for
        Returns:
        the value or null if not found
      • getPassword

        public BasePassword getPassword​(String key,
                                        BasePassword defValue)
        Returns the password associated with the string.
        Parameters:
        key - the property to look for
        defValue - the default value
        Returns:
        the value or defValue if not found
      • setPassword

        public void setPassword​(String key,
                                BasePassword value)
        Stores the password as 64-bit encoded string under the specified property key.
        Parameters:
        key - the property to store the password under
        value - the password to store
      • getFont

        public Font getFont​(String key)
        Returns the font associated with the string.
        Parameters:
        key - the property to look for
        Returns:
        the value or null if not found
      • getFont

        public Font getFont​(String key,
                            Font defValue)
        Returns the font associated with the string.
        Parameters:
        key - the property to look for
        defValue - the default value
        Returns:
        the value or defValue if not found
      • setFont

        public void setFont​(String key,
                            Font value)
        Stores the font under the specified property key.
        Parameters:
        key - the property to store the font under
        value - the font to store
      • setObject

        public void setObject​(String key,
                              Object obj)
        Stores the object as commandline.
        Parameters:
        key - the key to store the object under
        obj - the object to store
      • getObject

        public <T> T getObject​(String key,
                               Class<T> cls)
        Returns the object stored as commandline.
        Type Parameters:
        T - the type of the object
        Parameters:
        key - the key with the commandline
        cls - the type of the object
        Returns:
        the object, null if failed to instantiate
      • getObject

        public <T> T getObject​(String key,
                               Class<T> cls,
                               T defValue)
        Returns the object stored as commandline.
        Type Parameters:
        T - the type of the object
        Parameters:
        key - the key with the commandline
        cls - the type of the object
        defValue - the default value
        Returns:
        the object, null if failed to instantiate
      • mergeWith

        public void mergeWith​(Properties other)
        Merges its own data with the one provided by the specified object.
        Specified by:
        mergeWith in interface Mergeable<Properties>
        Parameters:
        other - the object to merge with
      • removeWithPrefix

        public void removeWithPrefix​(String prefix)
        Removes all properties that share this prefix.
        Parameters:
        prefix - the prefix to look for
      • removeWithSuffix

        public void removeWithSuffix​(String suffix)
        Removes all properties that share this suffix.
        Parameters:
        suffix - the suffix to look for
      • collapse

        public Properties collapse()
        Collapses all the inherited and current properties into a single Properties object and returns it.
        Returns:
        the collapsed version of this Properties object
      • compareTo

        public int compareTo​(Properties o)
        Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
        Specified by:
        compareTo in interface Comparable<Properties>
        Parameters:
        o - the object to be compared.
        Returns:
        a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
        Throws:
        ClassCastException - if the specified object's type prevents it from being compared to this object.
      • equals

        public boolean equals​(Object obj)
        Indicates whether some other object is "equal to" this one.
        Specified by:
        equals in interface Map<Object,​Object>
        Overrides:
        equals in class Properties
        Parameters:
        obj - the reference object with which to compare.
        Returns:
        true if this object is the same as the obj argument; false otherwise.
      • fromComment

        public static Properties fromComment​(String comments)
        Re-generates a properties object that was commented out.
        Parameters:
        comments - the commented out properties content
        Returns:
        the properties object, or null in case of an error
        See Also:
        toComment()
      • loadFromResource

        public static boolean loadFromResource​(Properties props,
                                               String resourceName)
        Loads the properties with the given name as a classpath resource.
        Parameters:
        resourceName - the name of the props file on the classpath
        Returns:
        true if successful
      • main

        public static void main​(String[] args)
                         throws Exception
        Only for testing. Exepects the name of a props file as first parameter, tries to load it and then dumps the content to stdout.
        Parameters:
        args - the commandline arguments
        Throws:
        Exception - if an error occurs