Class PropertiesFile


  • public final class PropertiesFile
    extends java.lang.Object
    A properties file.

    Represents a properties file together with the ability to parse it from a CharSource. This is similar to Properties but with a simpler format and without extending Map. Duplicate keys are allowed and handled.

    The properties file format used here is deliberately simple. There is only one element - key-value pairs.

    The key is separated from the value using the '=' symbol. The string ' = ' is searched for before '=' to allow an equals sign to be present in the key, which implies that this string cannot be in either the key or the value. Duplicate keys are allowed. For example 'key = value'. The equals sign and value may be omitted, in which case the value is an empty string.

    Keys and values are trimmed. Blank lines are ignored. Whole line comments begin with hash '#' or semicolon ';'. No escape format is available. Lookup is case sensitive.

    This example explains the format:

      # line comment
      key = value
      month = January
     

    The aim of this class is to parse the basic format. Interpolation of variables is not supported.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)
      Checks if this file equals another.
      PropertySet getProperties()
      Gets all the key-value properties of this file.
      int hashCode()
      Returns a suitable hash code for the file.
      static PropertiesFile of​(com.google.common.io.CharSource source)
      Parses the specified source as a properties file.
      static PropertiesFile of​(PropertySet keyValueMap)
      Obtains an instance from a key-value property set.
      java.lang.String toString()
      Returns a string describing the file.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • of

        public static PropertiesFile of​(com.google.common.io.CharSource source)
        Parses the specified source as a properties file.

        This parses the specified character source expecting a properties file format. The resulting instance can be queried for each key and value.

        Properties files sometimes contain a Unicode Byte Order Mark. Callers are responsible for handling this, such as by using UnicodeBom.

        Parameters:
        source - the properties file resource
        Returns:
        the properties file
        Throws:
        java.io.UncheckedIOException - if an IO exception occurs
        java.lang.IllegalArgumentException - if the file cannot be parsed
      • of

        public static PropertiesFile of​(PropertySet keyValueMap)
        Obtains an instance from a key-value property set.
        Parameters:
        keyValueMap - the key-value property set
        Returns:
        the properties file
      • getProperties

        public PropertySet getProperties()
        Gets all the key-value properties of this file.

        The map of key-value properties is exposed by this method.

        Returns:
        the key-value properties
      • equals

        public boolean equals​(java.lang.Object obj)
        Checks if this file equals another.

        The comparison checks the content.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the other section, null returns false
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        Returns a suitable hash code for the file.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code
      • toString

        public java.lang.String toString()
        Returns a string describing the file.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the descriptive string