Class ConfigurationContainer


  • public class ConfigurationContainer
    extends Object
    An object that holds multiple Configuration objects and retrieves configuration data seamlessly, by namespace and key.

    The Configuration objects are sorted by precedence (from highest to lowest). So, in case of key collision, the object with the highest precedence will be selected first. The container may still select other lower-precedence Configuration objects if a key is not found in the highest-precedence Configuration.

    To create a new empty container, use the default constructor new ConfigurationContainer(). To create a new container with preset Configuration objects, pass them as "var-args".

    Use the add(Configuration) method at any time to register new objects inside the container.

    To retrieve Configuration data, use any of the getter methods, specifying a namespace and key.

    Single-argument getter methods can be used to retrieve data from Configuration objects that do not have a declared namespace. These methods may also retrieve data from all Configuration objects if the selected data-fetch strategy is LENIENT.

    Each container may have a custom DataFetchStrategy which may be specified at container construction time and modified using the setter method anytime. If not specified, the container uses the default choice configured via ConfectorySettings.

    IMPORTANT: This class works only with map-based Configuration objects (e.g.: Properties, JSONObject, JsonNode, etc.). In other words, only "container" objects which values can be accessed using either a key or path expression (e.g. JSONPath). POJO-based Configuration objects are not supported inside a the ConfigurationContainer.

    Since:
    0.1.0
    Author:
    oswaldo.bapvic.jr (Oswaldo Junior)
    See Also:
    Configuration, DataFetchStrategy
    • Constructor Detail

      • ConfigurationContainer

        public ConfigurationContainer​(Configuration<?>... configs)
        Builds a new ConfigurationContainer with an arbitrary number of preset Configuration objects to be registered.
        Parameters:
        configs - an arbitrary number of Configuration objects (zero or more) to be registered at constructor time
      • ConfigurationContainer

        public ConfigurationContainer​(DataFetchStrategy dataFetchStrategy,
                                      Configuration<?>... configs)
        Builds a new ConfigurationContainer with a custom DataFetchStrategy and an arbitrary number of preset Configuration objects.
        Parameters:
        dataFetchStrategy - an optional DataFetchStrategy to be applied by this container; null is allowed and indicates that the default strategy defined in Confectory.settings() will be applied
        configs - an arbitrary number of Configuration objects (zero or more) to be registered at constructor time
        See Also:
        ConfectorySettings
    • Method Detail

      • setDataFetchStrategy

        public void setDataFetchStrategy​(DataFetchStrategy strategy)
        Defines a custom DataFetchStrategy for this container.
        Parameters:
        strategy - the DataFetchStrategy to set; not null
        Throws:
        NullPointerException - if the specified strategy is null
      • add

        public void add​(Configuration<?> configuration)
        Adds the specified Configuration to this container.
        Parameters:
        configuration - the Configuration to be added to the container
      • addAll

        public void addAll​(ConfigurationContainer source)
        Copies all of the Configuration objects from another container to this container.
        Parameters:
        source - the source container which Configuration objects are to be stored in this container; null is allowed
      • clear

        public void clear()
        Removes all of the Configuration objects from this container.
      • getBoolean

        public Boolean getBoolean​(String key)
        Returns the Boolean object associated with the specified key in the default namespace (or in all namespaces depending on the DataFetchStrategy in scope).
        Parameters:
        key - the object key (or path)
        Returns:
        the Boolean object associated with the specified key; null if not found
        See Also:
        DataFetchStrategy
      • getBoolean

        public Boolean getBoolean​(String namespace,
                                  String key)
        Returns the Boolean object associated with the specified key in the specified namespace.
        Parameters:
        namespace - the namespace to be used
        key - the object key (or path)
        Returns:
        the Boolean object associated with the specified key; null if not found
      • getInteger

        public Integer getInteger​(String key)
        Returns the Integer object associated with the specified key in the default namespace (or in all namespaces depending on the DataFetchStrategy in scope).
        Parameters:
        key - the object key (or path)
        Returns:
        the Integer object associated with the specified key; null if not found
        See Also:
        DataFetchStrategy
      • getInteger

        public Integer getInteger​(String namespace,
                                  String key)
        Returns the Integer object associated with the specified key in the specified namespace.
        Parameters:
        namespace - the namespace to be used
        key - the object key (or path)
        Returns:
        the Integer object associated with the specified key; null if not found
      • getLong

        public Long getLong​(String key)
        Returns the Long object associated with the specified key in the default namespace (or in all namespaces depending on the DataFetchStrategy in scope).
        Parameters:
        key - the object key (or path)
        Returns:
        the Long object associated with the specified key; null if not found
        See Also:
        DataFetchStrategy
      • getLong

        public Long getLong​(String namespace,
                            String key)
        Returns the Long object associated with the specified key in the specified namespace.
        Parameters:
        namespace - the namespace to be used
        key - the object key (or path)
        Returns:
        the Long object associated with the specified key; null if not found
      • getDouble

        public Double getDouble​(String key)
        Returns the Double object associated with the specified key in the default namespace (or in all namespaces depending on the DataFetchStrategy in scope).
        Parameters:
        key - the object key (or path)
        Returns:
        the Double value associated with the specified key; null if not found
        See Also:
        DataFetchStrategy
      • getDouble

        public Double getDouble​(String namespace,
                                String key)
        Returns the Double object associated with the specified key in the specified namespace.
        Parameters:
        namespace - the namespace to be used
        key - the object key (or path)
        Returns:
        the Double object associated with the specified key; null if not found
      • getString

        public String getString​(String key)
        Returns the String object associated with the specified key in the default namespace (or in all namespaces depending on the DataFetchStrategy in scope).
        Parameters:
        key - the object key (or path)
        Returns:
        the String object associated with the specified key; null if not found
        See Also:
        DataFetchStrategy
      • getString

        public String getString​(String namespace,
                                String key)
        Returns the String object associated with the specified key in the specified namespace.
        Parameters:
        namespace - the namespace to be used
        key - the object key (or path)
        Returns:
        the String object associated with the specified key; null if not found
      • size

        public long size()
        Returns the number of Configuration objects in this container.
        Returns:
        the number of Configuration objects in this container
        Since:
        1.0.0
      • size

        public long size​(String namespace)
        Returns the number of Configuration objects associated with the specified namespace in this container.
        Parameters:
        namespace - the namespace to be tested
        Returns:
        the number of Configuration objects associated with the specified namespace
      • isEmpty

        public boolean isEmpty()
        Returns true if this container contains no Configuration objects.
        Returns:
        true if this container contains no Configuration objects
        Since:
        1.0.0
      • getNamespaces

        public Collection<String> getNamespaces()
        Returns all of the namespaces defined inside this container.
        Returns:
        a set of namespaces