Class Configurer<T>

  • Type Parameters:
    T - the type of the value being configured

    public class Configurer<T>
    extends java.lang.Object
    A fluent configuration utility for applying conditional operations on a value of type T.

    The Configurer class provides a builder-style API to configure and manipulate values, with support for comparison, predicate checks, type conversion, and application of consumer actions. It is designed to be non-thread-safe and intended for single-thread usage.

    Key Features

    • Value initialization with static factory methods.
    • Conditional comparison to detect changes in value.
    • Predicate-based filtering to control flow based on the current value.
    • Type conversion using functional interfaces.
    • Consumer application to perform side effects on the final value.
    • Detailed logging of each operation for traceability.

    Example Usage

    Basic Configuration Flow

    
     Configurer.configure("timeout", 30)
               .compare(25)
               .on(value -> value > 0)
               .as(value -> value * 1000)
               .apply(value -> System.setProperty("timeout.ms", String.valueOf(value)));
     

    Chaining Multiple Operations

    
     Configurer.configure(() -> System.getenv("DEBUG_MODE"))
               .on(mode -> "true".equalsIgnoreCase(mode))
               .as(Boolean::valueOf)
               .apply(enabled -> logger.info("Debug mode is enabled: {}", enabled));
     

    Discarding or Filtering Values

    
     Configurer.configure("username", () -> System.getProperty("user.name"))
               .on(name -> name.length() > 3)
               .apply(name -> System.out.println("Welcome, " + name));
     

    In the above example, if the username length is less than or equal to 3, the value will be discarded, and no action will be taken.

    Since:
    1.0.0
    Author:
    Mercy
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Configurer​(java.lang.String name)
      Constructs a Configurer with the specified name and no initial value.
      protected Configurer​(java.lang.String name, java.util.function.Supplier<T> valueSupplier)
      Constructs a Configurer with the specified name and a value eagerly resolved from the given Supplier.
      protected Configurer​(java.lang.String name, T value)
      Constructs a Configurer with the specified name and initial value.
      protected Configurer​(java.util.function.Supplier<T> valueSupplier)
      Constructs a Configurer with an unnamed configuration entry whose value is eagerly resolved from the given Supplier.
      protected Configurer​(T value)
      Constructs a Configurer with an unnamed configuration entry and the given value.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void apply​(java.util.function.Consumer<T> valueConsumer)
      Terminal operation that applies the given Consumer to the current value if it is non-null.
      <R> Configurer<R> as​(java.util.function.Function<T,​R> function)
      Converts the current value to a new type using the given Function.
      Configurer<T> compare​(java.util.function.Supplier<T> comparedValueSupplier)
      Compares the current value with a value resolved from the given Supplier.
      Configurer<T> compare​(T comparedValue)
      Compares the current value with the given value.
      static <T> Configurer<T> configure​(java.lang.String name)
      Creates a new Configurer with the specified name and no initial value.
      static <T> Configurer<T> configure​(java.lang.String name, java.util.function.Supplier<T> valueSupplier)
      Creates a new Configurer with the specified name and a value eagerly resolved from the given Supplier.
      static <T> Configurer<T> configure​(java.lang.String name, T value)
      Creates a new Configurer with the specified name and initial value.
      static <T> Configurer<T> configure​(java.util.function.Supplier<T> valueSupplier)
      Creates a new unnamed Configurer with a value eagerly resolved from the given Supplier.
      static <T> Configurer<T> configure​(T value)
      Creates a new unnamed Configurer with the specified value.
      Configurer<T> on​(java.util.function.Predicate<? super T> predicate)
      Filters the current value using the given Predicate.
      <T> Configurer<T> value​(java.util.function.Supplier<T> valueSupplier)
      Creates a new Configurer with the same name and a value eagerly resolved from the given Supplier, replacing the current configuration value.
      <T> Configurer<T> value​(T value)
      Creates a new Configurer with the same name and the specified value, replacing the current configuration value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Configurer

        protected Configurer​(java.lang.String name)
        Constructs a Configurer with the specified name and no initial value. The value can be set later using value(Object) or value(Supplier).

        Example Usage

        
           Configurer<Integer> configurer = new Configurer<>("timeout");
           configurer.value(30).apply(val -> System.out.println("Timeout: " + val));
         
        Parameters:
        name - the descriptive name of this configuration entry
        Since:
        1.0.0
      • Configurer

        protected Configurer​(java.util.function.Supplier<T> valueSupplier)
        Constructs a Configurer with an unnamed configuration entry whose value is eagerly resolved from the given Supplier.

        Example Usage

        
           Configurer<String> configurer = new Configurer<>(() -> System.getenv("SERVICE_URL"));
           configurer.on(Objects::nonNull).apply(url -> System.out.println("URL: " + url));
         
        Parameters:
        valueSupplier - a supplier providing the initial configuration value
        Since:
        1.0.0
      • Configurer

        protected Configurer​(T value)
        Constructs a Configurer with an unnamed configuration entry and the given value.

        Example Usage

        
           Configurer<Integer> configurer = new Configurer<>(5000);
           configurer.on(val -> val > 0).apply(val -> System.out.println("Timeout ms: " + val));
         
        Parameters:
        value - the initial configuration value
        Since:
        1.0.0
      • Configurer

        protected Configurer​(java.lang.String name,
                             java.util.function.Supplier<T> valueSupplier)
        Constructs a Configurer with the specified name and a value eagerly resolved from the given Supplier.

        Example Usage

        
           Configurer<String> configurer = new Configurer<>("db.host", () -> System.getProperty("db.host"));
           configurer.on(Objects::nonNull).apply(host -> connectTo(host));
         
        Parameters:
        name - the descriptive name of this configuration entry
        valueSupplier - a supplier providing the initial configuration value
        Since:
        1.0.0
      • Configurer

        protected Configurer​(java.lang.String name,
                             T value)
        Constructs a Configurer with the specified name and initial value. This is the primary constructor that initializes the internal log builder.

        Example Usage

        
           Configurer<Integer> configurer = new Configurer<>("retryCount", 3);
           configurer.on(val -> val >= 0)
                     .apply(val -> System.out.println("Retries set to: " + val));
         
        Parameters:
        name - the descriptive name of this configuration entry
        value - the initial configuration value
        Since:
        1.0.0
    • Method Detail

      • value

        public <T> Configurer<T> value​(T value)
        Creates a new Configurer with the same name and the specified value, replacing the current configuration value.

        Example Usage

        
           Configurer.configure("timeout")
                     .value(30)
                     .apply(val -> System.out.println("Timeout: " + val));
         
        Type Parameters:
        T - the type of the new value
        Parameters:
        value - the new configuration value
        Returns:
        a new Configurer instance with the given value
        Since:
        1.0.0
      • value

        public <T> Configurer<T> value​(java.util.function.Supplier<T> valueSupplier)
        Creates a new Configurer with the same name and a value eagerly resolved from the given Supplier, replacing the current configuration value.

        Example Usage

        
           Configurer.configure("serviceUrl")
                     .value(() -> System.getProperty("service.url", "http://localhost:8080"))
                     .apply(url -> System.out.println("Service URL: " + url));
         
        Type Parameters:
        T - the type of the new value
        Parameters:
        valueSupplier - a supplier providing the new configuration value
        Returns:
        a new Configurer instance with the supplied value
        Since:
        1.0.0
      • compare

        public Configurer<T> compare​(java.util.function.Supplier<T> comparedValueSupplier)
        Compares the current value with a value resolved from the given Supplier. If the current value equals the compared value, the current value is discarded (set to null) to indicate that no change has occurred.

        Example Usage

        
           Configurer.configure("maxConnections", 100)
                     .compare(() -> loadPreviousMaxConnections())
                     .apply(val -> updateConnectionPool(val));
         
        Parameters:
        comparedValueSupplier - a supplier providing the value to compare against
        Returns:
        this Configurer instance for method chaining
        Since:
        1.0.0
      • compare

        public Configurer<T> compare​(T comparedValue)
        Compares the current value with the given value. If both values are equal, the current value is discarded (set to null) to indicate that no configuration change occurred. This is useful for avoiding unnecessary updates when the value has not changed.

        Example Usage

        
           Configurer.configure("retryCount", 3)
                     .compare(3)
                     .apply(val -> System.out.println("Retry count changed to: " + val));
           // Nothing is printed because the value has not changed
         
        Parameters:
        comparedValue - the value to compare against the current value
        Returns:
        this Configurer instance for method chaining
        Since:
        1.0.0
      • on

        public Configurer<T> on​(java.util.function.Predicate<? super T> predicate)
        Filters the current value using the given Predicate. If the predicate evaluates to false, the current value is discarded (set to null) and subsequent operations in the chain will be skipped.

        Example Usage

        
           Configurer.configure("timeout", 30)
                     .on(val -> val > 0 && val <= 300)
                     .apply(val -> System.out.println("Valid timeout: " + val));
         
        Parameters:
        predicate - the predicate to test the current value against
        Returns:
        this Configurer instance for method chaining
        Since:
        1.0.0
      • as

        public <R> Configurer<R> as​(java.util.function.Function<T,​R> function)
        Converts the current value to a new type using the given Function. If the current value is null or the function returns null, the resulting configurer will hold a null value.

        Example Usage

        
           Configurer.configure("timeout", "5000")
                     .as(Integer::parseInt)
                     .on(ms -> ms > 0)
                     .apply(ms -> httpClient.setReadTimeout(ms));
         
        Type Parameters:
        R - the target type after conversion
        Parameters:
        function - the conversion function to apply to the current value
        Returns:
        a Configurer holding the converted value
        Since:
        1.0.0
      • apply

        public void apply​(java.util.function.Consumer<T> valueConsumer)
        Terminal operation that applies the given Consumer to the current value if it is non-null. If the value has been discarded by a prior compare(java.util.function.Supplier<T>) or on(java.util.function.Predicate<? super T>) operation, the consumer is not invoked. This method always logs the outcome.

        Example Usage

        
           Configurer.configure("maxPoolSize", 20)
                     .compare(10)
                     .on(val -> val > 0)
                     .apply(val -> dataSource.setMaxPoolSize(val));
         
        Parameters:
        valueConsumer - the consumer to accept the current value
        Since:
        1.0.0
      • configure

        public static <T> Configurer<T> configure​(java.lang.String name)
        Creates a new Configurer with the specified name and no initial value. The value can be set later using value(Object) or value(Supplier).

        Example Usage

        
           Configurer.<Integer>configure("connectionTimeout")
                     .value(5000)
                     .on(val -> val > 0)
                     .apply(val -> client.setConnectionTimeout(val));
         
        Type Parameters:
        T - the type of the value to be configured
        Parameters:
        name - the descriptive name of the configuration entry
        Returns:
        a new Configurer instance with the given name
        Since:
        1.0.0
      • configure

        public static <T> Configurer<T> configure​(java.lang.String name,
                                                  T value)
        Creates a new Configurer with the specified name and initial value.

        Example Usage

        
           Configurer.configure("retryCount", 3)
                     .compare(previousRetryCount)
                     .on(val -> val >= 0 && val <= 10)
                     .apply(val -> retryPolicy.setMaxRetries(val));
         
        Type Parameters:
        T - the type of the value to be configured
        Parameters:
        name - the descriptive name of the configuration entry
        value - the initial configuration value
        Returns:
        a new Configurer instance with the given name and value
        Since:
        1.0.0
      • configure

        public static <T> Configurer<T> configure​(java.lang.String name,
                                                  java.util.function.Supplier<T> valueSupplier)
        Creates a new Configurer with the specified name and a value eagerly resolved from the given Supplier.

        Example Usage

        
           Configurer.configure("cacheSize", () -> Integer.getInteger("cache.size", 256))
                     .on(size -> size > 0)
                     .apply(size -> cacheManager.resize(size));
         
        Type Parameters:
        T - the type of the value to be configured
        Parameters:
        name - the descriptive name of the configuration entry
        valueSupplier - a supplier providing the initial configuration value
        Returns:
        a new Configurer instance with the given name and supplied value
        Since:
        1.0.0
      • configure

        public static <T> Configurer<T> configure​(T value)
        Creates a new unnamed Configurer with the specified value.

        Example Usage

        
           Configurer.configure(Duration.ofSeconds(30))
                     .as(Duration::toMillis)
                     .apply(ms -> httpClient.setReadTimeout(ms));
         
        Type Parameters:
        T - the type of the value to be configured
        Parameters:
        value - the initial configuration value
        Returns:
        a new unnamed Configurer instance with the given value
        Since:
        1.0.0
      • configure

        public static <T> Configurer<T> configure​(java.util.function.Supplier<T> valueSupplier)
        Creates a new unnamed Configurer with a value eagerly resolved from the given Supplier.

        Example Usage

        
           Configurer.configure(() -> System.getenv("LOG_LEVEL"))
                     .on(level -> level != null && !level.isEmpty())
                     .apply(level -> loggerConfig.setLevel(level));
         
        Type Parameters:
        T - the type of the value to be configured
        Parameters:
        valueSupplier - a supplier providing the initial configuration value
        Returns:
        a new unnamed Configurer instance with the supplied value
        Since:
        1.0.0