• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

javadev / underscore-java / #3210

pending completion
#3210

push

web-flow
Bump maven-project-info-reports-plugin from 3.4.4 to 3.4.5

4359 of 4359 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/main/java/com/github/underscore/Optional.java
1
/*
2
 * The MIT License (MIT)
3
 *
4
 * Copyright 2015-2023 Valentyn Kolesnikov
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
package com.github.underscore;
25

26
import java.util.Objects;
27
import java.util.function.Function;
28
import java.util.function.Predicate;
29
import java.util.function.Supplier;
30

31
public final class Optional<T> {
32
    private static final Optional<?> EMPTY = new Optional<>();
1✔
33
    private final T value;
34
    private final boolean absent;
35

36
    private Optional() {
1✔
37
        this.value = null;
1✔
38
        this.absent = true;
1✔
39
    }
1✔
40

41
    private Optional(final T value) {
1✔
42
        this.value = value;
1✔
43
        this.absent = false;
1✔
44
    }
1✔
45

46
    public static <T> Optional<T> of(final T arg) {
47
        return new Optional<>(arg);
1✔
48
    }
49

50
    public static <T> Optional<T> fromNullable(final T nullableReference) {
51
        return nullableReference == null ? Optional.empty() : new Optional<>(nullableReference);
1✔
52
    }
53

54
    @SuppressWarnings("unchecked")
55
    public static <T> Optional<T> empty() {
56
        return (Optional<T>) EMPTY;
1✔
57
    }
58

59
    public T get() {
60
        if (absent) {
1✔
61
            throw new IllegalStateException("Optional.get() cannot be called on an empty value");
1✔
62
        }
63
        return value;
1✔
64
    }
65

66
    public T or(final T defaultValue) {
67
        if (absent) {
1✔
68
            return defaultValue;
1✔
69
        }
70
        return value;
1✔
71
    }
72

73
    public T orNull() {
74
        if (absent) {
1✔
75
            return null;
1✔
76
        }
77
        return value;
1✔
78
    }
79

80
    public boolean isEmpty() {
81
        return absent;
1✔
82
    }
83

84
    public boolean isPresent() {
85
        return !absent;
1✔
86
    }
87

88
    public Optional<T> filter(Predicate<? super T> predicate) {
89
        Underscore.checkNotNull(predicate);
1✔
90
        if (isPresent()) {
1✔
91
            return predicate.test(value) ? this : Optional.empty();
1✔
92
        } else {
93
            return this;
1✔
94
        }
95
    }
96

97
    public <F> Optional<F> map(Function<? super T, F> mapper) {
98
        Underscore.checkNotNull(mapper);
1✔
99
        if (isPresent()) {
1✔
100
            return Optional.fromNullable(mapper.apply(value));
1✔
101
        } else {
102
            return empty();
1✔
103
        }
104
    }
105

106
    public <X extends Throwable> T orThrow(Supplier<? extends X> exceptionFunction) throws X {
107
        if (absent) {
1✔
108
            throw exceptionFunction.get();
1✔
109
        } else {
110
            return value;
1✔
111
        }
112
    }
113

114
    public java.util.Optional<T> toJavaOptional() {
115
        return java.util.Optional.ofNullable(value);
1✔
116
    }
117

118
    @Override
119
    public boolean equals(final Object o) {
120
        if (this == o) {
1✔
121
            return true;
1✔
122
        }
123
        if (o == null || getClass() != o.getClass()) {
1✔
124
            return false;
1✔
125
        }
126

127
        final Optional<?> optional = (Optional<?>) o;
1✔
128

129
        return absent == optional.absent && Objects.equals(value, optional.value);
1✔
130
    }
131

132
    @Override
133
    public int hashCode() {
134
        int result = value == null ? 0 : value.hashCode();
1✔
135
        result = 31 * result + (absent ? 1 : 0);
1✔
136
        return result;
1✔
137
    }
138

139
    @Override
140
    public String toString() {
141
        return absent ? "Optional.empty" : "Optional[" + value + "]";
1✔
142
    }
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc