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

jhannes / fluent-jdbc / #214

14 Oct 2026 08:20PM UTC coverage: 91.373% (-0.3%) from 91.667%
#214

push

jhannes-test
[maven-release-plugin] prepare release fluent-jdbc-0.8.1

1218 of 1333 relevant lines covered (91.37%)

0.91 hits per line

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

37.84
/src/main/java/org/fluentjdbc/SingleRow.java
1
package org.fluentjdbc;
2

3
import javax.annotation.Nonnull;
4
import java.util.Collections;
5
import java.util.Iterator;
6
import java.util.NoSuchElementException;
7
import java.util.Optional;
8
import java.util.function.Consumer;
9
import java.util.function.Function;
10
import java.util.function.Supplier;
11
import java.util.stream.Stream;
12

13
/**
14
 * An internal alternative to {@link java.util.Optional}, but when you try to access a {@link Absent} single row
15
 * (equivalent of {@link Optional#empty()}, an exception specified at creation time is thrown. This allows
16
 * for good exception messages like "More than one row returned for query ..."
17
 */
18
public interface SingleRow<TYPE> extends Iterable<TYPE> {
19

20
    /**
21
     * Signifies that no row matched the query. If the variable is accessed, the specified exception is thrown
22
     */
23
    static <T, EX extends RuntimeException> SingleRow<T> absent(Supplier<EX> exception) {
24
        return new Absent<>(exception);
1 ✔
25
    }
26

27
    static <T> SingleRow<T> of(T result) {
28
        return new Present<>(result);
1 ✔
29
    }
30

31
    @Nonnull
32
    TYPE orElseThrow();
33

34
    boolean isPresent();
35

36
    default TYPE get() {
37
        return orElseThrow();
1 ✔
38
    }
39

40
    default boolean isEmpty() {
41
        return !isPresent();
1 ✔
42
    }
43

44
    <U> SingleRow<U> map(Function<? super TYPE, ? extends U> mapper);
45

46
    <U> SingleRow<U> flatMap(Function<? super TYPE, Optional<? extends U>> mapper);
47

48
    void ifPresent(Consumer<? super TYPE> consumer);
49

50
    void ifPresentOrElse(Consumer<? super TYPE> consumer, Runnable emptyAction);
51

52
    Stream<TYPE> stream();
53

54
    TYPE orElse(TYPE alternative);
55

56
    TYPE orElseGet(Supplier<? extends TYPE> alternative);
57

58
    class Absent<T, EX extends RuntimeException> implements SingleRow<T> {
59
        private final Supplier<EX> exception;
60

61
        public Absent(Supplier<EX> exception) {
1 ✔
62
            this.exception = exception;
1 ✔
63
        }
1 ✔
64

65
        @Override
66
        @Nonnull
67
        public Iterator<T> iterator() {
68
            return Collections.emptyIterator();
1 ✔
69
        }
70

71
        @Nonnull
72
        @Override
73
        public T orElseThrow() throws EX {
74
            throw exception.get();
×
75
        }
76

77
        @Override
78
        public boolean isPresent() {
79
            return false;
1 ✔
80
        }
81

82
        @Override
83
        public <U> SingleRow<U> map(Function<? super T, ? extends U> mapper) {
84
            return new Absent<>(exception);
×
85
        }
86

87
        @Override
88
        public <U> SingleRow<U> flatMap(Function<? super T, Optional<? extends U>> mapper) {
89
            return new Absent<>(exception);
×
90
        }
91

92
        @Override
93
        public void ifPresent(Consumer<? super T> consumer) {
94
        }
×
95

96
        @Override
97
        public void ifPresentOrElse(Consumer<? super T> consumer, Runnable emptyAction) {
98
            emptyAction.run();
×
99
        }
×
100

101
        @Override
102
        public Stream<T> stream() {
103
            return Stream.empty();
×
104
        }
105

106
        @Override
107
        public T orElse(T alternative) {
108
            return alternative;
×
109
        }
110

111
        @Override
112
        public T orElseGet(Supplier<? extends T> alternative) {
113
            return alternative.get();
×
114
        }
115

116
        @Override
117
        public String toString() {
118
            return "SingleRow.Absent{" + exception + "}";
×
119
        }
120
    }
121

122
    class Present<T> implements SingleRow<T> {
123
        private final T result;
124

125
        public Present(@Nonnull T result) {
1 ✔
126
            this.result = result;
1 ✔
127
        }
1 ✔
128

129
        @Override
130
        @Nonnull
131
        public Iterator<T> iterator() {
132
            return Collections.singletonList(result).iterator();
×
133
        }
134

135
        @Nonnull
136
        @Override
137
        public T orElseThrow() {
138
            return result;
1 ✔
139
        }
140

141
        @Override
142
        public boolean isPresent() {
143
            return true;
1 ✔
144
        }
145

146
        @Override
147
        public <U> SingleRow<U> map(Function<? super T, ? extends U> mapper) {
148
            return new Present<>(mapper.apply(result));
×
149
        }
150

151
        @Override
152
        public <U> SingleRow<U> flatMap(Function<? super T, Optional<? extends U>> mapper) {
153
            return mapper.apply(result).map(v -> (SingleRow<U>)new Present<U>(v))
×
154
                    .orElseGet(() -> new Absent<>(() -> new NoSuchElementException("No value present")));
×
155
        }
156

157
        @Override
158
        public void ifPresent(Consumer<? super T> consumer) {
159
            consumer.accept(result);
×
160
        }
×
161

162
        @Override
163
        public void ifPresentOrElse(Consumer<? super T> consumer, Runnable emptyAction) {
164
            consumer.accept(result);
×
165
        }
×
166

167
        @Override
168
        public Stream<T> stream() {
169
            return Stream.of(result);
×
170
        }
171

172
        @Override
173
        public T orElse(T alternative) {
174
            return result;
×
175
        }
176

177
        @Override
178
        public T orElseGet(Supplier<? extends T> alternative) {
179
            return result;
×
180
        }
181

182
        @Override
183
        public boolean equals(Object obj) {
184
            return obj instanceof Present && result.equals(((Present<?>) obj).result);
×
185
        }
186

187
        @Override
188
        public String toString() {
189
            return "SingleRow{" + result + "}";
×
190
        }
191
    }
192
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc