-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathJsonNullable.java
More file actions
333 lines (303 loc) · 10.5 KB
/
JsonNullable.java
File metadata and controls
333 lines (303 loc) · 10.5 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package org.openapitools.jackson.nullable;
import java.io.Serializable;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
public class JsonNullable<T> implements Serializable {
private static final long serialVersionUID = 1L;
private static final JsonNullable<?> UNDEFINED = new JsonNullable<>(null, false);
private final T value;
private final boolean isPresent;
private JsonNullable(T value, boolean isPresent) {
this.value = value;
this.isPresent = isPresent;
}
/**
* Create a <code>JsonNullable</code> representing an undefined value (not present).
*
* @param <T> a type wildcard
* @return an empty <code>JsonNullable</code> with no value defined
*/
public static <T> JsonNullable<T> undefined() {
@SuppressWarnings("unchecked")
JsonNullable<T> t = (JsonNullable<T>) UNDEFINED;
return t;
}
/**
* Create a <code>JsonNullable</code> from the submitted value.
*
* @param value the value
* @param <T> the type of the value
* @return the <code>JsonNullable</code> with the submitted value present.
*/
public static <T> JsonNullable<T> of(T value) {
return new JsonNullable<>(value, true);
}
/**
* Obtain the value of this <code>JsonNullable</code>.
*
* @return the value, if present
* @throws NoSuchElementException if no value is present
*/
public T get() {
if (!isPresent) {
throw new NoSuchElementException("Value is undefined");
}
return value;
}
/**
* Obtain the value of this <code>JsonNullable</code>.
*
* @param other the value to be returned if no value is present
* @return the value of this <code>JsonNullable</code> if present, the submitted value otherwise
*/
public T orElse(T other) {
return this.isPresent ? this.value : other;
}
/**
* If a value is present, returns the value, otherwise returns the result
* produced by the supplying function.
*
* @param supplier the supplying function that produces a value to be returned
* @return the value, if present, otherwise the result produced by the supplying function
* @throws NullPointerException if no value is present and the supplying function is null
*
* @since 0.2.8
*/
public T orElseGet(Supplier<? extends T> supplier) {
return this.isPresent ? this.value : supplier.get();
}
/**
* If a value is present, returns the value, otherwise throws
* NoSuchElementException.
*
* @return the value of this JsonNullable
* @throws NoSuchElementException if no value if present
*
* @since 0.2.8
*/
public T orElseThrow() {
if (!isPresent) {
throw new NoSuchElementException("Value is undefined");
}
return value;
}
/**
* If a value is present, returns the value, otherwise throws an exception
* produced by the exception supplying function.
*
* @param <X> type of the exception to be thrown
* @param supplier the supplying function that produces an exception to be
* thrown
* @return the value, if present
* @throws X if no value is present
* @throws NullPointerException if no value is present and the exception
* supplying function is {@code null}
*
* @since 0.2.8
*/
public <X extends Throwable> T orElseThrow(Supplier<? extends X> supplier)
throws X
{
if( this.isPresent ) {
return this.value;
}
throw supplier.get();
}
/**
* If a value is present, returns true, otherwise false.
*
* @return true if a value is present, otherwise false
*/
public boolean isPresent() {
return isPresent;
}
/**
* If a value is not present, returns true, otherwise false.
*
* @return true if a value is not present, otherwise false
*
* @since 0.2.8
*/
public boolean isUndefined() {
return !isPresent;
}
/**
* If a value is present, performs the given action with the value,
* otherwise does nothing.
*
* @param action the action to be performed, if a value is present
* @throws NullPointerException if a value is present and the given action
* is null
*/
public void ifPresent(
Consumer<? super T> action) {
if (this.isPresent) {
action.accept(value);
}
}
/**
* If a value is present, performs the given action with the value,
* otherwise performs the given empty-based action.
*
* @param action the action to be performed, if a value is present
* @param undefinedAction the empty-based action to be performed, if no
* value is present
* @throws NullPointerException if a value is present and the given action
* is null, or no value is present and the given empty-based action
* is null
*
* @since 0.2.8
*/
public void ifPresentOrElse( Consumer<? super T> action, Runnable undefinedAction ) {
if (this.isPresent) {
action.accept(value);
}
else {
undefinedAction.run();
}
}
/**
* If a value is present, and the value matches the given predicate, returns
* a JsonNullable describing the value, otherwise returns an undefined
* JsonNullable.
*
* @param predicate the predicate to apply to a value, if present
* @return a JsonNullable describing the value of this JsonNullable,
* if a value is present and the value matches the given predicate,
* otherwise an undefined JsonNullable
* @throws NullPointerException if the predicate is null
*
* @since 0.2.8
*/
public JsonNullable<T> filter( Predicate<T> predicate ) {
if (predicate == null) {
throw new NullPointerException("filter predicate is null");
}
if (this.isPresent && predicate.test(value)) {
return this;
}
else {
return undefined();
}
}
/**
* If a value is present, returns a JsonNullable describing the result of
* applying the given mapping function to the value, otherwise returns an
* undefined JsonNullable.
*
* @param <U> the type of the value returned from the mapping function
* @param mapper the mapping function to apply to a value, if present
* @return a JsonNullable describing the result of applying a mapping
* function to the value of this JsonNullable, if a value is
* present, otherwise an undefined JsonNullable
* @throws NullPointerException if the mapping function is null
*
* @since 0.2.8
*/
public <U> JsonNullable<U> map( Function<T, U> mapper) {
if (mapper == null) {
throw new NullPointerException("mapping function is null");
}
if (this.isPresent) {
return new JsonNullable<U>(mapper.apply(value), true);
}
return undefined();
}
/**
* If a value is present, returns the result of applying the given
* JsonNullable-bearing mapping function to the value, otherwise returns an
* undefined JsonNullable.
*
* @param <U> the type of value of the JsonNullable returned by the mapping
* function
* @param mapper the mapping function to apply to a value, if present
* @return the result of applying a JsonNullable-bearing mapping function to
* the value of this JsonNullable, if a value is present, otherwise
* an undefined JsonNullable
* @throws NullPointerException if the mapping function is null or returns a
* null result
*
* @since 0.2.8
*/
@SuppressWarnings("unchecked")
public <U> JsonNullable<U> flatMap( Function<? super T, ? extends JsonNullable<? extends U>> mapper ) {
if (mapper == null) {
throw new NullPointerException("mapping function is null");
}
if (!this.isPresent) {
return undefined();
}
JsonNullable<U> mapped = (JsonNullable<U>)mapper.apply(value);
if (mapped == null) {
throw new NullPointerException("mapped value is null");
}
return mapped;
}
/**
* If a value is present, returns a JsonNullable describing the value,
* otherwise returns a JsonNullable produced by the supplying function.
*
* @param supplier the supplying function that produces a JsonNullable to be
* returned
* @return returns a JsonNullable describing the value of this JsonNullable,
* if a value is present, otherwise a JsonNullable produced by the
* supplying function.
* @throws NullPointerException if the supplying function is null or
* produces a null result
*
* @since 0.2.8
*/
@SuppressWarnings("unchecked")
public JsonNullable<T> or( Supplier<? extends JsonNullable<? extends T>> supplier ) {
if( supplier == null ) {
throw new NullPointerException("or supplier is null");
}
if (this.isPresent) {
return this;
}
JsonNullable<T> supplied = (JsonNullable<T>)supplier.get();
if (supplied == null) {
throw new NullPointerException("supplied value is null");
}
return supplied;
}
/**
* If a value is present, returns a sequential Stream containing only that
* value, otherwise returns an empty Stream.
*
* @return the JsonNullable value as a Stream
*
* @since 0.2.8
*/
public Stream<T> stream() {
if (this.isPresent) {
return Stream.of(value);
}
return Stream.empty();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof JsonNullable)) {
return false;
}
JsonNullable<?> other = (JsonNullable<?>) obj;
return Objects.equals(value, other.value) &&
isPresent == other.isPresent;
}
@Override
public int hashCode() {
return Objects.hash(value, isPresent);
}
@Override
public String toString() {
return this.isPresent ? String.format("JsonNullable[%s]", value) : "JsonNullable.undefined";
}
}