Class FormatPreservingProperties

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

    public class FormatPreservingProperties
    extends java.util.Properties
    A specialization of Properties that stores its contents in the same order and with the same formatting as was used to read the contents from an input stream. This is useful because it means that a properties file loaded via this object and then written back out later on will only be different where changes or additions were made. By default, the store(java.io.OutputStream, java.lang.String) method in this class does not behave the same as Properties.store(java.io.Writer, java.lang.String). You can cause an instance to approximate the behavior of Properties.store(java.io.Writer, java.lang.String) by invoking setDefaultEntryDelimiter(char) with =, setAddWhitespaceAfterDelimiter(boolean) with false, and setAllowDuplicates(boolean) with true. However, this will only influence how the instance will write new values, not how it will write existing key-value pairs that are modified. In conjunction with a conservative output writer, it is possible to only write to disk changes / additions. This implementation does not permit escaped ' ', '=', ':' characters in key names.
    Since:
    0.3.3
    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from class java.util.Properties

        defaults
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      java.lang.Object clone()  
      boolean getAddWhitespaceAfterDelimiter()
      If set to true, this properties object will add a space after the delimiter character(if the delimiter is not the space character).
      boolean getAllowDuplicates()
      If set to true, duplicate properties are allowed, and the last property setting in the input will overwrite any previous settings.
      char getDefaultEntryDelimiter()
      boolean getInsertTimestamp()
      If set to true, this properties object will add a timestamp to the beginning of the file, just after the header (if any) is printed.
      java.lang.String getProperty​(java.lang.String key)  
      java.lang.String getProperty​(java.lang.String key, java.lang.String defaultValue)  
      void load​(java.io.InputStream in)
      Loads the properties in in, according to the rules described in Properties.load(java.io.Reader).
      java.lang.Object put​(java.lang.Object key, java.lang.Object val)  
      void putAll​(java.util.Map m)
      Circumvents the superclass putAll(java.util.Map) implementation, putting all the key-value pairs via put(java.lang.Object, java.lang.Object).
      java.lang.Object remove​(java.lang.Object key)
      Removes the key from the bookkeeping collectiotns as well.
      void save​(java.io.OutputStream out, java.lang.String header)  
      void setAddWhitespaceAfterDelimiter​(boolean add)
      If set to true, this properties object will add a space after the delimiter character(if the delimiter is not the space character).
      void setAllowDuplicates​(boolean allowDuplicates)
      If set to true, duplicate properties are allowed, and the last property setting in the input will overwrite any previous settings.
      void setDefaultEntryDelimiter​(char defaultEntryDelimiter)
      The character to use as a delimiter between property keys and values.
      void setInsertTimestamp​(boolean insertTimestamp)
      If set to true, this properties object will add a timestamp to the beginning of the file, just after the header (if any) is printed.
      java.lang.Object setProperty​(java.lang.String key, java.lang.String value)  
      void store​(java.io.OutputStream out, java.lang.String header)  
      • Methods inherited from class java.util.Properties

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

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

      • FormatPreservingProperties

        public FormatPreservingProperties()
      • FormatPreservingProperties

        public FormatPreservingProperties​(java.util.Properties defaults)
    • Method Detail

      • setDefaultEntryDelimiter

        public void setDefaultEntryDelimiter​(char defaultEntryDelimiter)
        The character to use as a delimiter between property keys and values.
        Parameters:
        defaultEntryDelimiter - either ':' or '='
      • setAddWhitespaceAfterDelimiter

        public void setAddWhitespaceAfterDelimiter​(boolean add)
        If set to true, this properties object will add a space after the delimiter character(if the delimiter is not the space character). Else, this will not add a space. Default value: true. Note that Properties.store(java.io.Writer, java.lang.String) never writes whitespace.
      • getAddWhitespaceAfterDelimiter

        public boolean getAddWhitespaceAfterDelimiter()
        If set to true, this properties object will add a space after the delimiter character(if the delimiter is not the space character). Else, this will not add a space. Default value: true. Note that Properties.store(java.io.Writer, java.lang.String) never writes whitespace.
      • setInsertTimestamp

        public void setInsertTimestamp​(boolean insertTimestamp)
        If set to true, this properties object will add a timestamp to the beginning of the file, just after the header (if any) is printed. Else, this will not add a timestamp. Default value: false. Note that Properties.store(java.io.Writer, java.lang.String) always writes a timestamp.
      • getInsertTimestamp

        public boolean getInsertTimestamp()
        If set to true, this properties object will add a timestamp to the beginning of the file, just after the header (if any) is printed. Else, this will not add a timestamp. Default value: false. Note that Properties.store(java.io.Writer, java.lang.String) always writes a timestamp.
      • setAllowDuplicates

        public void setAllowDuplicates​(boolean allowDuplicates)
        If set to true, duplicate properties are allowed, and the last property setting in the input will overwrite any previous settings. If set to false, duplicate property definitions in the input will cause an exception to be thrown during load(java.io.InputStream). Default value: false. Note that Properties.store(java.io.Writer, java.lang.String) always allows duplicates.
      • getAllowDuplicates

        public boolean getAllowDuplicates()
        If set to true, duplicate properties are allowed, and the last property setting in the input will overwrite any previous settings. If set to false, duplicate property definitions in the input will cause an exception to be thrown during load(java.io.InputStream). Default value: false. Note that Properties.store(java.io.Writer, java.lang.String) always allows duplicates.
      • getProperty

        public java.lang.String getProperty​(java.lang.String key)
        Overrides:
        getProperty in class java.util.Properties
      • getProperty

        public java.lang.String getProperty​(java.lang.String key,
                                            java.lang.String defaultValue)
        Overrides:
        getProperty in class java.util.Properties
      • setProperty

        public java.lang.Object setProperty​(java.lang.String key,
                                            java.lang.String value)
        Overrides:
        setProperty in class java.util.Properties
      • putAll

        public void putAll​(java.util.Map m)
        Circumvents the superclass putAll(java.util.Map) implementation, putting all the key-value pairs via put(java.lang.Object, java.lang.Object).
        Specified by:
        putAll in interface java.util.Map<java.lang.Object,​java.lang.Object>
        Overrides:
        putAll in class java.util.Properties
      • remove

        public java.lang.Object remove​(java.lang.Object key)
        Removes the key from the bookkeeping collectiotns as well.
        Specified by:
        remove in interface java.util.Map<java.lang.Object,​java.lang.Object>
        Overrides:
        remove in class java.util.Properties
      • clear

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

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

        public java.lang.Object put​(java.lang.Object key,
                                    java.lang.Object val)
        Specified by:
        put in interface java.util.Map<java.lang.Object,​java.lang.Object>
        Overrides:
        put in class java.util.Properties
      • load

        public void load​(java.io.InputStream in)
                  throws java.io.IOException
        Loads the properties in in, according to the rules described in Properties.load(java.io.Reader). If getAllowDuplicates() returns true, this will throw a FormatPreservingProperties.DuplicateKeyException if duplicate property declarations are encountered.
        Overrides:
        load in class java.util.Properties
        Throws:
        java.io.IOException
        See Also:
        Properties.load(java.io.Reader)
      • save

        public void save​(java.io.OutputStream out,
                         java.lang.String header)
        Overrides:
        save in class java.util.Properties
      • store

        public void store​(java.io.OutputStream out,
                          java.lang.String header)
                   throws java.io.IOException
        Overrides:
        store in class java.util.Properties
        Throws:
        java.io.IOException