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

torand / FasterSQL / 15071216480

16 May 2025 02:49PM UTC coverage: 69.877% (+2.4%) from 67.475%
15071216480

push

github

web-flow
Merge pull request #30 from torand/access-support

Access support

229 of 414 branches covered (55.31%)

Branch coverage included in aggregate %.

105 of 152 new or added lines in 26 files covered. (69.08%)

3 existing lines in 3 files now uncovered.

1193 of 1621 relevant lines covered (73.6%)

3.92 hits per line

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

14.29
/src/main/java/io/github/torand/fastersql/predicate/OptionalPredicate.java
1
/*
2
 * Copyright (c) 2024-2025 Tore Eide Andersen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.torand.fastersql.predicate;
17

18
import io.github.torand.fastersql.predicate.compound.CompoundPredicates;
19

20
import java.util.Collection;
21
import java.util.stream.Stream;
22

23
import static io.github.torand.fastersql.util.collection.CollectionHelper.streamSafely;
24
import static io.github.torand.fastersql.util.contract.Requires.precondition;
25
import static java.util.Objects.nonNull;
26
import static java.util.Objects.requireNonNull;
27

28
/**
29
 * Implements an optional predicate.
30
 * The predicate is only executed if the wrapped predicate is present.
31
 */
32
public class OptionalPredicate {
33
    private final Predicate predicate;
34

35
    /**
36
     * Creates an optional predicate wrapping specified non-null predicate.
37
     * @param predicate the predicate.
38
     * @return the optional predicate.
39
     */
40
    public static OptionalPredicate of(Predicate predicate) {
41
        requireNonNull(predicate, "Predicate not specified");
×
42
        return new OptionalPredicate(predicate);
×
43
    }
44

45
    /**
46
     * Creates an optional predicate wrapping specified null or non-null predicate.
47
     * @param predicate the predicate.
48
     * @return the optional predicate.
49
     */
50
    public static OptionalPredicate ofNullable(Predicate predicate) {
51
        return new OptionalPredicate(predicate);
5✔
52
    }
53

54
    /**
55
     * Creates an optional predicate with no wrapped predicate.
56
     * @return the optional predicate.
57
     */
58
    public static OptionalPredicate empty() {
59
        return new OptionalPredicate(null);
×
60
    }
61

62
    private OptionalPredicate(Predicate predicate) {
2✔
63
        this.predicate = predicate;
3✔
64
    }
1✔
65

66
    /**
67
     * Indicates whether there is a wrapped predicate.
68
     * @return true if there is wrapped predicate; else false.
69
     */
70
    public boolean isPresent() {
71
        return nonNull(predicate);
×
72
    }
73

74
    /**
75
     * Gets the wrapped predicate if present. Throws if not present.
76
     * @return the wrapped predicate.
77
     */
78
    public Predicate get() {
79
        precondition(() -> nonNull(predicate), "Predicate is not present");
×
80
        return predicate;
×
81
    }
82

83
    /**
84
     * Gets a stream containing the wrapped predicate, if any.
85
     * @return the stream.
86
     */
87
    public Stream<Predicate> stream() {
88
        return nonNull(predicate) ? Stream.of(predicate) : Stream.empty();
10✔
89
    }
90

91
    /**
92
     * Creates an optional compound predicate of performing the boolean operator OR on this optional predicate and the specified predicate.
93
     * @param other the other predicate.
94
     * @return the optional compound predicate.
95
     */
96
    public OptionalPredicate or(Predicate other) {
97
        requireNonNull(other, "other");
×
98

99
        if (nonNull(this.predicate)) {
×
100
            return new OptionalPredicate(CompoundPredicates.or(this.predicate, other));
×
101
        } else {
102
            return new OptionalPredicate(other);
×
103
        }
104
    }
105

106
    /**
107
     * Creates an optional compound predicate of performing the boolean operator OR on this optional predicate and the specified optional predicate.
108
     * @param other the other optional predicate.
109
     * @return the optional compound predicate.
110
     */
111
    public OptionalPredicate or(OptionalPredicate other) {
112
        if (nonNull(this.predicate) && nonNull(other.predicate)) {
×
113
            return new OptionalPredicate(CompoundPredicates.or(this.predicate, other.predicate));
×
114
        } else if (nonNull(this.predicate)) {
×
115
            return new OptionalPredicate(this.predicate);
×
116
        } else if (nonNull(other.predicate)) {
×
117
            return new OptionalPredicate(other.predicate);
×
118
        } else {
119
            return new OptionalPredicate(null);
×
120
        }
121
    }
122

123
    /**
124
     * Creates an optional compound predicate of performing the boolean operator AND on this optional predicate and the specified predicate.
125
     * @param other the other predicate.
126
     * @return the optional compound predicate.
127
     */
128
    public OptionalPredicate and(Predicate other) {
NEW
129
        requireNonNull(other, "other");
×
130

NEW
131
        if (nonNull(this.predicate)) {
×
NEW
132
            return new OptionalPredicate(CompoundPredicates.and(this.predicate, other));
×
133
        } else {
NEW
134
            return new OptionalPredicate(other);
×
135
        }
136
    }
137

138
    /**
139
     * Creates an optional compound predicate of performing the boolean operator AND on this optional predicate and the specified optional predicate.
140
     * @param other the other optional predicate.
141
     * @return the optional compound predicate.
142
     */
143
    public OptionalPredicate and(OptionalPredicate other) {
144
        if (nonNull(this.predicate) && nonNull(other.predicate)) {
×
145
            return new OptionalPredicate(CompoundPredicates.and(this.predicate, other.predicate));
×
146
        } else if (nonNull(this.predicate)) {
×
147
            return new OptionalPredicate(this.predicate);
×
148
        } else if (nonNull(other.predicate)) {
×
149
            return new OptionalPredicate(other.predicate);
×
150
        } else {
151
            return new OptionalPredicate(null);
×
152
        }
153
    }
154

155
    /**
156
     * Gets a list of present wrapped predicates from the specified array of optional predicates.
157
     * @param optionalPredicates the optional predicates-
158
     * @return the collection of present predicates.
159
     */
160
    public static Collection<Predicate> unwrap(OptionalPredicate... optionalPredicates) {
161
        return streamSafely(optionalPredicates).flatMap(OptionalPredicate::stream).toList();
6✔
162
    }
163
}
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

© 2026 Coveralls, Inc