Interface Config

All Known Subinterfaces:
Config

public interface Config
Immutable tree-structured configuration.

See ConfigValue.

  • Method Details

    • empty

      static Config empty()
      Empty instance of Config.
      Returns:
      empty instance of Config.
    • key

      Config.Key key()
      Returns the fully-qualified key of the Config node.

      The fully-qualified key is a sequence of tokens derived from the name of each node along the path from the config root to the current node. Tokens are separated by . (the dot character). See name() for more information on the format of each token.

      Returns:
      current config node key
      See Also:
    • name

      default String name()
      Returns the last token of the fully-qualified key for the Config node.

      The name of a node is the last token in its fully-qualified key.

      The exact format of the name depends on the Type of the containing node:

      • from a Type#OBJECT node the token for a child is the name of the object member;
      • from a Type#LIST node the token for a child is a zero-based index of the element, an unsigned base-10 integer value with no leading zeros.

      The ABNF syntax of config key is:

      
       config-key = *1( key-token *( "." key-token ) )
        key-token = *( unescaped / escaped )
        unescaped = %x00-2D / %x2F-7D / %x7F-10FFFF
                  ; %x2E ('.') and %x7E ('~') are excluded from 'unescaped'
          escaped = "~" ( "0" / "1" )
                  ; representing '~' and '.', respectively
       
      Returns:
      current config node key
      See Also:
    • get

      Config get(String key) throws ConfigException
      Returns the single sub-node for the specified sub-key.

      The format of the key is described on key() method.

      Parameters:
      key - sub-key of requested sub-node
      Returns:
      config node for specified sub-key, never returns null.
      Throws:
      ConfigException - if not defined
      See Also:
    • root

      Config root()
      Get the root of the configuration tree. In case this node is part of detached tree, this method returns the node that was detached.
      Returns:
      root of this configuration tree
    • get

      default Config get(Config.Key key)
      Returns the single sub-node for the specified sub-key.
      Parameters:
      key - sub-key of requested sub-node
      Returns:
      config node for specified sub-key, never returns null.
      See Also:
    • detach

      Config detach() throws ConfigException
      Returns a copy of the Config node with no parent.

      The returned node acts as a root node for the subtree below it. Its key is the empty string; "". The original config node is unchanged, and the original and the copy point to the same children.

      Consider the following configuration:

       app:
            name: Example 1
            page-size: 20
       logging:
            app.level = INFO
            level = WARNING
       
      The Config instances name1 and name2 represents same data and in fact refer to the same object:
       Config name1 = config
                        .get("app")
                        .get("name");
       Config name2 = config
                        .get("app")
                        .detach()               //DETACHED node
                        .get("name");
      
       assert name1.asString() == "Example 1";
       assert name2.asString() == "Example 1";  //DETACHED node
       
      The only difference is the key each node returns:
       assert name1.key() == "app.name";
       assert name2.key() == "name";            //DETACHED node
       

      See asMap() for example of config detaching.

      Returns:
      returns detached Config instance of same config node
      Throws:
      ConfigException - if not defined
    • exists

      boolean exists()
      Returns true if the node exists, whether an object, a list, a value node, etc.
      Returns:
      true if the node exists
    • isLeaf

      boolean isLeaf()
      Returns true if this node exists and is a leaf node (has no children).

      A leaf node has no nested configuration subtree and has a single value.

      Returns:
      true if the node is existing leaf node, false otherwise.
    • isObject

      boolean isObject()
      Returns true if this node exists and is Type#Object.
      Returns:
      true if the node exists and is Type#Object, false otherwise.
    • isList

      boolean isList()
      Returns true if this node exists and is Type#List.
      Returns:
      true if the node exists and is Type#List, false otherwise.
    • hasValue

      boolean hasValue()
      Returns true if this configuration node has a direct value.

      This may be a value node (e.g. a leaf) or object node or a list node (e.g. a branch with value). The application can invoke methods such as as(Class) on nodes that have value.

      Returns:
      true if the node has direct value, false otherwise.
    • as

      <T> ConfigValue<T> as(Class<T> type)
      Typed value as a ConfigValue.
      Type Parameters:
      T - type
      Parameters:
      type - type class
      Returns:
      typed value
      See Also:
    • map

      <T> ConfigValue<T> map(Function<Config,T> mapper)
      Typed value as a ConfigValue created from factory method. To convert from String, you can use config.asString().as(Function).
      Type Parameters:
      T - type
      Parameters:
      mapper - method to create an instance from config
      Returns:
      typed value
    • asList

      <T> ConfigValue<List<T>> asList(Class<T> type) throws ConfigException
      Returns list of specified type.
      Type Parameters:
      T - type of list elements
      Parameters:
      type - type class
      Returns:
      a typed list with values
      Throws:
      ConfigException - in case of problem to map property value.
    • mapList

      <T> ConfigValue<List<T>> mapList(Function<Config,T> mapper) throws ConfigException
      Returns this node as a list mapping each list value using the provided mapper.
      Type Parameters:
      T - type of list elements
      Parameters:
      mapper - mapper to convert each list node into a typed value
      Returns:
      a typed list with values
      Throws:
      ConfigException - in case the mapper fails to map the values
    • asNodeList

      <C extends Config> ConfigValue<List<C>> asNodeList() throws ConfigException
      Returns a list of child Config nodes if the node is Type#OBJECT. Returns a list of element nodes if the node is Type#LIST. Throws MissingValueException if the node is Type#MISSING. Otherwise, if node is Type#VALUE, it throws ConfigMappingException.
      Type Parameters:
      C - the common config derived type
      Returns:
      a list of Type#OBJECT members or a list of Type#LIST members
      Throws:
      ConfigException - in case the node is Type#VALUE
    • asMap

      Transform all leaf nodes (values) into Map instance.
      Returns:
      new Map instance that contains all config leaf node values
      Throws:
      ConfigException - in case the node is Type#MISSING.
    • asNode

      default ConfigValue<? extends Config> asNode()
      Returns existing current config node as ConfigValue.
      Returns:
      current config node as ConfigValue
    • asString

      default ConfigValue<String> asString()
      String typed value.
      Returns:
      typed value
    • asBoolean

      default ConfigValue<Boolean> asBoolean()
      Boolean typed value.
      Returns:
      typed value
    • asInt

      default ConfigValue<Integer> asInt()
      Integer typed value.
      Returns:
      typed value
    • asLong

      default ConfigValue<Long> asLong()
      Long typed value.
      Returns:
      typed value
    • asDouble

      default ConfigValue<Double> asDouble()
      Double typed value.
      Returns:
      typed value