From dd5038e323a3463fc27c5ef49a6cee8ac2ecb5c0 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Tue, 3 Oct 2023 14:48:52 -0500 Subject: [PATCH] [WIP] API for config instance builder Fixes #1001 --- .../config/ConfigInstanceBuilder.java | 311 ++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100644 implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilder.java diff --git a/implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilder.java b/implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilder.java new file mode 100644 index 000000000..bbe485150 --- /dev/null +++ b/implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilder.java @@ -0,0 +1,311 @@ +package io.smallrye.config; + +import java.io.Serializable; +import java.util.Optional; +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.OptionalLong; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.ToIntFunction; +import java.util.function.ToLongFunction; + +/** + * A builder which can produce instances of a configuration interface. + *

+ * Objects which are produced by this API will contain values for every property found on the configuration + * interface or its supertypes. + * If no value is given for a property, its default value is used. + * If a required property has no default value, then an exception will be thrown when {@link #build} is called. + * The returned object instance is immutable and has a stable {@code equals} and {@code hashCode} method. + * If the runtime is Java 16 or later, the returned object may be a {@code Record}. + *

+ * To provide a value for a property, use a method reference to indicate which property the value should be associated + * with. + * For example, + *


+ * ConfigInstanceBuilder<MyProgramConfig> builder = ConfigInstanceBuilder.forInterface(MyProgramConfig.class);
+ * builder.with(MyProgramConfig::message, "Hello everyone!");
+ * builder.with(MyProgramConfig::repeatCount, 42);
+ * MyProgramConfig config = builder.build();
+ * for (int i = 0; i < config.repeatCount(); i ++) {
+ *     System.out.println(config.message());
+ * }
+ * 
+ * + * @param the configuration interface type + */ +public interface ConfigInstanceBuilder { + /** + * {@return the configuration interface (not null)} + */ + Class configurationInterface(); + + /** + * Set a property on the configuration object to an object value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the value type + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null} + * or if the value is {@code null} + */ + & Serializable> ConfigInstanceBuilder with(F getter, T value); + + /** + * Set a property on the configuration object to an integer value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder with(F getter, int value); + + /** + * Set a property on the configuration object to an integer value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder with(F getter, long value); + + /** + * Set a property on the configuration object to a floating-point value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder with(F getter, double value); + + /** + * Set a property on the configuration object to a boolean value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder with(F getter, boolean value); + + /** + * Set an optional property on the configuration object to an object value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @param the value type + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + * or the value is {@code null} + */ + default > & Serializable> ConfigInstanceBuilder withOptional(F getter, T value) { + return with(getter, Optional.of(value)); + } + + /** + * Set an optional property on the configuration object to an integer value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + default & Serializable> ConfigInstanceBuilder withOptional(F getter, int value) { + return with(getter, OptionalInt.of(value)); + } + + /** + * Set an optional property on the configuration object to an integer value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + default & Serializable> ConfigInstanceBuilder withOptional(F getter, long value) { + return with(getter, OptionalLong.of(value)); + } + + /** + * Set an optional property on the configuration object to a floating-point value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + default & Serializable> ConfigInstanceBuilder withOptional(F getter, double value) { + return with(getter, OptionalDouble.of(value)); + } + + /** + * Set an optional property on the configuration object to a boolean value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + default > & Serializable> ConfigInstanceBuilder withOptional(F getter, boolean value) { + return with(getter, Optional.of(Boolean.valueOf(value))); + } + + /** + * Set a property to its default value (if any). + * + * @param getter the property to modify (must not be {@code null}) + * @param the value type + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder withDefaultFor(F getter); + + /** + * Set a property to its default value (if any). + * + * @param getter the property to modify (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder withDefaultFor(F getter); + + /** + * Set a property to its default value (if any). + * + * @param getter the property to modify (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder withDefaultFor(F getter); + + /** + * Set a property to its default value (if any). + * + * @param getter the property to modify (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder withDefaultFor(F getter); + + /** + * Set a property on the configuration object to a string value. + * The value set on the property will be the result of conversion of the string + * using the property's converter. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null}, + * or if the value is {@code null}, + * or if the value was rejected by the converter + */ + & Serializable> ConfigInstanceBuilder withString(F getter, String value); + + /** + * Set a property on the configuration object to a string value. + * The value set on the property will be the result of conversion of the string + * using the property's converter. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null}, + * or if the value is {@code null}, + * or if the value was rejected by the converter + */ + & Serializable> ConfigInstanceBuilder withString(F getter, String value); + + /** + * Set a property on the configuration object to a string value. + * The value set on the property will be the result of conversion of the string + * using the property's converter. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null}, + * or if the value is {@code null}, + * or if the value was rejected by the converter + */ + & Serializable> ConfigInstanceBuilder withString(F getter, String value); + + /** + * Set a property on the configuration object to a string value. + * The value set on the property will be the result of conversion of the string + * using the property's converter. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null}, + * or if the value is {@code null}, + * or if the value was rejected by the converter + */ + & Serializable> ConfigInstanceBuilder withString(F getter, String value); + + /** + * Set a property on the configuration object to a string value, using the property's + * declaring class and name to identify the property to set. + * The value set on the property will be the result of conversion of the string + * using the property's converter. + * + * @param propertyClass the declaring class of the property to set (must not be {@code null}) + * @param propertyName the name of the property to set (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the property class or name is {@code null}, + * or if the value is {@code null}, + * or if the value was rejected by the converter, + * or if no property matches the given name and declaring class + */ + ConfigInstanceBuilder withString(Class propertyClass, String propertyName, String value); + + /** + * Build the configuration instance. + * + * @return the configuration instance (not {@code null}) + * @throws IllegalArgumentException if a required property does not have a value + */ + I build(); + + /** + * Get a builder instance for the given configuration interface. + * + * @param interfaceClass the interface class object (must not be {@code null}) + * @param the configuration interface type + * @return a new builder for the configuration interface (not {@code null}) + * @throws IllegalArgumentException if the interface class is {@code null}, + * or if the class object does not represent an interface, + * or if the interface is not a valid configuration interface, + * or if the interface has one or more required properties that were not given a value, + * or if the interface has one or more converters that could not be instantiated + * @throws SecurityException if this class does not have permission to introspect the given interface + * or one of its superinterfaces + */ + static ConfigInstanceBuilder forInterface(Class interfaceClass) throws IllegalArgumentException, SecurityException { + throw new UnsupportedOperationException("todo"); + } +}