Package groovy.util

Class ConfigObject

java.lang.Object
groovy.lang.GroovyObjectSupport
groovy.util.ConfigObject
All Implemented Interfaces:
GroovyObject, Writable, java.lang.Cloneable, java.util.Map

public class ConfigObject
extends GroovyObjectSupport
implements Writable, java.util.Map, java.lang.Cloneable
A ConfigObject at a simple level is a Map that creates configuration entries (other ConfigObjects) when referencing them. This means that navigating to foo.bar.stuff will not return null but nested ConfigObjects which are of course empty maps The Groovy truth can be used to check for the existence of "real" entries.
Since:
1.5
  • Nested Class Summary

    Nested classes/interfaces inherited from interface java.util.Map

    java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
  • Constructor Summary

    Constructors
    Constructor Description
    ConfigObject()  
    ConfigObject​(java.net.URL file)  
  • Method Summary

    Modifier and Type Method Description
    void clear()  
    ConfigObject clone()
    Returns a shallow copy of this ConfigObject, keys and configuration entries are not cloned.
    boolean containsKey​(java.lang.Object key)  
    boolean containsValue​(java.lang.Object value)  
    java.util.Set entrySet()  
    java.util.Map flatten()
    A ConfigObject is a tree structure consisting of nested maps.
    java.util.Map flatten​(java.util.Map target)
    Flattens this ConfigObject populating the results into the target Map
    java.lang.Object get​(java.lang.Object key)  
    java.net.URL getConfigFile()  
    java.lang.Object getProperty​(java.lang.String name)
    Overrides the default getProperty implementation to create nested ConfigObject instances on demand for non-existent keys
    boolean isEmpty()  
    java.lang.Boolean isSet​(java.lang.String option)
    Checks if a config option is set.
    java.util.Set keySet()  
    java.util.Map merge​(ConfigObject other)
    Merges the given map with this ConfigObject overriding any matching configuration entries in this ConfigObject
    java.lang.String prettyPrint()  
    java.lang.Object put​(java.lang.Object key, java.lang.Object value)  
    void putAll​(java.util.Map m)  
    java.lang.Object remove​(java.lang.Object key)  
    void setConfigFile​(java.net.URL configFile)  
    int size()  
    java.util.Properties toProperties()
    Converts this ConfigObject into a the java.util.Properties format, flattening the tree structure beforehand
    java.util.Properties toProperties​(java.lang.String prefix)
    Converts this ConfigObject ino the java.util.Properties format, flatten the tree and prefixing all entries with the given prefix
    java.lang.String toString()  
    java.util.Collection values()  
    java.io.Writer writeTo​(java.io.Writer outArg)
    Writes this config object into a String serialized representation which can later be parsed back using the parse() method

    Methods inherited from class groovy.lang.GroovyObjectSupport

    getMetaClass, setMetaClass

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface groovy.lang.GroovyObject

    invokeMethod, setProperty

    Methods inherited from interface java.util.Map

    compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
  • Constructor Details

    • ConfigObject

      public ConfigObject​(java.net.URL file)
    • ConfigObject

      public ConfigObject()
  • Method Details

    • getConfigFile

      public java.net.URL getConfigFile()
    • setConfigFile

      public void setConfigFile​(java.net.URL configFile)
    • writeTo

      public java.io.Writer writeTo​(java.io.Writer outArg) throws java.io.IOException
      Writes this config object into a String serialized representation which can later be parsed back using the parse() method
      Specified by:
      writeTo in interface Writable
      Parameters:
      outArg - the Writer to which this Writable should output its data.
      Returns:
      the Writer that was passed
      Throws:
      java.io.IOException - if an error occurred while outputting data to the writer
      See Also:
      Writable.writeTo(java.io.Writer)
    • getProperty

      public java.lang.Object getProperty​(java.lang.String name)
      Overrides the default getProperty implementation to create nested ConfigObject instances on demand for non-existent keys
      Specified by:
      getProperty in interface GroovyObject
      Parameters:
      name - the name of the property of interest
      Returns:
      the given property
    • flatten

      public java.util.Map flatten()
      A ConfigObject is a tree structure consisting of nested maps. This flattens the maps into a single level structure like a properties file
    • flatten

      public java.util.Map flatten​(java.util.Map target)
      Flattens this ConfigObject populating the results into the target Map
      See Also:
      flatten()
    • merge

      public java.util.Map merge​(ConfigObject other)
      Merges the given map with this ConfigObject overriding any matching configuration entries in this ConfigObject
      Parameters:
      other - The ConfigObject to merge with
      Returns:
      The result of the merge
    • toProperties

      public java.util.Properties toProperties()
      Converts this ConfigObject into a the java.util.Properties format, flattening the tree structure beforehand
      Returns:
      A java.util.Properties instance
    • toProperties

      public java.util.Properties toProperties​(java.lang.String prefix)
      Converts this ConfigObject ino the java.util.Properties format, flatten the tree and prefixing all entries with the given prefix
      Parameters:
      prefix - The prefix to append before property entries
      Returns:
      A java.util.Properties instance
    • size

      public int size()
      Specified by:
      size in interface java.util.Map
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface java.util.Map
    • containsKey

      public boolean containsKey​(java.lang.Object key)
      Specified by:
      containsKey in interface java.util.Map
    • containsValue

      public boolean containsValue​(java.lang.Object value)
      Specified by:
      containsValue in interface java.util.Map
    • get

      public java.lang.Object get​(java.lang.Object key)
      Specified by:
      get in interface java.util.Map
    • put

      public java.lang.Object put​(java.lang.Object key, java.lang.Object value)
      Specified by:
      put in interface java.util.Map
    • remove

      public java.lang.Object remove​(java.lang.Object key)
      Specified by:
      remove in interface java.util.Map
    • putAll

      public void putAll​(java.util.Map m)
      Specified by:
      putAll in interface java.util.Map
    • clear

      public void clear()
      Specified by:
      clear in interface java.util.Map
    • keySet

      public java.util.Set keySet()
      Specified by:
      keySet in interface java.util.Map
    • values

      public java.util.Collection values()
      Specified by:
      values in interface java.util.Map
    • entrySet

      public java.util.Set entrySet()
      Specified by:
      entrySet in interface java.util.Map
    • clone

      public ConfigObject clone()
      Returns a shallow copy of this ConfigObject, keys and configuration entries are not cloned.
      Overrides:
      clone in class java.lang.Object
      Returns:
      a shallow copy of this ConfigObject
    • isSet

      public java.lang.Boolean isSet​(java.lang.String option)
      Checks if a config option is set. Example usage:
       def config = new ConfigSlurper().parse("foo { password='' }")
       assert config.foo.isSet('password')
       assert config.foo.isSet('username') == false
       
      The check works only for options one block below the current block. E.g. config.isSet('foo.password') will always return false.
      Parameters:
      option - The name of the option
      Returns:
      true if the option is set false otherwise
      Since:
      2.3.0
    • prettyPrint

      public java.lang.String prettyPrint()
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object