Class MergedConfig

java.lang.Object
io.helidon.config.MergedConfig
All Implemented Interfaces:
Config, Config

public class MergedConfig extends Object implements Config
Configuration that merges two Config instances, primary and fallback. When a property is not present in the primary configuration, fallback is used.

When a key is present in both the primary and fallback configurations, the value from the primary configuration takes precedence.

  • Method Details

    • create

      public static Config create(Config primary, Config fallback)
      Creates a new MergedConfig that merges the specified configurations.
      Parameters:
      primary - the primary configuration, which has precedence in the case of duplicate keys
      fallback - the fallback configuration, queried for keys not found in the primary one
      Returns:
      a merged Config instance
    • timestamp

      public Instant timestamp()
      Description copied from interface: Config
      Returns when the configuration tree was created.

      Each config node of single Config tree returns same timestamp.

      Specified by:
      timestamp in interface Config
      Returns:
      timestamp of created instance of whole configuration tree.
      See Also:
    • key

      public Config.Key key()
      Description copied from interface: Config
      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 Config.name() for more information on the format of each token.

      Specified by:
      key in interface Config
      Specified by:
      key in interface Config
      Returns:
      current config node key
      See Also:
    • root

      public Config root()
      Description copied from interface: Config
      Get the root of the configuration tree. In case this node is part of detached tree, this method returns the node that was detached.
      Specified by:
      root in interface Config
      Specified by:
      root in interface Config
      Returns:
      root of this configuration tree
    • get

      public Config get(Config.Key key)
      Description copied from interface: Config
      Returns the single sub-node for the specified sub-key.
      Specified by:
      get in interface Config
      Parameters:
      key - sub-key of requested sub-node
      Returns:
      config node for specified sub-key, never returns null.
      See Also:
    • detach

      public Config detach()
      Description copied from interface: Config
      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 Config.asMap() for example of config detaching.

      Specified by:
      detach in interface Config
      Specified by:
      detach in interface Config
      Returns:
      returns detached Config instance of same config node
    • type

      public Config.Type type()
      Description copied from interface: Config
      Provides the Config.Type of the Config node.
      Specified by:
      type in interface Config
      Returns:
      the Type of the configuration node
    • hasValue

      public boolean hasValue()
      Description copied from interface: Config
      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 Config.as(Class) on nodes that have value.

      Specified by:
      hasValue in interface Config
      Specified by:
      hasValue in interface Config
      Returns:
      true if the node has direct value, false otherwise.
    • traverse

      public Stream<Config> traverse(Predicate<Config> predicate)
      Traverses over merged configs like Config.traverse(). Every config node exising in both primary and fallback configs is merged.
      Specified by:
      traverse in interface Config
      Parameters:
      predicate - predicate evaluated on each visited Config node to continue or stop visiting the node
      Returns:
      stream of deepening depth-first sub nodes
    • convert

      public <T> T convert(Class<T> type, String value) throws ConfigMappingException
      Description copied from interface: Config
      Convert a String to a specific type. This is a helper method to allow for processing of default values that cannot be typed (e.g. in annotations).
      Specified by:
      convert in interface Config
      Type Parameters:
      T - type
      Parameters:
      type - type of the property
      value - String value
      Returns:
      instance of the correct type
      Throws:
      ConfigMappingException - in case the String provided cannot be converted to the type expected
      See Also:
    • mapper

      public ConfigMapper mapper()
      Only mappers from the primary config are considered.
      Specified by:
      mapper in interface Config
      Returns:
      mapper from primary config
    • as

      public <T> ConfigValue<T> as(GenericType<T> genericType)
      Description copied from interface: Config
      Typed value as a ConfigValue for a generic type. If appropriate mapper exists, returns a properly typed generic instance.

      Example:

       
       ConfigValue<Map<String, Integer>> myMapValue = config.as(new GenericType<Map<String, Integer>>(){});
       myMapValue.ifPresent(map -> {
            Integer port = map.get("service.port");
        }
       
       
      Specified by:
      as in interface Config
      Type Parameters:
      T - type of the returned value
      Parameters:
      genericType - a (usually anonymous) instance of generic type to prevent type erasure
      Returns:
      properly typed config value
    • as

      public <T> ConfigValue<T> as(Class<T> type)
      Description copied from interface: Config
      Typed value as a ConfigValue.
      Specified by:
      as in interface Config
      Specified by:
      as in interface Config
      Type Parameters:
      T - type
      Parameters:
      type - type class
      Returns:
      typed value
      See Also:
    • as

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

      public <T> ConfigValue<List<T>> asList(Class<T> type) throws ConfigMappingException
      Description copied from interface: Config
      Returns list of specified type.
      Specified by:
      asList in interface Config
      Specified by:
      asList in interface Config
      Type Parameters:
      T - type of list elements
      Parameters:
      type - type class
      Returns:
      a typed list with values
      Throws:
      ConfigMappingException - in case of problem to map property value.
    • asList

      public <T> ConfigValue<List<T>> asList(Function<Config,T> mapper) throws ConfigMappingException
      Description copied from interface: Config
      Returns this node as a list converting each list value using the provided mapper.
      Specified by:
      asList in interface Config
      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:
      ConfigMappingException - in case the mapper fails to map the values
    • asNodeList

      public ConfigValue<List<Config>> asNodeList() throws ConfigMappingException
      Description copied from interface: Config
      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.
      Specified by:
      asNodeList in interface Config
      Specified by:
      asNodeList in interface Config
      Returns:
      a list of Type#OBJECT members or a list of Type#LIST members
      Throws:
      ConfigMappingException
    • asMap

      Description copied from interface: Config
      Transform all leaf nodes (values) into Map instance.

      Fully qualified key of config node is used as a key in returned Map. Detach config node before transforming to Map in case you want to cut current Config node key prefix.

      Let's say we work with following configuration:

       app:
            name: Example 1
            page-size: 20
       logging:
            app.level = INFO
            level = WARNING
       
      Map app1 contains two keys: app.name, app.page-size.
      
       Map<String, String> app1 = config.get("app").asMap();
       
      Detaching app config node returns new Config instance with "reset" local root.
      
       Map<String, String> app2 = config.get("app").detach().asMap();
       
      Map app2 contains two keys without app prefix: name, page-size.
      Specified by:
      asMap in interface Config
      Specified by:
      asMap in interface Config
      Returns:
      new Map instance that contains all config leaf node values
      Throws:
      MissingValueException - in case the node is Config.Type.MISSING.
      See Also: