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

mybatis / mybatis-dynamic-sql / 1488

07 Mar 2025 04:31PM CUT coverage: 100.0%. Remained the same
1488

Pull #918

github

web-flow
Merge 10fe79dbe into eeedb4038
Pull Request #918: Add a Kotlin example of a custom condition

200 of 200 branches covered (100.0%)

Branch coverage included in aggregate %.

4993 of 4993 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/org/mybatis/dynamic/sql/AbstractSingleValueCondition.java
1
/*
2
 *    Copyright 2016-2025 the original author or authors.
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
 *       https://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 org.mybatis.dynamic.sql;
17

18
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19

20
import java.util.function.Function;
21
import java.util.function.Predicate;
22
import java.util.function.Supplier;
23

24
import org.mybatis.dynamic.sql.render.RenderedParameterInfo;
25
import org.mybatis.dynamic.sql.render.RenderingContext;
26
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
27

28
public abstract class AbstractSingleValueCondition<T> implements RenderableCondition<T> {
29
    protected final T value;
30

31
    protected AbstractSingleValueCondition(T value) {
1✔
32
        this.value = value;
1✔
33
    }
1✔
34

35
    public T value() {
36
        return value;
1✔
37
    }
38

39
    protected <S extends AbstractSingleValueCondition<T>> S filterSupport(Predicate<? super T> predicate,
40
            Supplier<S> emptySupplier, S self) {
41
        if (isEmpty()) {
1✔
42
            return self;
1✔
43
        } else {
44
            return predicate.test(value) ? self : emptySupplier.get();
1✔
45
        }
46
    }
47

48
    protected <R, S extends AbstractSingleValueCondition<R>> S mapSupport(Function<? super T, ? extends R> mapper,
49
            Function<R, S> constructor, Supplier<S> emptySupplier) {
50
        if (isEmpty()) {
1✔
51
            return emptySupplier.get();
1✔
52
        } else {
53
            return constructor.apply(mapper.apply(value));
1✔
54
        }
55
    }
56

57
    /**
58
     * If renderable and the value matches the predicate, returns this condition. Else returns a condition
59
     *     that will not render.
60
     *
61
     * @param predicate predicate applied to the value, if renderable
62
     * @return this condition if renderable and the value matches the predicate, otherwise a condition
63
     *     that will not render.
64
     */
65
    public abstract AbstractSingleValueCondition<T> filter(Predicate<? super T> predicate);
66

67
    public abstract String operator();
68

69
    @Override
70
    public FragmentAndParameters renderCondition(RenderingContext renderingContext, BindableColumn<T> leftColumn) {
71
        RenderedParameterInfo parameterInfo = renderingContext.calculateParameterInfo(leftColumn);
1✔
72
        String finalFragment = operator() + spaceBefore(parameterInfo.renderedPlaceHolder());
1✔
73

74
        return FragmentAndParameters.withFragment(finalFragment)
1✔
75
                .withParameter(parameterInfo.parameterMapKey(), leftColumn.convertParameterType(value()))
1✔
76
                .build();
1✔
77
    }
78
}
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