|
14 | 14 | import org.eclipse.microprofile.config.spi.Converter; |
15 | 15 |
|
16 | 16 | /** |
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}. |
18 | 18 | * <p> |
19 | 19 | * Objects which are produced by this API will contain values for every property found on the configuration |
20 | 20 | * interface or its supertypes. |
|
24 | 24 | * If the runtime is Java 16 or later, the returned object <em>may</em> be a {@code Record}. |
25 | 25 | * <p> |
26 | 26 | * 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, |
29 | 28 | * |
30 | 29 | * <pre> |
31 | | -<code> |
32 | | -ConfigInstanceBuilder<MyProgramConfig> 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 | + @ConfigMapping |
| 33 | + interface MyProgramConfig { |
| 34 | + String message(); |
| 35 | + int repeatCount(); |
| 36 | + } |
| 37 | +
|
| 38 | + ConfigInstanceBuilder<MyProgramConfig> 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> |
40 | 47 | * </pre> |
41 | 48 | * |
| 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 | + * |
42 | 59 | * @param <I> the configuration interface type |
| 60 | + * |
| 61 | + * @see io.smallrye.config.ConfigMapping |
| 62 | + * @see org.eclipse.microprofile.config.spi.Converter |
43 | 63 | */ |
44 | 64 | public interface ConfigInstanceBuilder<I> { |
45 | 65 | /** |
@@ -164,127 +184,9 @@ default ConfigInstanceBuilder<I> withOptional(OptionalDoubleGetter<I> getter, do |
164 | 184 | */ |
165 | 185 | default <F extends Function<? super I, Optional<Boolean>> & Serializable> ConfigInstanceBuilder<I> withOptional(F getter, |
166 | 186 | boolean value) { |
167 | | - return with(getter, Optional.of(Boolean.valueOf(value))); |
| 187 | + return with(getter, Optional.of(value)); |
168 | 188 | } |
169 | 189 |
|
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 | | - |
288 | 190 | /** |
289 | 191 | * Build the configuration instance. |
290 | 192 | * |
@@ -312,25 +214,63 @@ static <I> ConfigInstanceBuilder<I> forInterface(Class<I> interfaceClass) |
312 | 214 | return ConfigInstanceBuilderImpl.forInterface(interfaceClass); |
313 | 215 | } |
314 | 216 |
|
| 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 | + */ |
315 | 225 | static <T> void registerConverter(Class<T> type, Converter<T> converter) { |
316 | 226 | ConfigInstanceBuilderImpl.CONVERTERS.put(type, converter); |
317 | 227 | } |
318 | 228 |
|
| 229 | + /** |
| 230 | + * Represents a getter in the configuration interface of primitive type {@code int}. |
| 231 | + * |
| 232 | + * @param <T> the configuration interface type |
| 233 | + */ |
319 | 234 | interface ToIntFunctionGetter<T> extends ToIntFunction<T>, Serializable { |
320 | 235 | } |
321 | 236 |
|
| 237 | + /** |
| 238 | + * Represents a getter in the configuration interface of primitive type {@code long}. |
| 239 | + * |
| 240 | + * @param <T> the configuration interface type |
| 241 | + */ |
322 | 242 | interface ToLongFunctionGetter<T> extends ToLongFunction<T>, Serializable { |
323 | 243 | } |
324 | 244 |
|
| 245 | + /** |
| 246 | + * Represents a getter in the configuration interface of primitive type {@code double}. |
| 247 | + * |
| 248 | + * @param <T> the configuration interface type |
| 249 | + */ |
325 | 250 | interface ToDoubleFunctionGetter<T> extends ToDoubleFunction<T>, Serializable { |
326 | 251 | } |
327 | 252 |
|
| 253 | + /** |
| 254 | + * Represents a getter in the configuration interface of type {@code OptionalInt}. |
| 255 | + * |
| 256 | + * @param <T> the configuration interface type |
| 257 | + */ |
328 | 258 | interface OptionalIntGetter<T> extends Function<T, OptionalInt>, Serializable { |
329 | 259 | } |
330 | 260 |
|
| 261 | + /** |
| 262 | + * Represents a getter in the configuration interface of type {@code OptionalLong}. |
| 263 | + * |
| 264 | + * @param <T> the configuration interface type |
| 265 | + */ |
331 | 266 | interface OptionalLongGetter<T> extends Function<T, OptionalLong>, Serializable { |
332 | 267 | } |
333 | 268 |
|
| 269 | + /** |
| 270 | + * Represents a getter in the configuration interface of type {@code OptionalDouble}. |
| 271 | + * |
| 272 | + * @param <T> the configuration interface type |
| 273 | + */ |
334 | 274 | interface OptionalDoubleGetter<T> extends Function<T, OptionalDouble>, Serializable { |
335 | 275 | } |
336 | 276 | } |
0 commit comments