Class PropertySet


  • public final class PropertySet
    extends Object
    A map of key-value properties.

    This class represents a map of key to value. Multiple values may be associated with each key.

    This class is generally created by reading an INI or properties file. See IniFile and PropertiesFile.

    • Method Detail

      • empty

        public static PropertySet empty()
        Obtains an empty property set.

        The result contains no properties.

        Returns:
        an empty property set
      • of

        public static PropertySet of​(Map<String,​String> keyValues)
        Obtains an instance from a map.

        The returned instance will have one value for each key.

        Parameters:
        keyValues - the key-values to create the instance with
        Returns:
        the property set
      • of

        public static PropertySet of​(Multimap<String,​String> keyValues)
        Obtains an instance from a map allowing for multiple values for each key.

        The returned instance may have more than one value for each key.

        Parameters:
        keyValues - the key-values to create the instance with
        Returns:
        the property set
      • stream

        public MapStream<String,​String> stream()
        Returns the keys and values as a MapStream.

        There may be multiple values for the same key. The iteration order of the stream matches that of the input data.

        Returns:
        the map stream
      • keys

        public ImmutableSet<String> keys()
        Returns the set of keys of this property set.

        The iteration order of the map matches that of the input data.

        Returns:
        the set of keys
      • asMultimap

        public ImmutableListMultimap<String,​String> asMultimap()
        Returns the property set as a multimap.

        The iteration order of the map matches that of the input data.

        Returns:
        the key-value map
      • asMap

        public ImmutableMap<String,​String> asMap()
        Returns the property set as a map.

        The iteration order of the map matches that of the input data.

        If a key has multiple values, the values will be returned as a comma separated list.

        Returns:
        the key-value map
      • isEmpty

        public boolean isEmpty()
        Checks if this property set is empty.
        Returns:
        true if the set is empty
      • contains

        public boolean contains​(String key)
        Checks if this property set contains the specified key.
        Parameters:
        key - the key name
        Returns:
        true if the key exists
      • value

        public String value​(String key)
        Gets a single value from this property set.

        This returns the value associated with the specified key.

        If a key has multiple values, the values will be returned as a comma separated list.

        Parameters:
        key - the key name
        Returns:
        the value
        Throws:
        IllegalArgumentException - if the key does not exist
      • findValue

        public Optional<String> findValue​(String key)
        Finds a single value in this property set.

        This returns the value associated with the specified key, empty if not present.

        If a key has multiple values, the values will be returned as a comma separated list.

        Parameters:
        key - the key name
        Returns:
        the value, empty if not found
        Throws:
        IllegalArgumentException - if more than one value is associated
      • valueList

        public ImmutableList<String> valueList​(String key)
        Gets the list of values associated with the specified key.

        A key-values instance may contain multiple values for each key. This method returns that list of values. The iteration order of the map matches that of the input data. The returned list may be empty.

        Parameters:
        key - the key name
        Returns:
        the list of values associated with the key
      • combinedWith

        public PropertySet combinedWith​(PropertySet other)
        Combines this property set with another.

        This property set takes precedence. Where a key exists in both sets the values in the other property set 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 property set
        Returns:
        the combined property set
      • overrideWith

        public PropertySet overrideWith​(PropertySet other)
        Overrides this property set with another.

        The specified property set takes precedence. The order of any existing keys will be retained, with the value replaced. Any order of any additional keys will be retained, with those keys located after the base set of keys.

        Parameters:
        other - the other property set
        Returns:
        the combined property set
      • equals

        public boolean equals​(Object obj)
        Checks if this property set equals another.

        The comparison checks the content.

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

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

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