|
| 1 | +package io.jooby.spi; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.ServiceLoader; |
| 6 | + |
| 7 | +import javax.annotation.Nullable; |
| 8 | + |
| 9 | +import io.jooby.TypeMismatchException; |
| 10 | + |
| 11 | +/** |
| 12 | + * Contains the {@link ValueConverter}s loaded via the ServiceLoader. It is is a |
| 13 | + * singleton and an instance can be retrieved with {@link #getInstance()}. The |
| 14 | + * ValueConverters are stored in an ordered collection and thus resolution of |
| 15 | + * type to be converted is based on the order of the converters. |
| 16 | + * |
| 17 | + * @author agentgt |
| 18 | + * |
| 19 | + */ |
| 20 | +public final class ValueConverters { |
| 21 | + |
| 22 | + // Allow thread safe adding of ValueConverters. |
| 23 | + private final Iterable<ValueConverter> valueConverters; |
| 24 | + |
| 25 | + // Initialization on demand |
| 26 | + private static final class Hidden { |
| 27 | + |
| 28 | + private static volatile ValueConverters instance = ValueConverters.builder().fromServiceLoader().build(); |
| 29 | + } |
| 30 | + |
| 31 | + private ValueConverters(Iterable<ValueConverter> valueConverters) { |
| 32 | + super(); |
| 33 | + this.valueConverters = valueConverters; |
| 34 | + } |
| 35 | + |
| 36 | + static Builder builder() { |
| 37 | + return new Builder(); |
| 38 | + } |
| 39 | + |
| 40 | + static final class Builder { |
| 41 | + |
| 42 | + private final List<ValueConverter> valueConverters = new ArrayList<>(); |
| 43 | + |
| 44 | + Builder fromServiceLoader() { |
| 45 | + ServiceLoader<ValueConverter> sl = ServiceLoader.load(ValueConverter.class); |
| 46 | + // If any failes to load we will fail entirely. |
| 47 | + // The value converters found earlier in the classpath take precedence. |
| 48 | + sl.forEach(this::add); |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * You can add value converters programmatic. For now its protected. Its |
| 54 | + * also to aid unit testing since serviceloader is inherently static |
| 55 | + * singleton. |
| 56 | + * |
| 57 | + * @param vc |
| 58 | + * @return |
| 59 | + */ |
| 60 | + Builder add(ValueConverter vc) { |
| 61 | + valueConverters.add(vc); |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + Builder clear() { |
| 66 | + valueConverters.clear(); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + ValueConverters build() { |
| 71 | + return new ValueConverters(valueConverters); |
| 72 | + } |
| 73 | + |
| 74 | + ValueConverters set() { |
| 75 | + ValueConverters vc = build(); |
| 76 | + Hidden.instance = vc; |
| 77 | + return vc; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Attempts to convert values to an object based on the provided type. |
| 83 | + * |
| 84 | + * @param v |
| 85 | + * value |
| 86 | + * @param c |
| 87 | + * desired type |
| 88 | + * @return the type if converted or null if conversion was not possible. |
| 89 | + * @throws TypeMismatchException |
| 90 | + * failure in a converter |
| 91 | + */ |
| 92 | + public @Nullable Object convert(ValueContainer v, Class<?> c) throws TypeMismatchException { |
| 93 | + Object result = null; |
| 94 | + for (ValueConverter vc : valueConverters) { |
| 95 | + if (vc.supportsType(c)) { |
| 96 | + result = vc.convert(v, c); |
| 97 | + if (result != null) { |
| 98 | + return result; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return result; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * The ValueConverters singleton usually preloaded by the ServiceLoader. |
| 107 | + * |
| 108 | + * @return the shared singleton used by Jooby |
| 109 | + */ |
| 110 | + public static ValueConverters getInstance() { |
| 111 | + return ValueConverters.Hidden.instance; |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments