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

jhannes / fluent-jdbc / #186

10 Oct 2024 02:37PM UTC coverage: 92.567% (-0.4%) from 93.009%
#186

push

jhannes
[maven-release-plugin] prepare release fluent-jdbc-0.5.3

1183 of 1278 relevant lines covered (92.57%)

0.93 hits per line

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

41.18
/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.Optional;
7
import java.util.function.Consumer;
8
import java.util.function.Function;
9
import java.util.function.Supplier;
10
import java.util.stream.Stream;
11

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

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

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

30
    @Nonnull
31
    TYPE orElseThrow();
32

33
    boolean isPresent();
34

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

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

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

45
    void ifPresent(Consumer<? super TYPE> consumer);
46

47
    void ifPresentOrElse(Consumer<? super TYPE> consumer, Runnable emptyAction);
48

49
    Stream<TYPE> stream();
50

51
    TYPE orElse(TYPE alternative);
52

53
    TYPE orElseGet(Supplier<? extends TYPE> alternative);
54

55
    class Absent<T, EX extends RuntimeException> implements SingleRow<T> {
56
        private final Supplier<EX> exception;
57

58
        public Absent(Supplier<EX> exception) {
1✔
59
            this.exception = exception;
1✔
60
        }
1✔
61

62
        @Override
63
        @Nonnull
64
        public Iterator<T> iterator() {
65
            return Collections.emptyIterator();
1✔
66
        }
67

68
        @Nonnull
69
        @Override
70
        public T orElseThrow() throws EX {
71
            throw exception.get();
×
72
        }
73

74
        @Override
75
        public boolean isPresent() {
76
            return false;
1✔
77
        }
78

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

84
        @Override
85
        public void ifPresent(Consumer<? super T> consumer) {
86
        }
×
87

88
        @Override
89
        public void ifPresentOrElse(Consumer<? super T> consumer, Runnable emptyAction) {
90
            emptyAction.run();
×
91
        }
×
92

93
        @Override
94
        public Stream<T> stream() {
95
            return Stream.empty();
×
96
        }
97

98
        @Override
99
        public T orElse(T alternative) {
100
            return alternative;
×
101
        }
102

103
        @Override
104
        public T orElseGet(Supplier<? extends T> alternative) {
105
            return alternative.get();
×
106
        }
107

108
        @Override
109
        public String toString() {
110
            return "SingleRow.Absent{" + exception + "}";
×
111
        }
112
    }
113

114
    class Present<T> implements SingleRow<T> {
115
        private final T result;
116

117
        public Present(@Nonnull T result) {
1✔
118
            this.result = result;
1✔
119
        }
1✔
120

121
        @Override
122
        @Nonnull
123
        public Iterator<T> iterator() {
124
            return Collections.singletonList(result).iterator();
×
125
        }
126

127
        @Nonnull
128
        @Override
129
        public T orElseThrow() {
130
            return result;
1✔
131
        }
132

133
        @Override
134
        public boolean isPresent() {
135
            return true;
1✔
136
        }
137

138
        @Override
139
        public <U> SingleRow<U> map(Function<? super T, ? extends U> mapper) {
140
            return new Present<>(mapper.apply(result));
×
141
        }
142

143
        @Override
144
        public void ifPresent(Consumer<? super T> consumer) {
145
            consumer.accept(result);
×
146
        }
×
147

148
        @Override
149
        public void ifPresentOrElse(Consumer<? super T> consumer, Runnable emptyAction) {
150
            consumer.accept(result);
×
151
        }
×
152

153
        @Override
154
        public Stream<T> stream() {
155
            return Stream.of(result);
×
156
        }
157

158
        @Override
159
        public T orElse(T alternative) {
160
            return result;
×
161
        }
162

163
        @Override
164
        public T orElseGet(Supplier<? extends T> alternative) {
165
            return result;
×
166
        }
167

168
        @Override
169
        public boolean equals(Object obj) {
170
            return obj instanceof Present && result.equals(((Present<?>) obj).result);
×
171
        }
172

173
        @Override
174
        public String toString() {
175
            return "SingleRow{" + result + "}";
×
176
        }
177
    }
178
}
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