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 voidclear()ConfigObjectclone()Returns a shallow copy of this ConfigObject, keys and configuration entries are not cloned.booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)java.util.SetentrySet()java.util.Mapflatten()A ConfigObject is a tree structure consisting of nested maps.java.util.Mapflatten(java.util.Map target)Flattens this ConfigObject populating the results into the target Mapjava.lang.Objectget(java.lang.Object key)java.net.URLgetConfigFile()java.lang.ObjectgetProperty(java.lang.String name)Overrides the default getProperty implementation to create nested ConfigObject instances on demand for non-existent keysbooleanisEmpty()java.lang.BooleanisSet(java.lang.String option)Checks if a config option is set.java.util.SetkeySet()java.util.Mapmerge(ConfigObject other)Merges the given map with this ConfigObject overriding any matching configuration entries in this ConfigObjectjava.lang.StringprettyPrint()java.lang.Objectput(java.lang.Object key, java.lang.Object value)voidputAll(java.util.Map m)java.lang.Objectremove(java.lang.Object key)voidsetConfigFile(java.net.URL configFile)intsize()java.util.PropertiestoProperties()Converts this ConfigObject into a the java.util.Properties format, flattening the tree structure beforehandjava.util.PropertiestoProperties(java.lang.String prefix)Converts this ConfigObject ino the java.util.Properties format, flatten the tree and prefixing all entries with the given prefixjava.lang.StringtoString()java.util.Collectionvalues()java.io.WriterwriteTo(java.io.Writer outArg)Writes this config object into a String serialized representation which can later be parsed back using the parse() methodMethods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClassMethods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface groovy.lang.GroovyObject
invokeMethod, setPropertyMethods 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.IOExceptionWrites this config object into a String serialized representation which can later be parsed back using the parse() method- Specified by:
writeToin interfaceWritable- 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:
getPropertyin interfaceGroovyObject- 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
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:
sizein interfacejava.util.Map
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfacejava.util.Map
-
containsKey
public boolean containsKey(java.lang.Object key)- Specified by:
containsKeyin interfacejava.util.Map
-
containsValue
public boolean containsValue(java.lang.Object value)- Specified by:
containsValuein interfacejava.util.Map
-
get
public java.lang.Object get(java.lang.Object key)- Specified by:
getin interfacejava.util.Map
-
put
public java.lang.Object put(java.lang.Object key, java.lang.Object value)- Specified by:
putin interfacejava.util.Map
-
remove
public java.lang.Object remove(java.lang.Object key)- Specified by:
removein interfacejava.util.Map
-
putAll
public void putAll(java.util.Map m)- Specified by:
putAllin interfacejava.util.Map
-
clear
public void clear()- Specified by:
clearin interfacejava.util.Map
-
keySet
public java.util.Set keySet()- Specified by:
keySetin interfacejava.util.Map
-
values
public java.util.Collection values()- Specified by:
valuesin interfacejava.util.Map
-
entrySet
public java.util.Set entrySet()- Specified by:
entrySetin interfacejava.util.Map
-
clone
Returns a shallow copy of this ConfigObject, keys and configuration entries are not cloned.- Overrides:
clonein classjava.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') == falseThe 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:
trueif the option is setfalseotherwise- Since:
- 2.3.0
-
prettyPrint
public java.lang.String prettyPrint() -
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-