Package io.microsphere.util
Class Configurer<T>
- java.lang.Object
-
- io.microsphere.util.Configurer<T>
-
- Type Parameters:
T- the type of the value being configured
public class Configurer<T> extends java.lang.ObjectA fluent configuration utility for applying conditional operations on a value of typeT.The
Configurerclass 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 protectedConfigurer(java.lang.String name)Constructs aConfigurerwith the specified name and no initial value.protectedConfigurer(java.lang.String name, java.util.function.Supplier<T> valueSupplier)Constructs aConfigurerwith the specified name and a value eagerly resolved from the givenSupplier.protectedConfigurer(java.lang.String name, T value)Constructs aConfigurerwith the specified name and initial value.protectedConfigurer(java.util.function.Supplier<T> valueSupplier)Constructs aConfigurerwith an unnamed configuration entry whose value is eagerly resolved from the givenSupplier.protectedConfigurer(T value)Constructs aConfigurerwith an unnamed configuration entry and the given value.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidapply(java.util.function.Consumer<T> valueConsumer)Terminal operation that applies the givenConsumerto 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 givenFunction.Configurer<T>compare(java.util.function.Supplier<T> comparedValueSupplier)Compares the current value with a value resolved from the givenSupplier.Configurer<T>compare(T comparedValue)Compares the current value with the given value.static <T> Configurer<T>configure(java.lang.String name)Creates a newConfigurerwith the specified name and no initial value.static <T> Configurer<T>configure(java.lang.String name, java.util.function.Supplier<T> valueSupplier)Creates a newConfigurerwith the specified name and a value eagerly resolved from the givenSupplier.static <T> Configurer<T>configure(java.lang.String name, T value)Creates a newConfigurerwith the specified name and initial value.static <T> Configurer<T>configure(java.util.function.Supplier<T> valueSupplier)Creates a new unnamedConfigurerwith a value eagerly resolved from the givenSupplier.static <T> Configurer<T>configure(T value)Creates a new unnamedConfigurerwith the specified value.Configurer<T>on(java.util.function.Predicate<? super T> predicate)Filters the current value using the givenPredicate.<T> Configurer<T>value(java.util.function.Supplier<T> valueSupplier)Creates a newConfigurerwith the same name and a value eagerly resolved from the givenSupplier, replacing the current configuration value.<T> Configurer<T>value(T value)Creates a newConfigurerwith the same name and the specified value, replacing the current configuration value.
-
-
-
Constructor Detail
-
Configurer
protected Configurer(java.lang.String name)
Constructs aConfigurerwith the specified name and no initial value. The value can be set later usingvalue(Object)orvalue(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 aConfigurerwith an unnamed configuration entry whose value is eagerly resolved from the givenSupplier.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 aConfigurerwith 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 aConfigurerwith the specified name and a value eagerly resolved from the givenSupplier.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 entryvalueSupplier- a supplier providing the initial configuration value- Since:
- 1.0.0
-
Configurer
protected Configurer(java.lang.String name, T value)Constructs aConfigurerwith 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 entryvalue- the initial configuration value- Since:
- 1.0.0
-
-
Method Detail
-
value
public <T> Configurer<T> value(T value)
Creates a newConfigurerwith 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
Configurerinstance with the given value - Since:
- 1.0.0
-
value
public <T> Configurer<T> value(java.util.function.Supplier<T> valueSupplier)
Creates a newConfigurerwith the same name and a value eagerly resolved from the givenSupplier, 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
Configurerinstance 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 givenSupplier. If the current value equals the compared value, the current value is discarded (set tonull) 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
Configurerinstance 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 tonull) 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
Configurerinstance 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 givenPredicate. If the predicate evaluates tofalse, the current value is discarded (set tonull) 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
Configurerinstance 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 givenFunction. If the current value isnullor the function returnsnull, the resulting configurer will hold anullvalue.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
Configurerholding the converted value - Since:
- 1.0.0
-
apply
public void apply(java.util.function.Consumer<T> valueConsumer)
Terminal operation that applies the givenConsumerto the current value if it is non-null. If the value has been discarded by a priorcompare(java.util.function.Supplier<T>)oron(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 newConfigurerwith the specified name and no initial value. The value can be set later usingvalue(Object)orvalue(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
Configurerinstance with the given name - Since:
- 1.0.0
-
configure
public static <T> Configurer<T> configure(java.lang.String name, T value)
Creates a newConfigurerwith 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 entryvalue- the initial configuration value- Returns:
- a new
Configurerinstance 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 newConfigurerwith the specified name and a value eagerly resolved from the givenSupplier.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 entryvalueSupplier- a supplier providing the initial configuration value- Returns:
- a new
Configurerinstance with the given name and supplied value - Since:
- 1.0.0
-
configure
public static <T> Configurer<T> configure(T value)
Creates a new unnamedConfigurerwith 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
Configurerinstance with the given value - Since:
- 1.0.0
-
configure
public static <T> Configurer<T> configure(java.util.function.Supplier<T> valueSupplier)
Creates a new unnamedConfigurerwith a value eagerly resolved from the givenSupplier.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
Configurerinstance with the supplied value - Since:
- 1.0.0
-
-