Skip to content

Commit 9e93aa2

Browse files
committed
Fix checkstyle errors
1 parent a1a31ae commit 9e93aa2

3 files changed

Lines changed: 77 additions & 27 deletions

File tree

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
import javax.annotation.Nonnull;
44
import javax.annotation.Nullable;
55

6+
import io.jooby.Value;
7+
8+
/**
9+
* A restricted base type of {@link Value} for the {@link ValueConverter} SPI.
10+
* @author agentgt
11+
*
12+
*/
613
public interface ValueContainer {
7-
14+
815
/**
916
* Get a value at the given position.
1017
*
@@ -20,11 +27,21 @@ public interface ValueContainer {
2027
* @return Field value.
2128
*/
2229
@Nonnull ValueContainer get(@Nonnull String name);
23-
30+
31+
/**
32+
* Get string value.
33+
*
34+
* @return String value.
35+
*/
2436
@Nonnull String value();
25-
37+
38+
/**
39+
* Convert this value to String (if possible) or <code>null</code> when missing.
40+
*
41+
* @return Convert this value to String (if possible) or <code>null</code> when missing.
42+
*/
2643
@Nullable String valueOrNull();
27-
44+
2845
/**
2946
* True for missing values.
3047
*
@@ -38,7 +55,7 @@ public interface ValueContainer {
3855
* @return Number of values. Mainly for array and hash values.
3956
*/
4057
int size();
41-
58+
4259
/**
4360
* True if this value is an array/sequence (not single or hash).
4461
*

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,28 @@
66
import io.jooby.TypeMismatchException;
77
import io.jooby.Value;
88

9+
/**
10+
* An SPI for value conversion.
11+
* @author agentgt
12+
*/
913
public interface ValueConverter {
1014
/**
11-
* This defaults to true to allow a functional interface.
12-
* @param type
13-
* @return
15+
* A short circuit to see if the converter supports the given type.
16+
*
17+
* This defaults to true to allow a functional interface since convert can return null
18+
* to indicate it does not support the type.
19+
*
20+
* @param type class or interface
21+
* @return true if the converter can convert for the type
1422
*/
1523
default boolean supportsType(@Nonnull Class<?> type) {
1624
return true;
1725
}
1826
/**
1927
* Converts values for {@link Value#to} and friends. Returning null indicates the type is not supported
2028
* or the converter chose not to do the conversion.
21-
* @param value
22-
* @param type
29+
* @param value the value to be converted
30+
* @param type the desired type. The type should be the equal or a super of the resulting instance returned.
2331
* @return <code>null</code> indicates that the converter chose to delegate to other converters down the chain.
2432
* @throws TypeMismatchException if the converter cannot convert the type and does not want to delegate.
2533
*/

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

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,45 @@
77

88
import io.jooby.TypeMismatchException;
99

10+
/**
11+
* Contains the {@link ValueConverter}s loaded via the ServiceLoader. It is is a
12+
* singleton and an instance can be retrieved with {@link #getInstance()}. The
13+
* ValueConverters are stored in an ordered collection and thus resolution of
14+
* type to be converted is based on the order of the converters.
15+
*
16+
* @author agentgt
17+
*
18+
*/
1019
public final class ValueConverters {
20+
1121
// Allow thread safe adding of ValueConverters.
1222
private final CopyOnWriteArrayList<ValueConverter> valueConverters;
13-
23+
1424
// Initialization on demand
1525
private static final class Hidden {
26+
1627
private static final ValueConverters INSTANCE = ValueConverters.create().fromServiceLoader();
1728
}
18-
29+
1930
private ValueConverters(CopyOnWriteArrayList<ValueConverter> valueConverters) {
2031
super();
2132
this.valueConverters = valueConverters;
2233
}
23-
34+
2435
static ValueConverters create() {
2536
return new ValueConverters(new CopyOnWriteArrayList<>());
2637
}
27-
38+
39+
/**
40+
* Attempts to convert values to an object based on the provided type.
41+
*
42+
* @param v
43+
* value
44+
* @param c
45+
* desired type
46+
* @return the type if converted or null if conversion was not possible.
47+
* @throws TypeMismatchException failure in a converter
48+
*/
2849
public @Nullable Object convert(ValueContainer v, Class<?> c) throws TypeMismatchException {
2950
Object result = null;
3051
for (ValueConverter vc : valueConverters) {
@@ -37,33 +58,37 @@ static ValueConverters create() {
3758
}
3859
return result;
3960
}
40-
41-
final ValueConverters fromServiceLoader() {
61+
62+
ValueConverters fromServiceLoader() {
4263
ServiceLoader<ValueConverter> sl = ServiceLoader.load(ValueConverter.class);
43-
//If any failes to load we will fail entirely.
44-
//The value converters found earlier in the classpath take precedence.
64+
// If any failes to load we will fail entirely.
65+
// The value converters found earlier in the classpath take precedence.
4566
sl.forEach(this::add);
4667
return this;
4768
}
69+
4870
/**
49-
* You can add value converters programmatic. For now its protected.
50-
* Its also to aid unit testing since serviceloader is inherently static singleton.
71+
* You can add value converters programmatic. For now its protected. Its also
72+
* to aid unit testing since serviceloader is inherently static singleton.
73+
*
5174
* @param vc
5275
* @return
5376
*/
54-
/* private */ final ValueConverters add(ValueConverter vc) {
77+
/* private */ ValueConverters add(ValueConverter vc) {
5578
valueConverters.add(vc);
5679
return this;
5780
}
58-
59-
final ValueConverters clear() {
81+
82+
ValueConverters clear() {
6083
valueConverters.clear();
6184
return this;
6285
}
63-
64-
65-
66-
public static final ValueConverters getInstance() {
86+
87+
/**
88+
* The ValueConverters singleton usually preloaded by the ServiceLoader.
89+
* @return the shared singleton used by Jooby
90+
*/
91+
public static ValueConverters getInstance() {
6792
return ValueConverters.Hidden.INSTANCE;
6893
}
6994

0 commit comments

Comments
 (0)