-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathJsonNullable.java
More file actions
161 lines (136 loc) · 4.44 KB
/
JsonNullable.java
File metadata and controls
161 lines (136 loc) · 4.44 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
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;
public class JsonNullable<T> implements Serializable {
private static final long serialVersionUID = 1L;
private static final JsonNullable<?> UNDEFINED = new JsonNullable<>(null, false);
private static final JsonNullable<?> NULL = new JsonNullable<>(null, true);
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;
}
@SuppressWarnings("unchecked")
public static <T> JsonNullable<T> ofNull() {
return (JsonNullable<T>) NULL;
}
/**
* 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);
}
/**
* Returns wrapper of either UNDEFINED or NULL state
*
* @param value the value
* @param <T> type of value inside
* @return <code>JsonNullable</code> in UNDEFINED or NULL state
*/
public static <T> JsonNullable<T> ofMissable(T value) {
return new JsonNullable<>(value, value != null);
}
public <R> JsonNullable<R> flatMap(Function<? super T, ? extends JsonNullable<R>> mapper) {
if (isNonNull()) {
return mapper.apply(value);
}
return isNull() ? JsonNullable.ofNull() : JsonNullable.undefined();
}
public <R> JsonNullable<R> map(Function<? super T, ? extends R> mapper) {
if (isNonNull()) {
return JsonNullable.ofMissable(mapper.apply(value));
}
return isNull() ? JsonNullable.ofNull() : JsonNullable.undefined();
}
/**
* 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;
}
public boolean isPresent() {
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
*/
public void ifPresent(
Consumer<? super T> action) {
if (this.isPresent) {
action.accept(value);
}
}
public boolean isNull() {
return isPresent && value == null;
}
public boolean isNonNull() {
return isPresent && value != null;
}
/**
* If non-null value is present, performs an action with the value
*
* @param action The action performed with value
*/
public void ifNotNull(Consumer<T> action) {
if (isNonNull()) {
action.accept(value);
}
}
@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";
}
}