Skip to content

Commit

Permalink
add getConverter method to expose converter for a given type
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Mattheis <[email protected]>
  • Loading branch information
emattheis committed Mar 12, 2020
1 parent 83db7f7 commit 5fd88df
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions api/src/main/java/org/eclipse/microprofile/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Optional;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;

/**
* <p>
Expand Down Expand Up @@ -144,7 +145,7 @@ public interface Config {
* @return the configuration sources
*/
Iterable<ConfigSource> getConfigSources();

/**
* Convert an abritrary value to the specified type using the converters known to this instance
*
Expand All @@ -157,5 +158,20 @@ public interface Config {
* @return the converted value
* @throws java.lang.IllegalArgumentException if the value cannot be converted to the specified type.
*/
<T> T convert(String value, Class<T> type);
default <T> T convert(String value, Class<T> type) {
return value == null ? null
: getConverter(type).orElseThrow(() -> new IllegalArgumentException("no converter available for " + type))
.convert(value);
}

/**
* Return the {@link Converter} used by this instance to produce instances of the specified type from string values.
*
* @param <T>
* the conversion type
* @param forType
* the type to be produced by the converter
* @return an {@link Optional} containing the converter, or empty if no converter is available for the specified type
*/
<T> Optional<Converter<T>> getConverter(Class<T> forType);
}

0 comments on commit 5fd88df

Please sign in to comment.