Class ConfigurationBuilder<T>

  • Type Parameters:
    T - the target configuration type
    All Implemented Interfaces:
    ConfigurationMetadataRetriever<T>

    public class ConfigurationBuilder<T>
    extends Object
    implements ConfigurationMetadataRetriever<T>
    A mutable object that supports the creation of immutable Configuration objects.

    For example:

     Configuration<Properties> config = new ConfigurationBuilder<Properties>()
             .source(new ClasspathFileSource<>("my.properties"))
             .mapper(new PropertiesMapper())
             .namespace("default")
             .precedence(10)
             .lazy()
             .build();
     
    Since:
    0.1.0
    Author:
    oswaldo.bapvic.jr (Oswaldo Junior)
    See Also:
    Configuration
    • Constructor Detail

      • ConfigurationBuilder

        public ConfigurationBuilder()
        Creates a new, empty ConfigurationBuilder.
      • ConfigurationBuilder

        public ConfigurationBuilder​(Configuration<T> sourceConfiguration)
        Creates a new ConfigurationBuilder filled with the attributes of an existing base Configuration.
        Parameters:
        sourceConfiguration - a preset Configuration object whose attributes are to be copied; null is allowed
    • Method Detail

      • namespace

        public ConfigurationBuilder<T> namespace​(String namespace)
        Declares a namespace to be assigned to the new Configuration object.

        Note: The namespace is optional, but usually recommended to organize the scope of the target object and to prevent key collisions.

        Parameters:
        namespace - the namespace to set; null is allowed
        Returns:
        a reference to this same ConfigurationBuilder for chained calls
      • precedence

        public ConfigurationBuilder<T> precedence​(int precedence)
        Defines the level of precedence of the new Configuration compared to similar objects in the same namespace.

        In a common configuration container, the object with the highest precedence level may be selected first in the occurrence of a key collision in the same namespace.

        Note: This precedence value is optional and the default value is 0 (zero).

        Parameters:
        precedence - an integer number representing the order of importance given to the target configuration
        Returns:
        a reference to this same ConfigurationBuilder for chained calls
      • source

        public ConfigurationBuilder<T> source​(Source<T> source)
        Defines the Source of the new Configuration.
        Parameters:
        source - the Source to be set; not null
        Returns:
        a reference to this same ConfigurationBuilder for chained calls
      • source

        public ConfigurationBuilder<T> source​(String path)
        Defines a DynamicSource with the specified path for the new Configuration.
        Parameters:
        path - the path to be set; not null
        Returns:
        a reference to this same ConfigurationBuilder for chained calls
      • mapper

        public ConfigurationBuilder<T> mapper​(Mapper<T> mapper)
        Defines the Mapper of the new Configuration.
        Parameters:
        mapper - the Mapper to be set; not null
        Returns:
        a reference to this same ConfigurationBuilder for chained calls
      • optional

        public ConfigurationBuilder<T> optional()
        Marks the new Configuration as optional.

        The configuration source will be loaded quietly, i.e., not throwing an exception if the source is not found or not loaded successfully.

        Returns:
        a reference to this same ConfigurationBuilder for chained calls
        See Also:
        required()
      • required

        public ConfigurationBuilder<T> required()
        Marks the new Configuration as required (default).
        Returns:
        a reference to this same ConfigurationBuilder for chained calls
        See Also:
        optional()
      • lazy

        public ConfigurationBuilder<T> lazy()
        Marks the new Configuration as lazy.

        The configuration source will not be loaded until needed.

        Returns:
        a reference to this same ConfigurationBuilder for chained calls
        Since:
        0.4.0
        See Also:
        eager()
      • eager

        public ConfigurationBuilder<T> eager()
        Marks the new Configuration as eager.

        The configuration source will loaded directly during build() time (default).

        Returns:
        a reference to this same ConfigurationBuilder for chained calls
        Since:
        0.4.0
        See Also:
        lazy()
      • bean

        public ConfigurationBuilder<T> bean​(T bean)
        [Optional] Defines a preset bean for the new Configuration.
        Parameters:
        bean - the preset bean to be set
        Returns:
        a reference to this same ConfigurationBuilder for chained calls
        Since:
        2.1.0
      • getPrecedence

        public int getPrecedence()
        Description copied from interface: ConfigurationMetadataRetriever
        Returns the precedence value defined for this Configuration object.

        In a common container, objects with higher-precedence may be selected first in case of key collision.

        Specified by:
        getPrecedence in interface ConfigurationMetadataRetriever<T>
        Returns:
        an integer number representing the order of importance given to this Configuration
      • isOptional

        public boolean isOptional()
        Description copied from interface: ConfigurationMetadataRetriever
        Returns a flag indicating whether this Configuration is optional.

        An optional Configuration object may behave quietly in the event of a failure to load the data.

        Specified by:
        isOptional in interface ConfigurationMetadataRetriever<T>
        Returns:
        true if this Configuration setup is optional; false, otherwise
      • getBean

        public T getBean()