forked from socketio/socket.io-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptional.java
More file actions
44 lines (33 loc) · 922 Bytes
/
Copy pathOptional.java
File metadata and controls
44 lines (33 loc) · 922 Bytes
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
package io.socket.util;
import java.util.NoSuchElementException;
public class Optional<T> {
static final Optional<Void> EMPTY = Optional.ofNullable(null);
private T value;
public static <T> Optional<T> of(T value) {
if (value == null) {
throw new NullPointerException();
}
return new Optional<T>(value);
}
public static <T> Optional<T> ofNullable(T value) {
return new Optional<T>(value);
}
public static Optional<Void> empty() {
return EMPTY;
}
private Optional(T value) {
this.value = value;
}
public boolean isPresent() {
return this.value != null;
}
public T get() {
if (this.value == null) {
throw new NoSuchElementException();
}
return this.value;
}
public T orElse(T other) {
return this.value != null ? this.value : other;
}
}