• 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/AbstractTwoValueCondition.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.BiFunction;
21
import java.util.function.BiPredicate;
22
import java.util.function.Function;
23
import java.util.function.Predicate;
24
import java.util.function.Supplier;
25

26
import org.mybatis.dynamic.sql.render.RenderedParameterInfo;
27
import org.mybatis.dynamic.sql.render.RenderingContext;
28
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
29

30
public abstract class AbstractTwoValueCondition<T> implements RenderableCondition<T> {
31
    protected final T value1;
32
    protected final T value2;
33

34
    protected AbstractTwoValueCondition(T value1, T value2) {
1✔
35
        this.value1 = value1;
1✔
36
        this.value2 = value2;
1✔
37
    }
1✔
38

39
    public T value1() {
40
        return value1;
1✔
41
    }
42

43
    public T value2() {
44
        return value2;
1✔
45
    }
46

47
    protected <S extends AbstractTwoValueCondition<T>> S filterSupport(BiPredicate<? super T, ? super T> predicate,
48
            Supplier<S> emptySupplier, S self) {
49
        if (isEmpty()) {
1✔
50
            return self;
1✔
51
        } else {
52
            return predicate.test(value1, value2) ? self : emptySupplier.get();
1✔
53
        }
54
    }
55

56
    protected <S extends AbstractTwoValueCondition<T>> S filterSupport(Predicate<? super T> predicate,
57
            Supplier<S> emptySupplier, S self) {
58
        return filterSupport((v1, v2) -> predicate.test(v1) && predicate.test(v2), emptySupplier, self);
1✔
59
    }
60

61
    protected <R, S extends AbstractTwoValueCondition<R>> S mapSupport(Function<? super T, ? extends R> mapper1,
62
            Function<? super T, ? extends R> mapper2, BiFunction<R, R, S> constructor, Supplier<S> emptySupplier) {
63
        if (isEmpty()) {
1✔
64
            return emptySupplier.get();
1✔
65
        } else {
66
            return constructor.apply(mapper1.apply(value1), mapper2.apply(value2));
1✔
67
        }
68
    }
69

70
    /**
71
     * If renderable and the values match the predicate, returns this condition. Else returns a condition
72
     *     that will not render.
73
     *
74
     * @param predicate predicate applied to the values, if renderable
75
     * @return this condition if renderable and the values match the predicate, otherwise a condition
76
     *     that will not render.
77
     */
78
    public abstract AbstractTwoValueCondition<T> filter(BiPredicate<? super T, ? super T> predicate);
79

80
    /**
81
     * If renderable and both values match the predicate, returns this condition. Else returns a condition
82
     *     that will not render. This function implements a short-circuiting test. If the
83
     *     first value does not match the predicate, then the second value will not be tested.
84
     *
85
     * @param predicate predicate applied to both values, if renderable
86
     * @return this condition if renderable and the values match the predicate, otherwise a condition
87
     *     that will not render.
88
     */
89
    public abstract AbstractTwoValueCondition<T> filter(Predicate<? super T> predicate);
90

91
    public abstract String operator1();
92

93
    public abstract String operator2();
94

95
    @Override
96
    public FragmentAndParameters renderCondition(RenderingContext renderingContext, BindableColumn<T> leftColumn) {
97
        RenderedParameterInfo parameterInfo1 = renderingContext.calculateParameterInfo(leftColumn);
1✔
98
        RenderedParameterInfo parameterInfo2 = renderingContext.calculateParameterInfo(leftColumn);
1✔
99

100
        String finalFragment = operator1()
1✔
101
                + spaceBefore(parameterInfo1.renderedPlaceHolder())
1✔
102
                + spaceBefore(operator2())
1✔
103
                + spaceBefore(parameterInfo2.renderedPlaceHolder());
1✔
104

105
        return FragmentAndParameters.withFragment(finalFragment)
1✔
106
                .withParameter(parameterInfo1.parameterMapKey(), leftColumn.convertParameterType(value1()))
1✔
107
                .withParameter(parameterInfo2.parameterMapKey(), leftColumn.convertParameterType(value2()))
1✔
108
                .build();
1✔
109
    }
110
}
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