Class IniFile


  • public final class IniFile
    extends Object
    An INI file.

    Represents an INI file together with the ability to parse it from a CharSource.

    The INI file format used here is deliberately simple. There are two elements - key-value pairs and sections.

    The basic element is a key-value pair. 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.

    All properties are grouped into named sections. The section name occurs on a line by itself surrounded by square brackets. Duplicate section names are not allowed. For example '[section]'.

    Keys, values and section names 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
      [foo]
      key = value
     
      [bar]
      key = value
      month = January
     

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

    • Method Detail

      • of

        public static IniFile of​(CharSource source)
        Parses the specified source as an INI file.

        This parses the specified character source expecting an INI file format. The resulting instance can be queried for each section in the file.

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

        Parameters:
        source - the INI file resource
        Returns:
        the INI file
        Throws:
        UncheckedIOException - if an IO exception occurs
        IllegalArgumentException - if the file cannot be parsed
      • of

        public static IniFile of​(Map<String,​PropertySet> sectionMap)
        Obtains an instance, specifying the map of section to properties.
        Parameters:
        sectionMap - the map of sections
        Returns:
        the INI file
      • sections

        public ImmutableSet<String> sections()
        Returns the set of sections of this INI file.
        Returns:
        the set of sections
      • asMap

        public ImmutableMap<String,​PropertySet> asMap()
        Returns the INI file as a map.

        The iteration order of the map matches that of the original file.

        Returns:
        the INI file sections
      • contains

        public boolean contains​(String name)
        Checks if this INI file contains the specified section.
        Parameters:
        name - the section name
        Returns:
        true if the section exists
      • section

        public PropertySet section​(String name)
        Gets a single section of this INI file.

        This returns the section associated with the specified name. If the section does not exist an exception is thrown.

        Parameters:
        name - the section name
        Returns:
        the INI file section
        Throws:
        IllegalArgumentException - if the section does not exist
      • findSection

        public Optional<PropertySet> findSection​(String name)
        Finds a single section in this INI file.

        This returns the section associated with the specified name, empty if not present.

        Parameters:
        name - the section name
        Returns:
        the INI file section, empty if not found
      • combinedWith

        public IniFile combinedWith​(IniFile other)
        Combines this file with another.

        This file takes precedence. Where a key exists in both files the values in the other file will be discarded. Any order of any additional keys will be retained, with those keys located after the base set of keys.

        Parameters:
        other - the other INI file
        Returns:
        the combined INI file
      • equals

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

        The comparison checks the content.

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

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

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