Skip to content

Commit 339e216

Browse files
committed
Value API: move more methods to Value base class
1 parent 67a14d3 commit 339e216

2 files changed

Lines changed: 96 additions & 122 deletions

File tree

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

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@
2020
import java.util.Set;
2121
import java.util.function.Function;
2222

23+
/**
24+
* Unified API for HTTP value. This API plays two role:
25+
*
26+
* - unify access to HTTP values, like query, path, form and header parameter
27+
* - works as validation API, because it is able to check for required and type-safe values
28+
*
29+
* The value API is composed by three types:
30+
*
31+
* - Single value
32+
* - Object value
33+
* - Sequence of values (array)
34+
*
35+
* Single value are can be converted to string, int, boolean, enum like values.
36+
* Object value is a key:value structure (like a hash).
37+
* Sequence of values are index based structure.
38+
*
39+
* All these 3 types are modeled into a single Value class. At any time you can treat a value as
40+
* 1) single, 2) hash or 3) array of them.
41+
*
42+
* @since 2.0.0
43+
* @author edgar
44+
*/
2345
public interface Value {
2446
/**
2547
* Convert this value to long (if possible).
@@ -299,13 +321,87 @@ default boolean isMissing() {
299321
return this instanceof MissingValue;
300322
}
301323

324+
/**
325+
* True if this value is an array/sequence (not single or hash).
326+
*
327+
* @return True if this value is an array/sequence.
328+
*/
329+
default boolean isArray() {
330+
return this instanceof ArrayValue;
331+
}
332+
333+
/**
334+
* True if this is a hash/object value (not single or array).
335+
*
336+
* @return True if this is a hash/object value (not single or array).
337+
*/
338+
default boolean isObject() {
339+
return this instanceof HashValue;
340+
}
341+
342+
/**
343+
* True if this is a file upload (not single, not array, not hash).
344+
*
345+
* @return True for file upload.
346+
*/
347+
default boolean isUpload() {
348+
return this instanceof FileUpload;
349+
}
350+
302351
/**
303352
* Name of this value or <code>null</code>.
304353
*
305354
* @return Name of this value or <code>null</code>.
306355
*/
307356
@Nullable String name();
308357

358+
/**
359+
* Get a value or empty optional.
360+
*
361+
* @param type Item type.
362+
* @param <T> Item type.
363+
* @return Value or empty optional.
364+
*/
365+
@Nonnull default <T> Optional<T> toOptional(@Nonnull Class<T> type) {
366+
try {
367+
return Optional.ofNullable(to(type));
368+
} catch (MissingValueException x) {
369+
return Optional.empty();
370+
}
371+
}
372+
373+
/**
374+
* Get list of the given type.
375+
*
376+
* @param type Type to convert.
377+
* @param <T> Item type.
378+
* @return List of items.
379+
*/
380+
@Nonnull default <T> List<T> toList(@Nonnull Class<T> type) {
381+
return Collections.singletonList(to(type));
382+
}
383+
384+
/**
385+
* Get set of the given type.
386+
*
387+
* @param type Type to convert.
388+
* @param <T> Item type.
389+
* @return Set of items.
390+
*/
391+
@Nonnull default <T> Set<T> toSet(@Nonnull Class<T> type) {
392+
return Collections.singleton(to(type));
393+
}
394+
395+
/**
396+
* Convert this value to the given type. Support values are single-value, array-value and
397+
* object-value. Object-value can be converted to a JavaBean type.
398+
*
399+
* @param type Type to convert.
400+
* @param <T> Element type.
401+
* @return Instance of the type.
402+
*/
403+
@Nonnull <T> T to(@Nonnull Class<T> type);
404+
309405
/**
310406
* Value as multi-value map.
311407
*
@@ -416,5 +512,4 @@ default boolean isMissing() {
416512
static @Nonnull ValueNode hash(Context ctx, @Nonnull Map<String, Collection<String>> values) {
417513
return new HashValue(ctx, null).put(values);
418514
}
419-
420515
}

jooby/src/main/java/io/jooby/ValueNode.java

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,11 @@
55
*/
66
package io.jooby;
77

8-
import io.jooby.internal.ArrayValue;
9-
import io.jooby.internal.HashValue;
10-
import io.jooby.internal.MissingValue;
11-
import io.jooby.internal.SingleValue;
12-
138
import javax.annotation.Nonnull;
14-
import javax.annotation.Nullable;
15-
import java.time.Instant;
16-
import java.time.LocalDateTime;
17-
import java.time.ZoneOffset;
18-
import java.time.format.DateTimeParseException;
19-
import java.util.Collection;
209
import java.util.Collections;
2110
import java.util.Iterator;
22-
import java.util.LinkedHashMap;
23-
import java.util.List;
24-
import java.util.Map;
2511
import java.util.NoSuchElementException;
26-
import java.util.Optional;
27-
import java.util.Set;
2812
import java.util.function.BiFunction;
29-
import java.util.function.Function;
3013

3114
/**
3215
* Unified API for HTTP value. This API plays two role:
@@ -52,38 +35,6 @@
5235
*/
5336
public interface ValueNode extends Iterable<ValueNode>, Value {
5437

55-
/**
56-
* True if this value is an array/sequence (not single or hash).
57-
*
58-
* @return True if this value is an array/sequence.
59-
*/
60-
default boolean isArray() {
61-
return this instanceof ArrayValue;
62-
}
63-
64-
/**
65-
* True if this is a hash/object value (not single or array).
66-
*
67-
* @return True if this is a hash/object value (not single or array).
68-
*/
69-
default boolean isObject() {
70-
return this instanceof HashValue;
71-
}
72-
73-
/**
74-
* True if this is a file upload (not single, not array, not hash).
75-
*
76-
* @return True for file upload.
77-
*/
78-
default boolean isUpload() {
79-
return this instanceof FileUpload;
80-
}
81-
82-
/* ***********************************************************************************************
83-
* Node methods
84-
* ***********************************************************************************************
85-
*/
86-
8738
/**
8839
* Get a value at the given position.
8940
*
@@ -109,53 +60,6 @@ default int size() {
10960
return 0;
11061
}
11162

112-
/**
113-
* Get a value or empty optional.
114-
*
115-
* @param type Item type.
116-
* @param <T> Item type.
117-
* @return Value or empty optional.
118-
*/
119-
@Nonnull default <T> Optional<T> toOptional(@Nonnull Class<T> type) {
120-
try {
121-
return Optional.ofNullable(to(type));
122-
} catch (MissingValueException x) {
123-
return Optional.empty();
124-
}
125-
}
126-
127-
/**
128-
* Get list of the given type.
129-
*
130-
* @param type Type to convert.
131-
* @param <T> Item type.
132-
* @return List of items.
133-
*/
134-
@Nonnull default <T> List<T> toList(@Nonnull Class<T> type) {
135-
return Collections.singletonList(to(type));
136-
}
137-
138-
/**
139-
* Get set of the given type.
140-
*
141-
* @param type Type to convert.
142-
* @param <T> Item type.
143-
* @return Set of items.
144-
*/
145-
@Nonnull default <T> Set<T> toSet(@Nonnull Class<T> type) {
146-
return Collections.singleton(to(type));
147-
}
148-
149-
/**
150-
* Convert this value to the given type. Support values are single-value, array-value and
151-
* object-value. Object-value can be converted to a JavaBean type.
152-
*
153-
* @param type Type to convert.
154-
* @param <T> Element type.
155-
* @return Instance of the type.
156-
*/
157-
@Nonnull <T> T to(@Nonnull Class<T> type);
158-
15963
/**
16064
* Value iterator.
16165
*
@@ -165,31 +69,6 @@ default int size() {
16569
return Collections.emptyIterator();
16670
}
16771

168-
/**
169-
* Name of this value or <code>null</code>.
170-
*
171-
* @return Name of this value or <code>null</code>.
172-
*/
173-
@Nullable String name();
174-
175-
/**
176-
* Value as multi-value map.
177-
*
178-
* @return Value as multi-value map.
179-
*/
180-
@Nullable Map<String, List<String>> toMultimap();
181-
182-
/**
183-
* Value as single-value map.
184-
*
185-
* @return Value as single-value map.
186-
*/
187-
default @Nonnull Map<String, String> toMap() {
188-
Map<String, String> map = new LinkedHashMap<>();
189-
toMultimap().forEach((k, v) -> map.put(k, v.get(0)));
190-
return map;
191-
}
192-
19372
/**
19473
* Process the given expression and resolve value references.
19574
*

0 commit comments

Comments
 (0)