-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbsent.java
More file actions
118 lines (90 loc) · 2.96 KB
/
Absent.java
File metadata and controls
118 lines (90 loc) · 2.96 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
package me.sparky983.helios;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.jspecify.annotations.Nullable;
/**
* An {@code Optional} that contains no value.
*
* @param <T> the type of the value
* @since 0.1.0
* @helios.apiNote This class should not be constructed directly. Use {@link #absent()} instead.
*/
public record Absent<T>() implements Optional<T> {
static final Absent<?> ABSENT = new Absent<>();
@Override
public boolean isPresent() {
return false;
}
@Override
public boolean isAbsent() {
return true;
}
@SuppressWarnings("unchecked")
@Override
public Optional<T> or(final Optional<? extends T> other) {
Objects.requireNonNull(other, "other cannot be null");
return (Optional<T>) other;
}
@Override
public Optional<T> or(final Supplier<? extends Optional<? extends T>> otherSupplier) {
Objects.requireNonNull(otherSupplier, "otherSupplier cannot be null");
final var other =
Objects.requireNonNull(otherSupplier.get(), "otherSupplier cannot return null");
return or(other);
}
@Override
public T orDefault(final T defaultValue) {
Objects.requireNonNull(defaultValue, "defaultValue cannot be null");
return defaultValue;
}
@Override
public T orGet(final Supplier<? extends T> defaultValueSupplier) {
Objects.requireNonNull(defaultValueSupplier, "defaultValueSupplier cannot be null");
final var defaultValue = defaultValueSupplier.get();
Objects.requireNonNull(defaultValue, "defaultValueSupplier cannot return null");
return defaultValue;
}
@Override
public @Nullable T orNull() {
return null;
}
@Override
public <E extends Throwable> T orThrow(final Supplier<? extends E> exceptionSupplier) throws E {
Objects.requireNonNull(exceptionSupplier, "exceptionSupplier cannot be null");
final var exception = exceptionSupplier.get();
Objects.requireNonNull(exception, "exceptionSupplier cannot return null");
throw exception;
}
@Override
public T expect(final String message) {
Objects.requireNonNull(message, "message cannot be null");
throw new NoSuchElementException("Expected " + message);
}
@Override
public <M> Optional<M> map(final Function<? super T, ? extends M> mapper) {
Objects.requireNonNull(mapper, "mapper cannot be null");
return Optional.absent();
}
@Override
public <M> Optional<M> flatMap(
final Function<? super T, ? extends Optional<? extends M>> mapper) {
Objects.requireNonNull(mapper, "mapper cannot be null");
return Optional.absent();
}
@Override
public Optional<T> filter(final Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate cannot be null");
return Optional.absent();
}
@Override
public int hashCode() {
return 0;
}
@Override
public String toString() {
return "Absent()";
}
}