Class Section

java.lang.Object
org.apache.juneau.config.Section

public class Section extends Object
A single section in a config file.
  • Constructor Details

    • Section

      protected Section(Config config, ConfigMap configMap, String name)
      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

      public boolean isPresent()
      Returns true if this section exists.
      Returns:
      true if this section exists.
    • asBean

      public <T> Optional<T> asBean(Class<T> c) throws ParseException
      Shortcut for calling asBean(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

      public <T> Optional<T> asBean(Class<T> c, boolean ignoreUnknownProperties) throws ParseException
      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 String name, street, city; public StateEnum state; public int zip; }

      Example usage

      Config config = Config.create().name("MyConfig.cfg").build(); Address address = config.getSection("MySection").asBean(Address.class).orElse(null);

      Type Parameters:
      T - The bean class to create.
      Parameters:
      c - The bean class to create.
      ignoreUnknownProperties - If false, throws a ParseException 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

      public Optional<JsonMap> asMap()
      Returns this section as a map.
      Returns:
      A new JsonMap, or Optional.empty() if this section doesn't exist.
    • asInterface

      public <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.
      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(String value); int getInt(); void setInt(int value); MyEnum getEnum(); void setEnum(MyEnum value); MyBean getBean(); void setBean(MyBean value); 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(); MyConfigInterface ci = config.get("MySection").asInterface(MyConfigInterface.class).orElse(null); int myInt = ci.getInt(); ci.setBean(new MyBean()); ci.save();

      Notes:
      Type Parameters:
      T - The proxy interface class.
      Parameters:
      c - The proxy interface class.
      Returns:
      The proxy interface.
    • writeToBean

      public Section writeToBean(Object bean, boolean ignoreUnknownProperties) throws ParseException
      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 - If true, don't throw an IllegalArgumentException 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.