forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleValue.java
More file actions
92 lines (70 loc) · 2.09 KB
/
SingleValue.java
File metadata and controls
92 lines (70 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal;
import io.jooby.Context;
import io.jooby.ValueNode;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
public class SingleValue implements ValueNode {
private final Context ctx;
private final String name;
private String value;
public SingleValue(Context ctx, String name, String value) {
this.ctx = ctx;
this.name = name;
this.value = value;
}
@Override public String name() {
return name;
}
@Override public ValueNode get(@Nonnull int index) {
return index == 0 ? this : get(Integer.toString(index));
}
@Override public ValueNode get(@Nonnull String name) {
return new MissingValue(this.name + "." + name);
}
@Override public int size() {
return 1;
}
@Override public String value() {
return value;
}
@Override public String toString() {
return value;
}
@Override public Iterator<ValueNode> iterator() {
return Collections.<ValueNode>singletonList(this).iterator();
}
@Nonnull @Override public <T> List<T> toList(@Nonnull Class<T> type) {
return Collections.singletonList(to(type));
}
@Nonnull @Override public <T> Set<T> toSet(@Nonnull Class<T> type) {
return Collections.singleton(to(type));
}
@Nonnull @Override public <T> Optional<T> toOptional(@Nonnull Class<T> type) {
return Optional.of(to(type));
}
@Nonnull @Override public <T> T to(@Nonnull Class<T> type) {
return ctx.convert(this, type);
}
@Override public Map<String, List<String>> toMultimap() {
return singletonMap(name, singletonList(value));
}
@Override public List<String> toList() {
return singletonList(value);
}
@Override public Set<String> toSet() {
return singleton(value);
}
}