1
1
package dev .openfeature .javasdk ;
2
2
3
- import java .time .ZonedDateTime ;
3
+ import java .time .Instant ;
4
4
import java .util .List ;
5
5
6
6
import lombok .EqualsAndHashCode ;
7
7
import lombok .ToString ;
8
8
9
9
/**
10
- * Values serve as a return type for provider objects .
10
+ * Values serve as a generic return type for structure data from providers .
11
11
* Providers may deal in JSON, protobuf, XML or some other data-interchange format.
12
12
* This intermediate representation provides a good medium of exchange.
13
13
*/
@@ -19,7 +19,27 @@ public class Value {
19
19
private final Object innerObject ;
20
20
21
21
public Value () {
22
- this .innerObject = null ;
22
+ this .innerObject = null ;
23
+ }
24
+
25
+ /**
26
+ * Construct a new Value with an Object.
27
+ * @param value to be wrapped.
28
+ * @throws InstantiationException if value is not a valid type
29
+ * (boolean, string, int, double, list, structure, instant)
30
+ */
31
+ public Value (Object value ) throws InstantiationException {
32
+ // integer is a special case, convert those.
33
+ this .innerObject = value instanceof Integer ? ((Integer )value ).doubleValue () : value ;
34
+ if (!this .isNull ()
35
+ && !this .isBoolean ()
36
+ && !this .isString ()
37
+ && !this .isNumber ()
38
+ && !this .isStructure ()
39
+ && !this .isList ()
40
+ && !this .isInstant ()) {
41
+ throw new InstantiationException ("Invalid value type: " + value .getClass ());
42
+ }
23
43
}
24
44
25
45
public Value (Value value ) {
@@ -50,7 +70,7 @@ public Value(List<Value> value) {
50
70
this .innerObject = value ;
51
71
}
52
72
53
- public Value (ZonedDateTime value ) {
73
+ public Value (Instant value ) {
54
74
this .innerObject = value ;
55
75
}
56
76
@@ -109,12 +129,12 @@ public boolean isList() {
109
129
}
110
130
111
131
/**
112
- * Check if this Value represents a ZonedDateTime .
132
+ * Check if this Value represents an Instant .
113
133
*
114
134
* @return boolean
115
135
*/
116
- public boolean isZonedDateTime () {
117
- return this .innerObject instanceof ZonedDateTime ;
136
+ public boolean isInstant () {
137
+ return this .innerObject instanceof Instant ;
118
138
}
119
139
120
140
/**
@@ -131,6 +151,15 @@ public Boolean asBoolean() {
131
151
return null ;
132
152
}
133
153
154
+ /**
155
+ * Retrieve the underlying object.
156
+ *
157
+ * @return Object
158
+ */
159
+ public Object asObject () {
160
+ return this .innerObject ;
161
+ }
162
+
134
163
/**
135
164
* Retrieve the underlying String value, or null.
136
165
*
@@ -194,13 +223,13 @@ public List<Value> asList() {
194
223
}
195
224
196
225
/**
197
- * Retrieve the underlying ZonedDateTime value, or null.
226
+ * Retrieve the underlying Instant value, or null.
198
227
*
199
- * @return ZonedDateTime
228
+ * @return Instant
200
229
*/
201
- public ZonedDateTime asZonedDateTime () {
202
- if (this .isZonedDateTime ()) {
203
- return (ZonedDateTime )this .innerObject ;
230
+ public Instant asInstant () {
231
+ if (this .isInstant ()) {
232
+ return (Instant )this .innerObject ;
204
233
}
205
234
return null ;
206
235
}
0 commit comments