public final class IniFile extends Object
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.
| Modifier and Type | Method and Description |
|---|---|
ImmutableMap<String,PropertySet> |
asMap()
Returns the INI file as a map.
|
IniFile |
combinedWith(IniFile other)
Combines this file with another.
|
boolean |
contains(String name)
Checks if this INI file contains the specified section.
|
boolean |
equals(Object obj)
Checks if this INI file equals another.
|
Optional<PropertySet> |
findSection(String name)
Finds a single section in this INI file.
|
int |
hashCode()
Returns a suitable hash code for the INI file.
|
static IniFile |
of(CharSource source)
Parses the specified source as an INI file.
|
static IniFile |
of(Map<String,PropertySet> sectionMap)
Obtains an instance, specifying the map of section to properties.
|
PropertySet |
section(String name)
Gets a single section of this INI file.
|
ImmutableSet<String> |
sections()
Returns the set of sections of this INI file.
|
String |
toString()
Returns a string describing the INI file.
|
public static IniFile of(CharSource source)
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.
source - the INI file resourceUncheckedIOException - if an IO exception occursIllegalArgumentException - if the file cannot be parsedpublic static IniFile of(Map<String,PropertySet> sectionMap)
sectionMap - the map of sectionspublic ImmutableSet<String> sections()
public ImmutableMap<String,PropertySet> asMap()
The iteration order of the map matches that of the original file.
public boolean contains(String name)
name - the section namepublic PropertySet section(String name)
This returns the section associated with the specified name. If the section does not exist an exception is thrown.
name - the section nameIllegalArgumentException - if the section does not existpublic Optional<PropertySet> findSection(String name)
This returns the section associated with the specified name, empty if not present.
name - the section namepublic IniFile combinedWith(IniFile other)
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.
other - the other INI filepublic boolean equals(Object obj)
The comparison checks the content.
public int hashCode()
Copyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.