public class Config extends Object
One may wonder why to add extra processing and not use directly XML files. One reason would be XML has heavy dependencies, and this matter on embedded devices. The second is configuration object is not loaded only from XML. It can be loaded from Java properties files; in fact there is no formal requirements for specific format. This class can be used to unify varied configuration sources.
Configuration object resemble XML element. Its name is element tag name and attributes are mapped directly. Value is
element text content and parent and children fields are similar and use for the same purpose: describe the tree
structure. The only specialized field is properties that are stored in a standard Properties
object.
<emails repository="/srv/emails" files-pattern="*.html"> <property name="mail.transport.protocol" value="smtp" /> <property name="mail.debug" value="true" /> </emails>Above XML fragment describe an configuration object with name
emails
, two attributes
repository
and files-pattern
and two properties, mail.transport.protocol
and
mail.debug
.
Configuration object is not reusable and is not thread safe. Anyway, if a configurable instance does not alter state using setters, configuration object still can be used in a concurrent context.
Modifier and Type | Field and Description |
---|---|
private Map<String,String> |
attributes
Optional attributes list, possible empty.
|
private List<Config> |
children
Children list, possible empty.
|
private Converter |
converter
Converter for objects conversion to / from strings.
|
private String |
name
Configuration object name.
|
private Config |
parent
Parent reference, null if configuration object is root.
|
private Properties |
properties
Optional properties, possible empty.
|
private Set<String> |
usedProperties
Keep track of used properties.
|
private String |
value
Configuration object value.
|
Constructor and Description |
---|
Config(String name)
Create configuration object with name.
|
Modifier and Type | Method and Description |
---|---|
void |
addChild(Config child)
Add child configuration object.
|
void |
dump()
Dump configuration object tree to standard out, for debugging purposes.
|
List<Config> |
findChildren(String... name)
Find configuration object children with requested names.
|
String |
getAttribute(String name)
Get attribute value or null if there is no attribute with requested name.
|
<T> T |
getAttribute(String name,
Class<T> type)
Get attribute value converted to requested type or null if there is no attribute with requested name.
|
<T> T |
getAttribute(String name,
Class<T> type,
T defaultValue)
Get attribute value converted to requested type or default value if there is no attribute with requested name.
|
String |
getAttribute(String name,
String defaultValue)
Get attribute value or given default value if there is no attribute with requested name.
|
Config |
getChild(int index)
Get configuration object child by index.
|
Config |
getChild(String name)
Get configuration object first child with requested name or null if there is no child with given name.
|
List<Config> |
getChildren()
Get configuration object children list.
|
int |
getChildrenCount()
Get configuration object children count.
|
String |
getChildValue(String name)
Get named child string value or null if child not found or it has no value.
|
String |
getName()
Get configuration object name.
|
Config |
getParent()
Get configuration object parent.
|
Properties |
getProperties()
Get configuration object Java raw properties.
|
String |
getProperty(String name)
Get configuration object property value or null if there is no property with requested name.
|
<T> T |
getProperty(String name,
Class<T> type)
Get configuration object property converter to requested type or null if there is no property with given name.
|
<T> T |
getProperty(String name,
Class<T> type,
T defaultValue)
Get configuration object property converter to requested type or default value if there is no property with given
name.
|
String |
getProperty(String name,
String defaultValue)
Get configuration object property value or default value if there is no property with requested name.
|
Config |
getRoot()
Get root of the tree this configuration object is part of.
|
Properties |
getUnusedProperties()
Get properties not yet used at the moment this method is executed.
|
String |
getValue()
Get this configuration object string value, possible null.
|
<T> T |
getValue(Class<T> type)
Get this configuration object value converted to requested type.
|
boolean |
hasAttribute(String name)
Test if configuration object has an attribute with requested name.
|
boolean |
hasAttribute(String name,
String value)
Test if configuration object has an attribute with requested name and value.
|
boolean |
hasChild(String name)
Test if configuration object has at least a child with requested name.
|
boolean |
hasChildren()
Test if configuration object has children.
|
boolean |
hasProperty(String name)
Test if configuration object has named property.
|
private void |
print(Config config,
int indent)
Recursively print configuration object tree to standard out.
|
void |
setAttribute(String name,
String value)
Set configuration object attribute.
|
void |
setProperties(Properties properties)
Set this configuration object properties.
|
void |
setProperty(String name,
Object value)
Set configuration object property.
|
void |
setProperty(String name,
String value)
Set configuration object string property.
|
void |
setValue(Object value)
Set this configuration object value.
|
String |
toString() |
private final String name
private String value
private Config parent
private final List<Config> children
private final Map<String,String> attributes
private Properties properties
private Set<String> usedProperties
getProperty(String)
and tested by
getUnusedProperties()
.private final Converter converter
public Config(String name)
name
- configuration object name.IllegalArgumentException
- if name
argument is null or empty.public void addChild(Config child)
child
- child configuration object.IllegalArgumentException
- if child
argument is null.public void setAttribute(String name, String value)
name
- attribute name,value
- attribute value, null ignored.IllegalArgumentException
- if name
argument is null or empty.IllegalArgumentException
- if value
argument is empty.public void setProperties(Properties properties)
setProperty(String, String)
and setProperty(String, Object)
are lost.properties
- properties.public void setProperty(String name, String value)
value
argument is null this setter does nothing.name
- property name,value
- property value, null ignored.IllegalArgumentException
- if name
argument is null or empty.IllegalArgumentException
- if value
argument is empty.public void setProperty(String name, Object value)
value
to string and set property. If property already
exists overwrite old value. If value
argument is null this setter does nothing.name
- property name,value
- property value, null ignored.IllegalArgumentException
- if name
argument is null or empty.ConverterException
- if there is no converter registered for value type.public void setValue(Object value)
value
argument
is null this setter will reset this configuration object value.value
- value to set, null accepted.IllegalArgumentException
- if value
argument is an empty string.ConverterException
- if there is no converter registered for value type.public Config getRoot()
public Config getParent()
public String getName()
public boolean hasAttribute(String name)
name
- attribute name to search for.IllegalArgumentException
- if name
argument is null or empty.public boolean hasAttribute(String name, String value)
name
- name of the attribute to search for,value
- attribute value.IllegalArgumentException
- if name
argument is null or empty.IllegalArgumentException
- if value
argument is null or empty.public String getAttribute(String name)
name
- attribute name.IllegalArgumentException
- if name
argument is null or empty.public String getAttribute(String name, String defaultValue)
name
- attribute name.defaultValue
- default value, null or empty accepted.IllegalArgumentException
- if name
argument is null or empty.public <T> T getAttribute(String name, Class<T> type)
T
- type to convert attribute value.name
- attribute name,type
- type to converter attribute value to.IllegalArgumentException
- if name
argument is null or empty.IllegalArgumentException
- if type
argument is null.ConverterException
- if there is no converter registered for value type or value parse fails.public <T> T getAttribute(String name, Class<T> type, T defaultValue)
T
- type to convert attribute value to.name
- attribute name,type
- type to converter attribute value to,defaultValue
- default value.IllegalArgumentException
- if name
argument is null or empty.IllegalArgumentException
- if type
argument is null.ConverterException
- if there is no converter registered for value type or value parse fails.public Properties getProperties()
public Properties getUnusedProperties()
getProperty(String)
or related. This method returns a new properties instance. Changes on returned
instance does not affect configuration object internal properties.public boolean hasProperty(String name)
name
- property name.IllegalArgumentException
- if name
argument is null or empty.public String getProperty(String name)
name
- property name.IllegalArgumentException
- if name
argument is null or empty.public String getProperty(String name, String defaultValue)
name
- property name,defaultValue
- default value.IllegalArgumentException
- if name
argument is null or empty.public <T> T getProperty(String name, Class<T> type)
T
- value type.name
- property name.type
- type to convert property value to.IllegalArgumentException
- if name
argument is null or empty.IllegalArgumentException
- if type
argument is null.ConverterException
- if there is no converter registered for value type or value parse fails.public <T> T getProperty(String name, Class<T> type, T defaultValue)
T
- value type.name
- property name.type
- type to convert property value to,defaultValue
- default value, possible null or empty.IllegalArgumentException
- if name
argument is null or empty.IllegalArgumentException
- if type
argument is null.ConverterException
- if there is no converter registered for value type or value parse fails.public boolean hasChildren()
public int getChildrenCount()
public List<Config> getChildren()
public boolean hasChild(String name)
name
- child name.IllegalArgumentException
- if name
argument is null or empty.public Config getChild(String name)
name
- child name.IllegalArgumentException
- if name
argument is null or empty.public Config getChild(int index)
index
- child index.IllegalArgumentException
- if index
argument is not in range.public List<Config> findChildren(String... name)
name
- one or more child names.IllegalArgumentException
- if name
list does not contains at least one item.public String getValue()
value
public <T> T getValue(Class<T> type)
T
- value type.type
- desired value object type.IllegalArgumentException
- if type
argument is null.ConverterException
- if there is no converter registered for value type or value parse fails.public String getChildValue(String name)
name
- name of the child to retrieve value from.public void dump()
private void print(Config config, int indent)
config
- configuration object,indent
- indentation index.Copyright © 2018. All rights reserved.