• 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/AbstractListValueCondition.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 java.util.Collection;
19
import java.util.Objects;
20
import java.util.function.Function;
21
import java.util.function.Predicate;
22
import java.util.function.Supplier;
23
import java.util.stream.Collectors;
24
import java.util.stream.Stream;
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
import org.mybatis.dynamic.sql.util.FragmentCollector;
30

31
public abstract class AbstractListValueCondition<T> implements RenderableCondition<T> {
32
    protected final Collection<T> values;
33

34
    protected AbstractListValueCondition(Collection<T> values) {
1✔
35
        this.values = Objects.requireNonNull(values);
1✔
36
    }
1✔
37

38
    public final Stream<T> values() {
39
        return values.stream();
1✔
40
    }
41

42
    @Override
43
    public boolean isEmpty() {
44
        return values.isEmpty();
1✔
45
    }
46

47
    private <R> Collection<R> applyMapper(Function<? super T, ? extends R> mapper) {
48
        Objects.requireNonNull(mapper);
1✔
49
        return values().map(mapper).collect(Collectors.toList());
1✔
50
    }
51

52
    private Collection<T> applyFilter(Predicate<? super T> predicate) {
53
        Objects.requireNonNull(predicate);
1✔
54
        return values().filter(predicate).toList();
1✔
55
    }
56

57
    protected <S extends AbstractListValueCondition<T>> S filterSupport(Predicate<? super T> predicate,
58
            Function<Collection<T>, S> constructor, S self, Supplier<S> emptySupplier) {
59
        if (isEmpty()) {
1✔
60
            return self;
1✔
61
        } else {
62
            Collection<T> filtered = applyFilter(predicate);
1✔
63
            return filtered.isEmpty() ? emptySupplier.get() : constructor.apply(filtered);
1✔
64
        }
65
    }
66

67
    protected <R, S extends AbstractListValueCondition<R>> S mapSupport(Function<? super T, ? extends R> mapper,
68
            Function<Collection<R>, S> constructor, Supplier<S> emptySupplier) {
69
        if (isEmpty()) {
1✔
70
            return emptySupplier.get();
1✔
71
        } else {
72
            return constructor.apply(applyMapper(mapper));
1✔
73
        }
74
    }
75

76
    /**
77
     * If not empty, apply the predicate to each value in the list and return a new condition with the filtered values.
78
     *     Else returns an empty condition (this).
79
     *
80
     * @param predicate predicate applied to the values, if not empty
81
     *
82
     * @return a new condition with filtered values if renderable, otherwise an empty condition
83
     */
84
    public abstract AbstractListValueCondition<T> filter(Predicate<? super T> predicate);
85

86
    public abstract String operator();
87

88
    @Override
89
    public FragmentAndParameters renderCondition(RenderingContext renderingContext, BindableColumn<T> leftColumn) {
90
        return values().map(v -> toFragmentAndParameters(v, renderingContext, leftColumn))
1✔
91
                .collect(FragmentCollector.collect())
1✔
92
                .toFragmentAndParameters(Collectors.joining(",", //$NON-NLS-1$
1✔
93
                        operator() + " (", ")")); //$NON-NLS-1$ //$NON-NLS-2$
1✔
94
    }
95

96
    private FragmentAndParameters toFragmentAndParameters(T value, RenderingContext renderingContext,
97
                                                          BindableColumn<T> leftColumn) {
98
        RenderedParameterInfo parameterInfo = renderingContext.calculateParameterInfo(leftColumn);
1✔
99
        return FragmentAndParameters.withFragment(parameterInfo.renderedPlaceHolder())
1✔
100
                .withParameter(parameterInfo.parameterMapKey(), leftColumn.convertParameterType(value))
1✔
101
                .build();
1✔
102
    }
103
}
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