Skip to content

Commit 6f683f2

Browse files
committed
Value API: code clean up
1 parent 339e216 commit 6f683f2

14 files changed

Lines changed: 89 additions & 43 deletions

File tree

TODO

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
* Session set null context to Value API
2-
* review toString on Value API
3-
* Value vs ValueNode API
2+
* review toString on Value API (removal of QueryStringValue?)
43
* tests and coverage
54
* server options from application.conf MUST override hardcoded settings
65
* make reset headers on error configurable

jooby/src/main/java/io/jooby/BeanConverter.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import javax.annotation.Nonnull;
99

1010
/**
11-
* Bean value converter, works like {@link ValueConverter} except that the input value is always
12-
* a hash/object value, not a simple string like it is required by {@link ValueConverter}.
11+
* Value converter for complex values that come from query, path, form, etc... parameters into more
12+
* specific type.
1313
*
14-
* @since 2.1.1
14+
* It is an extension point for {@link ValueNode#to(Class)} calls.
1515
*/
1616
public interface BeanConverter {
1717
/**
@@ -22,5 +22,12 @@ public interface BeanConverter {
2222
*/
2323
boolean supports(@Nonnull Class type);
2424

25+
/**
26+
* Convert a node value into more specific type.
27+
*
28+
* @param node Value value.
29+
* @param type Requested type.
30+
* @return Converted value.
31+
*/
2532
Object convert(@Nonnull ValueNode node, @Nonnull Class type);
2633
}

jooby/src/main/java/io/jooby/Body.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import java.nio.channels.ReadableByteChannel;
1616
import java.nio.charset.Charset;
1717
import java.nio.file.Path;
18+
import java.util.Collections;
1819
import java.util.List;
20+
import java.util.Set;
1921

2022
/**
2123
* HTTP body value. Allows to access HTTP body as string, byte[], stream, etc..
@@ -78,6 +80,14 @@ public interface Body extends ValueNode {
7880
return to(Reified.list(type).getType());
7981
}
8082

83+
default @Nonnull @Override List<String> toList() {
84+
return Collections.singletonList(value());
85+
}
86+
87+
default @Nonnull @Override Set<String> toSet() {
88+
return Collections.singleton(value());
89+
}
90+
8191
@Override default @Nonnull <T> T to(@Nonnull Class<T> type) {
8292
return to((Type) type);
8393
}

jooby/src/main/java/io/jooby/FileUpload.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Iterator;
1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.Set;
2122

2223
/**
2324
* File upload class, file upload are available when request body is encoded as
@@ -93,6 +94,14 @@ public interface FileUpload extends ValueNode {
9394
throw new TypeMismatchException(name(), type);
9495
}
9596

97+
default @Nonnull @Override List<String> toList() {
98+
return Collections.singletonList(value());
99+
}
100+
101+
default @Nonnull @Override Set<String> toSet() {
102+
return Collections.singleton(value());
103+
}
104+
96105
/**
97106
* Multi-value map with field name as key and file name as values.
98107
*

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,18 +253,14 @@ default boolean booleanValue(boolean defaultValue) {
253253
*
254254
* @return List of values.
255255
*/
256-
@Nonnull default List<String> toList() {
257-
return Collections.emptyList();
258-
}
256+
@Nonnull List<String> toList();
259257

260258
/**
261259
* Get set of values.
262260
*
263261
* @return set of values.
264262
*/
265-
@Nonnull default Set<String> toSet() {
266-
return Collections.emptySet();
267-
}
263+
@Nonnull Set<String> toSet();
268264

