Skip to content

Commit df7757e

Browse files
committed
ConfigInstanceBuilder cleanups:
- Remove unused methods from ConfigInstanceBuilder - Share the same code between ConfigInstanceBuilder and ConfigMappingContext - Simplify ConfigMappingGenerator
1 parent 7d24b4f commit df7757e

File tree

7 files changed

+166
-488
lines changed

7 files changed

+166
-488
lines changed

implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilder.java

Lines changed: 71 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.eclipse.microprofile.config.spi.Converter;
1515

1616
/**
17-
* A builder which can produce instances of a configuration interface.
17+
* A builder which can produce instances of a configuration interface annotated with {@link ConfigMapping}.
1818
* <p>
1919
* Objects which are produced by this API will contain values for every property found on the configuration
2020
* interface or its supertypes.
@@ -24,22 +24,42 @@
2424
* If the runtime is Java 16 or later, the returned object <em>may</em> be a {@code Record}.
2525
* <p>
2626
* To provide a value for a property, use a method reference to indicate which property the value should be associated
27-
* with.
28-
* For example,
27+
* with. For example,
2928
*
3029
* <pre>
31-
<code>
32-
ConfigInstanceBuilder&lt;MyProgramConfig&gt; builder = ConfigInstanceBuilder.forInterface(MyProgramConfig.class);
33-
builder.with(MyProgramConfig::message, "Hello everyone!");
34-
builder.with(MyProgramConfig::repeatCount, 42);
35-
MyProgramConfig config = builder.build();
36-
for (int i = 0; i < config.repeatCount(); i ++) {
37-
System.out.println(config.message());
38-
}
39-
</code>
30+
<code>
31+
32+
&#064;ConfigMapping
33+
interface MyProgramConfig {
34+
String message();
35+
int repeatCount();
36+
}
37+
38+
ConfigInstanceBuilder&lt;MyProgramConfig&gt; builder = ConfigInstanceBuilder.forInterface(MyProgramConfig.class);
39+
builder.with(MyProgramConfig::message, "Hello everyone!");
40+
builder.with(MyProgramConfig::repeatCount, 42);
41+
42+
MyProgramConfig config = builder.build();
43+
for (int i = 0; i < config.repeatCount(); i ++) {
44+
System.out.println(config.message());
45+
}
46+
</code>
4047
* </pre>
4148
*
49+
* Configuration interface member types are automatically converted with a {@link Converter}. Global converters are
50+
* registered either by being discovered via the {@link java.util.ServiceLoader} mechanism, and can be
51+
* registered by providing a {@code META-INF/services/org.eclipse.microprofile.config.spi.Converter} file, which
52+
* contains the fully qualified class name of the custom {@code Converter} implementation, or explicitly by calling
53+
* {@link ConfigInstanceBuilder#registerConverter(Class, Converter)}.
54+
* <p>
55+
* Converters follow the same rules applied to {@link io.smallrye.config.SmallRyeConfig} and
56+
* {@link io.smallrye.config.ConfigMapping}, including overriding the converter to use with
57+
* {@link io.smallrye.config.WithConverter}.
58+
*
4259
* @param <I> the configuration interface type
60+
*
61+
* @see io.smallrye.config.ConfigMapping
62+
* @see org.eclipse.microprofile.config.spi.Converter
4363
*/
4464
public interface ConfigInstanceBuilder<I> {
4565
/**
@@ -164,127 +184,9 @@ default ConfigInstanceBuilder<I> withOptional(OptionalDoubleGetter<I> getter, do
164184
*/
165185
default <F extends Function<? super I, Optional<Boolean>> & Serializable> ConfigInstanceBuilder<I> withOptional(F getter,
166186
boolean value) {
167-
return with(getter, Optional.of(Boolean.valueOf(value)));
187+
return with(getter, Optional.of(value));
168188
}
169189

170-
/**
171-
* Set a property to its default value (if any).
172-
*
173-
* @param getter the property to modify (must not be {@code null})
174-
* @param <T> the value type
175-
* @param <F> the accessor type
176-
* @return this builder (not {@code null})
177-
* @throws IllegalArgumentException if the getter is {@code null}
178-
*/
179-
<T, F extends Function<? super I, T> & Serializable> ConfigInstanceBuilder<I> withDefaultFor(F getter);
180-
181-
/**
182-
* Set a property to its default value (if any).
183-
*
184-
* @param getter the property to modify (must not be {@code null})
185-
* @param <F> the accessor type
186-
* @return this builder (not {@code null})
187-
* @throws IllegalArgumentException if the getter is {@code null}
188-
*/
189-
<F extends ToIntFunction<? super I> & Serializable> ConfigInstanceBuilder<I> withDefaultFor(F getter);
190-
191-
/**
192-
* Set a property to its default value (if any).
193-
*
194-
* @param getter the property to modify (must not be {@code null})
195-
* @param <F> the accessor type
196-
* @return this builder (not {@code null})
197-
* @throws IllegalArgumentException if the getter is {@code null}
198-
*/
199-
<F extends ToLongFunction<? super I> & Serializable> ConfigInstanceBuilder<I> withDefaultFor(F getter);
200-
201-
/**
202-
* Set a property to its default value (if any).
203-
*
204-
* @param getter the property to modify (must not be {@code null})
205-
* @param <F> the accessor type
206-
* @return this builder (not {@code null})
207-
* @throws IllegalArgumentException if the getter is {@code null}
208-
*/
209-
<F extends Predicate<? super I> & Serializable> ConfigInstanceBuilder<I> withDefaultFor(F getter);
210-
211-
/**
212-
* Set a property on the configuration object to a string value.
213-
* The value set on the property will be the result of conversion of the string
214-
* using the property's converter.
215-
*
216-
* @param getter the property accessor (must not be {@code null})
217-
* @param value the value to set (must not be {@code null})
218-
* @return this builder (not {@code null})
219-
* @param <F> the accessor type
220-
* @throws IllegalArgumentException if the getter is {@code null},
221-
* or if the value is {@code null},
222-
* or if the value was rejected by the converter
223-
*/
224-
<F extends Function<? super I, ?> & Serializable> ConfigInstanceBuilder<I> withString(F getter, String value);
225-
226-
/**
227-
* Set a property on the configuration object to a string value.
228-
* The value set on the property will be the result of conversion of the string
229-
* using the property's converter.
230-
*
231-
* @param getter the property accessor (must not be {@code null})
232-
* @param value the value to set (must not be {@code null})
233-
* @return this builder (not {@code null})
234-
* @param <F> the accessor type
235-
* @throws IllegalArgumentException if the getter is {@code null},
236-
* or if the value is {@code null},
237-
* or if the value was rejected by the converter
238-
*/
239-
<F extends ToIntFunction<? super I> & Serializable> ConfigInstanceBuilder<I> withString(F getter, String value);
240-
241-
/**
242-
* Set a property on the configuration object to a string value.
243-
* The value set on the property will be the result of conversion of the string
244-
* using the property's converter.
245-
*
246-
* @param getter the property accessor (must not be {@code null})
247-
* @param value the value to set (must not be {@code null})
248-
* @return this builder (not {@code null})
249-
* @param <F> the accessor type
250-
* @throws IllegalArgumentException if the getter is {@code null},
251-
* or if the value is {@code null},
252-
* or if the value was rejected by the converter
253-
*/
254-
<F extends ToLongFunction<? super I> & Serializable> ConfigInstanceBuilder<I> withString(F getter, String value);
255-
256-
/**
257-
* Set a property on the configuration object to a string value.
258-
* The value set on the property will be the result of conversion of the string
259-
* using the property's converter.
260-
*
261-
* @param getter the property accessor (must not be {@code null})
262-
* @param value the value to set (must not be {@code null})
263-
* @return this builder (not {@code null})
264-
* @param <F> the accessor type
265-
* @throws IllegalArgumentException if the getter is {@code null},
266-
* or if the value is {@code null},
267-
* or if the value was rejected by the converter
268-
*/
269-
<F extends Predicate<? super I> & Serializable> ConfigInstanceBuilder<I> withString(F getter, String value);
270-
271-
/**
272-
* Set a property on the configuration object to a string value, using the property's
273-
* declaring class and name to identify the property to set.
274-
* The value set on the property will be the result of conversion of the string
275-
* using the property's converter.
276-
*
277-
* @param propertyClass the declaring class of the property to set (must not be {@code null})
278-
* @param propertyName the name of the property to set (must not be {@code null})
279-
* @param value the value to set (must not be {@code null})
280-
* @return this builder (not {@code null})
281-
* @throws IllegalArgumentException if the property class or name is {@code null},
282-
* or if the value is {@code null},
283-
* or if the value was rejected by the converter,
284-
* or if no property matches the given name and declaring class
285-
*/
286-
ConfigInstanceBuilder<I> withString(Class<? super I> propertyClass, String propertyName, String value);
287-
288190
/**
289191
* Build the configuration instance.
290192
*
@@ -312,25 +214,63 @@ static <I> ConfigInstanceBuilder<I> forInterface(Class<I> interfaceClass)
312214
return ConfigInstanceBuilderImpl.forInterface(interfaceClass);
313215
}
314216

217+
/**
218+
* Globally registers a {@link org.eclipse.microprofile.config.spi.Converter} to be used by the
219+
* {@link io.smallrye.config.ConfigInstanceBuilder} to convert configuration interface member types.
220+
*
221+
* @param type the class of the type to convert
222+
* @param converter the converter instance that can convert to the type
223+
* @param <T> the type to convert
224+
*/
315225
static <T> void registerConverter(Class<T> type, Converter<T> converter) {
316226
ConfigInstanceBuilderImpl.CONVERTERS.put(type, converter);
317227
}
318228

229+
/**
230+
* Represents a getter in the configuration interface of primitive type {@code int}.
231+
*
232+
* @param <T> the configuration interface type
233+
*/
319234
interface ToIntFunctionGetter<T> extends ToIntFunction<T>, Serializable {
320235
}
321236

237+
/**
238+
* Represents a getter in the configuration interface of primitive type {@code long}.
239+
*
240+
* @param <T> the configuration interface type
241+
*/
322242
interface ToLongFunctionGetter<T> extends ToLongFunction<T>, Serializable {
323243
}
324244

245+
/**
246+
* Represents a getter in the configuration interface of primitive type {@code double}.
247+
*
248+
* @param <T> the configuration interface type
249+
*/
325250
interface ToDoubleFunctionGetter<T> extends ToDoubleFunction<T>, Serializable {
326251
}
327252

253+
/**
254+
* Represents a getter in the configuration interface of type {@code OptionalInt}.
255+
*
256+
* @param <T> the configuration interface type
257+
*/
328258
interface OptionalIntGetter<T> extends Function<T, OptionalInt>, Serializable {
329259
}
330260

261+
/**
262+
* Represents a getter in the configuration interface of type {@code OptionalLong}.
263+
*
264+
* @param <T> the configuration interface type
265+
*/
331266
interface OptionalLongGetter<T> extends Function<T, OptionalLong>, Serializable {
332267
}
333268

269+
/**
270+
* Represents a getter in the configuration interface of type {@code OptionalDouble}.
271+
*
272+
* @param <T> the configuration interface type
273+
*/
334274
interface OptionalDoubleGetter<T> extends Function<T, OptionalDouble>, Serializable {
335275
}
336276
}

0 commit comments

Comments
 (0)