Class Section
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<T> Optional<T>
Shortcut for callingasBean(sectionName, c,
.false )<T> Optional<T>
Converts this config file section to the specified bean instance.<T> Optional<T>
asInterface
(Class<T> c) Wraps this section inside a Java interface so that values in the section can be read and write using getters and setters.asMap()
Returns this section as a map.boolean
Returnstrue if this section exists.writeToBean
(Object bean, boolean ignoreUnknownProperties) Copies the entries in this section to the specified bean by calling the public setters on that bean.
-
Constructor Details
-
Section
Constructor.- Parameters:
config
- The config that this entry belongs to.configMap
- The map that this belongs to.name
- The section name of this entry.
-
-
Method Details
-
isPresent
Returnstrue if this section exists.- Returns:
true if this section exists.
-
asBean
Shortcut for callingasBean(sectionName, c,
.false )- Type Parameters:
T
- The bean class to create.- Parameters:
c
- The bean class to create.- Returns:
- A new bean instance, or
Optional.empty()
if this section does not exist. - Throws:
ParseException
- Malformed input encountered.
-
asBean
Converts this config file section to the specified bean instance.Key/value pairs in the config file section get copied as bean property values to the specified bean class.
Example config file
[MyAddress] name =John Smith street =123 Main Street city =Anywhere state =NY zip =12345 Example bean
public class Address {public Stringname ,street ,city ;public StateEnumstate ;public int zip ; }Example usage
Config
config = Config.create ().name("MyConfig.cfg" ).build(); Addressaddress =config .getSection("MySection" ).asBean(Address.class ).orElse(null );- Type Parameters:
T
- The bean class to create.- Parameters:
c
- The bean class to create.ignoreUnknownProperties
- Iffalse , throws aParseException
if the section contains an entry that isn't a bean property name.- Returns:
- A new bean instance, or
null if this section doesn't exist. - Throws:
ParseException
- Unknown property was encountered in section.
-
asMap
Returns this section as a map.- Returns:
- A new
JsonMap
, orOptional.empty()
if this section doesn't exist.
-
asInterface
Wraps this section inside a Java interface so that values in the section can be read and write using getters and setters.Example config file
[MySection] string =foo int =123 enum =ONE bean ={foo:'bar',baz:123} int3dArray =[[[123,null],null],null] bean1d3dListMap ={key:[[[[{foo:'bar',baz:123}]]]]} Example interface
public interface MyConfigInterface { String getString();void setString(Stringvalue );int getInt();void setInt(int value ); MyEnum getEnum();void setEnum(MyEnumvalue ); MyBean getBean();void setBean(MyBeanvalue );int [][][] getInt3dArray();void setInt3dArray(int [][][]value ); Map<String,List<MyBean[][][]>> getBean1d3dListMap();void setBean1d3dListMap(Map<String,List<MyBean[][][]>>value ); }Example usage
Config
config = Config.create ().name("MyConfig.cfg" ).build(); MyConfigInterfaceci =config .get("MySection" ).asInterface(MyConfigInterface.class ).orElse(null );int myInt =ci .getInt();ci .setBean(new MyBean());ci .save();Notes:
- Calls to setters when the configuration is read-only will cause
UnsupportedOperationException
to be thrown.
- Type Parameters:
T
- The proxy interface class.- Parameters:
c
- The proxy interface class.- Returns:
- The proxy interface.
- Calls to setters when the configuration is read-only will cause
-
writeToBean
Copies the entries in this section to the specified bean by calling the public setters on that bean.- Parameters:
bean
- The bean to set the properties on.ignoreUnknownProperties
- Iftrue , don't throw anIllegalArgumentException
if this section contains a key that doesn't correspond to a setter method.- Returns:
- An object map of the changes made to the bean.
- Throws:
ParseException
- If parser was not set on this config file or invalid properties were found in the section.UnsupportedOperationException
- If configuration is read only.
-