Skip to content

Commit 586e631

Browse files
committed
Bean Value API: refactor
- remove ValueContainer - make supporsType non default - rename for now ValueConverter to BeanValueConverter
1 parent f236119 commit 586e631

6 files changed

Lines changed: 38 additions & 111 deletions

File tree

jooby/src/main/java/io/jooby/Value.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import io.jooby.internal.MissingValue;
1111
import io.jooby.internal.SingleValue;
1212
import io.jooby.internal.ValueInjector;
13-
import io.jooby.spi.ValueContainer;
1413

1514
import javax.annotation.Nonnull;
1615
import javax.annotation.Nullable;
@@ -53,7 +52,7 @@
5352
* @since 2.0.0
5453
* @author edgar
5554
*/
56-
public interface Value extends Iterable<Value>, ValueContainer {
55+
public interface Value extends Iterable<Value> {
5756

5857
/**
5958
* Convert this value to long (if possible).

jooby/src/main/java/io/jooby/internal/ValueInjector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import io.jooby.TypeMismatchException;
1515
import io.jooby.Value;
1616
import io.jooby.internal.reflect.$Types;
17-
import io.jooby.spi.ValueConverters;
17+
import io.jooby.spi.BeanValueConverters;
1818

1919
import javax.inject.Inject;
2020
import javax.inject.Named;
@@ -148,7 +148,7 @@ private static Object resolve(Value scope, Class type)
148148
throws IllegalAccessException, InvocationTargetException, InstantiationException,
149149
NoSuchMethodException {
150150
if (scope.isObject() || scope.isSingle()) {
151-
Object o = ValueConverters.getInstance().convert(scope, type);
151+
Object o = BeanValueConverters.getInstance().convert(scope, type);
152152
if (o != null) {
153153
return o;
154154
}

jooby/src/main/java/io/jooby/spi/ValueConverter.java renamed to jooby/src/main/java/io/jooby/spi/BeanValueConverter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* An SPI for value conversion.
1111
* @author agentgt
1212
*/
13-
public interface ValueConverter {
13+
public interface BeanValueConverter {
1414
/**
1515
* A short circuit to see if the converter supports the given type.
1616
*
@@ -20,9 +20,7 @@ public interface ValueConverter {
2020
* @param type class or interface
2121
* @return true if the converter can convert for the type
2222
*/
23-
default boolean supportsType(@Nonnull Class<?> type) {
24-
return true;
25-
}
23+
boolean supportsType(@Nonnull Class<?> type);
2624
/**
2725
* Converts values for {@link Value#to} and friends. Returning null indicates the type is not supported
2826
* or the converter chose not to do the conversion.
@@ -31,5 +29,5 @@ default boolean supportsType(@Nonnull Class<?> type) {
3129
* @return <code>null</code> indicates that the converter chose to delegate to other converters down the chain.
3230
* @throws TypeMismatchException if the converter cannot convert the type and does not want to delegate.
3331
*/
34-
@Nullable Object convert(@Nonnull ValueContainer value, @Nonnull Class<?> type) throws TypeMismatchException;
32+
@Nullable Object convert(@Nonnull Value value, @Nonnull Class<?> type) throws TypeMismatchException;
3533
}

jooby/src/main/java/io/jooby/spi/ValueConverters.java renamed to jooby/src/main/java/io/jooby/spi/BeanValueConverters.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,29 @@
77
import javax.annotation.Nullable;
88

99
import io.jooby.TypeMismatchException;
10+
import io.jooby.Value;
1011

1112
/**
12-
* Contains the {@link ValueConverter}s loaded via the ServiceLoader. It is is a
13+
* Contains the {@link BeanValueConverter}s loaded via the ServiceLoader. It is is a
1314
* singleton and an instance can be retrieved with {@link #getInstance()}. The
1415
* ValueConverters are stored in an ordered collection and thus resolution of
1516
* type to be converted is based on the order of the converters.
1617
*
1718
* @author agentgt
1819
*
1920
*/
20-
public final class ValueConverters {
21+
public final class BeanValueConverters {
2122

2223
// Allow thread safe adding of ValueConverters.
23-
private final Iterable<ValueConverter> valueConverters;
24+
private final Iterable<BeanValueConverter> valueConverters;
2425

2526
// Initialization on demand
2627
private static final class Hidden {
2728

28-
private static volatile ValueConverters instance = ValueConverters.builder().fromServiceLoader().build();
29+
private static volatile BeanValueConverters instance = BeanValueConverters.builder().fromServiceLoader().build();
2930
}
3031

31-
private ValueConverters(Iterable<ValueConverter> valueConverters) {
32+
private BeanValueConverters(Iterable<BeanValueConverter> valueConverters) {
3233
super();
3334
this.valueConverters = valueConverters;
3435
}
@@ -39,10 +40,10 @@ static Builder builder() {
3940

4041
static final class Builder {
4142

42-
private final List<ValueConverter> valueConverters = new ArrayList<>();
43+
private final List<BeanValueConverter> valueConverters = new ArrayList<>();
4344

4445
Builder fromServiceLoader() {
45-
ServiceLoader<ValueConverter> sl = ServiceLoader.load(ValueConverter.class);
46+
ServiceLoader<BeanValueConverter> sl = ServiceLoader.load(BeanValueConverter.class);
4647
// If any failes to load we will fail entirely.
4748
// The value converters found earlier in the classpath take precedence.
4849
sl.forEach(this::add);
@@ -57,7 +58,7 @@ Builder fromServiceLoader() {
5758
* @param vc
5859
* @return
5960
*/
60-
Builder add(ValueConverter vc) {
61+
Builder add(BeanValueConverter vc) {
6162
valueConverters.add(vc);
6263
return this;
6364
}
@@ -67,12 +68,12 @@ Builder clear() {
6768
return this;
6869
}
6970

70-
ValueConverters build() {
71-
return new ValueConverters(valueConverters);
71+
BeanValueConverters build() {
72+
return new BeanValueConverters(valueConverters);
7273
}
7374

74-
ValueConverters set() {
75-
ValueConverters vc = build();
75+
BeanValueConverters set() {
76+
BeanValueConverters vc = build();
7677
Hidden.instance = vc;
7778
return vc;
7879
}
@@ -89,9 +90,9 @@ ValueConverters set() {
8990
* @throws TypeMismatchException
9091
* failure in a converter
9192
*/
92-
public @Nullable Object convert(ValueContainer v, Class<?> c) throws TypeMismatchException {
93+
public @Nullable Object convert(Value v, Class<?> c) throws TypeMismatchException {
9394
Object result = null;
94-
for (ValueConverter vc : valueConverters) {
95+
for (BeanValueConverter vc : valueConverters) {
9596
if (vc.supportsType(c)) {
9697
result = vc.convert(v, c);
9798
if (result != null) {
@@ -107,8 +108,8 @@ ValueConverters set() {
107108
*
108109
* @return the shared singleton used by Jooby
109110
*/
110-
public static ValueConverters getInstance() {
111-
return ValueConverters.Hidden.instance;
111+
public static BeanValueConverters getInstance() {
112+
return BeanValueConverters.Hidden.instance;
112113
}
113114

114115
}

jooby/src/main/java/io/jooby/spi/ValueContainer.java

Lines changed: 0 additions & 80 deletions
This file was deleted.

jooby/src/test/java/io/jooby/spi/ValueConvertersTest.java renamed to jooby/src/test/java/io/jooby/spi/BeanValueConvertersTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,44 @@
44

55
import java.util.function.Consumer;
66

7+
import io.jooby.TypeMismatchException;
8+
import io.jooby.Value;
79
import org.junit.jupiter.api.AfterAll;
810
import org.junit.jupiter.api.Test;
911

1012
import io.jooby.MissingValueException;
1113
import io.jooby.QueryString;
1214
import io.jooby.internal.UrlParser;
1315

14-
class ValueConvertersTest {
16+
import javax.annotation.Nonnull;
17+
import javax.annotation.Nullable;
18+
19+
public class BeanValueConvertersTest {
1520

1621
@AfterAll
1722
static void restoreFromServiceLoader() {
1823
// Restore ValueConvert list for other unit tests.
19-
ValueConverters.builder().fromServiceLoader().set();
24+
BeanValueConverters.builder().fromServiceLoader().set();
2025
}
2126

2227
@Test
2328
void testConvert() {
24-
ValueConverters.builder().add((value, type) -> {
25-
if (type == MyValue.class) {
29+
BeanValueConverters.builder().add(new BeanValueConverter() {
30+
@Override public boolean supportsType(@Nonnull Class<?> type) {
31+
return type == MyValue.class;
32+
}
33+
34+
@Nullable @Override public Object convert(@Nonnull Value value,
35+
@Nonnull Class<?> type) throws TypeMismatchException {
2636
MyValue mv = new MyValue();
2737
// we have chosen simple parameters names to make sure we don't get a
2838
// false positve
2939
// from the reflection converter.
3040
mv.name = value.get("n").value();
3141
// TODO: ValueContainer probably should have primitive convert methods.
32-
mv.order = Integer.parseInt(value.get("o").value());
42+
mv.order = value.get("o").intValue();
3343
return mv;
3444
}
35-
return null;
3645
}).set();
3746
queryString("n=stuff&o=1", queryString -> {
3847
MyValue mv = queryString.to(MyValue.class);

0 commit comments

Comments
 (0)