269265
/**
270266
* Convert this value to an Enum.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Value converter for simple values that come from query, path, form, etc... parameters into more
1212
* specific type.
1313
*
14-
* It is an extension point for {@link ValueNode#to(Class)} calls.
14+
* It is an extension point for {@link Value#to(Class)} calls.
1515
*/
1616
public interface ValueConverter {
1717
/**
@@ -23,7 +23,7 @@ public interface ValueConverter {
2323
boolean supports(@Nonnull Class type);
2424

2525
/**
26-
* Convert string to specific type.
26+
* Convert simple to specific type.
2727
*
2828
* @param value Value value.
2929
* @param type Requested type.

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

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ArrayValue implements ValueNode {
2626

2727
private final String name;
2828

29-
private final List<ValueNode> value = new ArrayList<>(5);
29+
private final List<ValueNode> list = new ArrayList<>(5);
3030

3131
public ArrayValue(Context ctx, String name) {
3232
this.ctx = ctx;
@@ -38,7 +38,7 @@ public ArrayValue(Context ctx, String name) {
3838
}
3939

4040
public ArrayValue add(ValueNode value) {
41-
this.value.add(value);
41+
this.list.add(value);
4242
return this;
4343
}
4444

@@ -55,7 +55,7 @@ public ArrayValue add(String value) {
5555

5656
@Override public ValueNode get(@Nonnull int index) {
5757
try {
58-
return value.get(index);
58+
return list.get(index);
5959
} catch (IndexOutOfBoundsException x) {
6060
return new MissingValue(name + "[" + index + "]");
6161
}
@@ -66,33 +66,28 @@ public ArrayValue add(String value) {
6666
}
6767

6868
@Override public int size() {
69-
return value.size();
69+
return list.size();
7070
}
7171

7272
@Override public String value() {
7373
String name = name();
74-
7574
throw new TypeMismatchException(name == null ? getClass().getSimpleName() : name, String.class);
7675
}
7776

7877
@Override public String toString() {
79-
return value.toString();
78+
return list.toString();
8079
}
8180

8281
@Override public Iterator<ValueNode> iterator() {
83-
return value.iterator();
82+
return list.iterator();
8483
}
8584

8685
@Nonnull @Override public <T> T to(@Nonnull Class<T> type) {
87-
return ctx.convert(value.get(0), type);
86+
return ctx.convert(list.get(0), type);
8887
}
8988

9089
@Nonnull @Override public <T> List<T> toList(@Nonnull Class<T> type) {
91-
List<T> list = new ArrayList<>(value.size());
92-
for (ValueNode it : value) {
93-
list.add(it.to(type));
94-
}
95-
return list;
90+
return collect(new ArrayList<>(this.list.size()), type);
9691
}
9792

9893
@Nonnull @Override public <T> Optional<T> toOptional(@Nonnull Class<T> type) {
@@ -104,29 +99,27 @@ public ArrayValue add(String value) {
10499
}
105100

106101
@Nonnull @Override public <T> Set<T> toSet(@Nonnull Class<T> type) {
107-
Set<T> list = new LinkedHashSet<>(value.size());
108-
for (ValueNode it : value) {
109-
list.add(it.to(type));
110-
}
111-
return list;
102+
return collect(new LinkedHashSet<>(this.list.size()), type);
112103
}
113104

114105
@Override public Map<String, List<String>> toMultimap() {
115106
List<String> values = new ArrayList<>();
116-
value.stream().forEach(it -> it.toMultimap().values().forEach(values::addAll));
107+
list.stream().forEach(it -> it.toMultimap().values().forEach(values::addAll));
117108
return Collections.singletonMap(name, values);
118109
}
119110

120111
@Override public List<String> toList() {
121-
return fill(new ArrayList<>());
112+
return collect(new ArrayList<>(), String.class);
122113
}
123114

124115
@Override public Set<String> toSet() {
125-
return fill(new LinkedHashSet<>());
116+
return collect(new LinkedHashSet<>(), String.class);
126117
}
127118

128-
private <C extends Collection<String>> C fill(C values) {
129-
value.forEach(v -> values.addAll(v.toList()));
130-
return values;
119+
private <T, C extends Collection<T>> C collect(C collection, Class<T> type) {
120+
for (ValueNode node : list) {
121+
collection.add(node.to(type));
122+
}
123+
return collection;
131124
}
132125
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public ByteArrayBody(Context ctx, byte[] bytes) {
5757
return value(StandardCharsets.UTF_8);
5858
}
5959

60+
@Nonnull @Override public List<String> toList() {
61+
return Collections.singletonList(value());
62+
}
63+
6064
@Nonnull @Override public ValueNode get(@Nonnull int index) {
6165
return index == 0 ? this : get(Integer.toString(index));
6266
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public FileBody(Context ctx, Path file) {
7272
return value(StandardCharsets.UTF_8);
7373
}
7474

75+
@Nonnull @Override public List<String> toList() {
76+
return Collections.singletonList(value());
77+
}
78+
7579
@Nonnull @Override public ValueNode get(@Nonnull int index) {
7680
return index == 0 ? this : get(Integer.toString(index));
7781
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ public int size() {
200200
return hash.values().iterator();
201201
}
202202

203+
@Nonnull @Override public List<String> toList() {
204+
return toList(String.class);
205+
}
206+
207+
@Nonnull @Override public Set<String> toSet() {
208+
return toSet(String.class);
209+
}
210+
203211
@Nonnull @Override public <T> List<T> toList(@Nonnull Class<T> type) {
204212
return toCollection(type, new ArrayList<>());
205213
}

0 commit comments

Comments
 (0)