Skip to content

Commit bd691fe

Browse files
authored
Merge pull request #68 from jeffgbutler/master
Support custom conditional rendering on all conditions
2 parents b6f3f0f + 4d13b4b commit bd691fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1643
-512
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<java.version>1.8</java.version>
3434
<maven.compiler.source>${java.version}</maven.compiler.source>
3535
<maven.compiler.target>${java.version}</maven.compiler.target>
36-
<junit.jupiter.version>5.3.0</junit.jupiter.version>
37-
<junit.platform.version>1.3.0</junit.platform.version>
36+
<junit.jupiter.version>5.3.2</junit.jupiter.version>
37+
<junit.platform.version>1.3.2</junit.platform.version>
3838
<clirr.comparisonVersion>1.1.0</clirr.comparisonVersion>
3939
</properties>
4040

@@ -105,7 +105,7 @@
105105
<dependency>
106106
<groupId>org.springframework</groupId>
107107
<artifactId>spring-jdbc</artifactId>
108-
<version>5.0.9.RELEASE</version>
108+
<version>5.1.3.RELEASE</version>
109109
<scope>test</scope>
110110
</dependency>
111111
<dependency>

src/main/java/org/mybatis/dynamic/sql/AbstractListValueCondition.java

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,30 @@
1919
import java.util.List;
2020
import java.util.Objects;
2121
import java.util.function.Function;
22+
import java.util.function.UnaryOperator;
2223
import java.util.stream.Stream;
2324

2425
public abstract class AbstractListValueCondition<T> implements VisitableCondition<T> {
25-
private List<T> values;
26+
protected List<T> values;
27+
protected UnaryOperator<Stream<T>> valueStreamOperations;
2628

27-
protected AbstractListValueCondition(AbstractBuilder<T, ?> builder) {
28-
values = Objects.requireNonNull(builder.values);
29+
protected AbstractListValueCondition(List<T> values) {
30+
this(values, UnaryOperator.identity());
31+
}
32+
33+
protected AbstractListValueCondition(List<T> values, UnaryOperator<Stream<T>> valueStreamOperations) {
34+
this.values = new ArrayList<>(Objects.requireNonNull(values));
35+
this.valueStreamOperations = Objects.requireNonNull(valueStreamOperations);
2936
}
3037

3138
public final <R> Stream<R> mapValues(Function<T, R> mapper) {
32-
return values.stream()
33-
.map(this::mapValue)
34-
.map(mapper);
39+
return valueStreamOperations.apply(values.stream()).map(mapper);
3540
}
3641

3742
@Override
3843
public <R> R accept(ConditionVisitor<T, R> visitor) {
3944
return visitor.visit(this);
4045
}
4146

42-
/**
43-
* This method allows subclasses to alter the value before it is placed
44-
* into the parameter map. An example of this is when the case insensitive
45-
* conditions will change a value to upper case.
46-
*
47-
* <p>We do not expose the values stream because we cannot allow subclasses
48-
* to change the order or number of values.
49-
*
50-
* @param value the value
51-
* @return the mapped value - in most cases the value is not changed
52-
*/
53-
protected T mapValue(T value) {
54-
return value;
55-
}
56-
5747
public abstract String renderCondition(String columnName, Stream<String> placeholders);
58-
59-
public abstract static class AbstractBuilder<T, B extends AbstractBuilder<T, B>> {
60-
private List<T> values = new ArrayList<>();
61-
62-
public B withValues(List<T> values) {
63-
this.values.addAll(values);
64-
return getThis();
65-
}
66-
67-
public abstract B getThis();
68-
}
6948
}

src/main/java/org/mybatis/dynamic/sql/AbstractNoValueCondition.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,8 +15,26 @@
1515
*/
1616
package org.mybatis.dynamic.sql;
1717

18+
import java.util.Objects;
19+
import java.util.function.BooleanSupplier;
20+
1821
public abstract class AbstractNoValueCondition<T> implements VisitableCondition<T> {
1922

23+
private BooleanSupplier booleanSupplier;
24+
25+
protected AbstractNoValueCondition() {
26+
booleanSupplier = () -> true;
27+
}
28+
29+
protected AbstractNoValueCondition(BooleanSupplier booleanSupplier) {
30+
this.booleanSupplier = Objects.requireNonNull(booleanSupplier);
31+
}
32+
33+
@Override
34+
public boolean shouldRender() {
35+
return booleanSupplier.getAsBoolean();
36+
}
37+
2038
@Override
2139
public <R> R accept(ConditionVisitor<T,R> visitor) {
2240
return visitor.visit(this);

src/main/java/org/mybatis/dynamic/sql/AbstractSingleValueCondition.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,19 +16,32 @@
1616
package org.mybatis.dynamic.sql;
1717

1818
import java.util.Objects;
19+
import java.util.function.Predicate;
1920
import java.util.function.Supplier;
2021

2122
public abstract class AbstractSingleValueCondition<T> implements VisitableCondition<T> {
22-
private Supplier<T> valueSupplier;
23+
protected Supplier<T> valueSupplier;
24+
private Predicate<T> predicate;
2325

2426
protected AbstractSingleValueCondition(Supplier<T> valueSupplier) {
2527
this.valueSupplier = Objects.requireNonNull(valueSupplier);
28+
predicate = v -> true;
29+
}
30+
31+
protected AbstractSingleValueCondition(Supplier<T> valueSupplier, Predicate<T> predicate) {
32+
this.valueSupplier = Objects.requireNonNull(valueSupplier);
33+
this.predicate = Objects.requireNonNull(predicate);
2634
}
2735

2836
public T value() {
2937
return valueSupplier.get();
3038
}
3139

40+
@Override
41+
public boolean shouldRender() {
42+
return predicate.test(value());
43+
}
44+
3245
@Override
3346
public <R> R accept(ConditionVisitor<T,R> visitor) {
3447
return visitor.visit(this);

src/main/java/org/mybatis/dynamic/sql/AbstractTwoValueCondition.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,24 @@
1616
package org.mybatis.dynamic.sql;
1717

1818
import java.util.Objects;
19+
import java.util.function.BiPredicate;
1920
import java.util.function.Supplier;
2021

2122
public abstract class AbstractTwoValueCondition<T> implements VisitableCondition<T> {
22-
private Supplier<T> valueSupplier1;
23-
private Supplier<T> valueSupplier2;
23+
protected Supplier<T> valueSupplier1;
24+
protected Supplier<T> valueSupplier2;
25+
private BiPredicate<T, T> predicate;
2426

2527
protected AbstractTwoValueCondition(Supplier<T> valueSupplier1, Supplier<T> valueSupplier2) {
2628
this.valueSupplier1 = Objects.requireNonNull(valueSupplier1);
2729
this.valueSupplier2 = Objects.requireNonNull(valueSupplier2);
30+
predicate = (v1, v2) -> true;
31+
}
32+
33+
protected AbstractTwoValueCondition(Supplier<T> valueSupplier1, Supplier<T> valueSupplier2,
34+
BiPredicate<T, T> predicate) {
35+
this(valueSupplier1, valueSupplier2);
36+
this.predicate = Objects.requireNonNull(predicate);
2837
}
2938

3039
public T value1() {
@@ -34,7 +43,12 @@ public T value1() {
3443
public T value2() {
3544
return valueSupplier2.get();
3645
}
37-
46+
47+
@Override
48+
public boolean shouldRender() {
49+
return predicate.test(value1(), value2());
50+
}
51+
3852
@Override
3953
public <R> R accept(ConditionVisitor<T,R> visitor) {
4054
return visitor.visit(this);

src/main/java/org/mybatis/dynamic/sql/select/join/JoinType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public enum JoinType {
2525

2626
private String shortType;
2727

28-
private JoinType() {
28+
JoinType() {
2929
}
3030

31-
private JoinType(String shortType) {
31+
JoinType(String shortType) {
3232
this.shortType = shortType;
3333
}
3434

src/main/java/org/mybatis/dynamic/sql/util/FragmentCollector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public boolean hasMultipleFragments() {
5757
return fragments.size() > 1;
5858
}
5959

60+
public boolean isEmpty() {
61+
return fragments.isEmpty();
62+
}
63+
6064
public static Collector<FragmentAndParameters, FragmentCollector, FragmentCollector> collect() {
6165
return Collector.of(FragmentCollector::new,
6266
FragmentCollector::add,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2016-2018 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+
* 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 org.mybatis.dynamic.sql.util;
17+
18+
import java.util.function.BiPredicate;
19+
20+
public class Predicates {
21+
private Predicates() {}
22+
23+
public static <T> BiPredicate<T, T> bothPresent() {
24+
return (v1, v2) -> v1 != null && v2 != null;
25+
}
26+
}

src/main/java/org/mybatis/dynamic/sql/util/StringUtilities.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616
package org.mybatis.dynamic.sql.util;
1717

1818
import java.util.Optional;
19+
import java.util.function.UnaryOperator;
20+
import java.util.stream.Stream;
1921

2022
public interface StringUtilities {
2123

@@ -36,4 +38,13 @@ static String spaceBefore(Optional<String> in) {
3638
static String spaceBefore(String in) {
3739
return " " + in; //$NON-NLS-1$
3840
}
41+
42+
static String safelyUpperCase(String s) {
43+
return s == null ? null : s.toUpperCase();
44+
}
45+
46+
static UnaryOperator<Stream<String>> upperCaseAfter(UnaryOperator<Stream<String>> valueModifier) {
47+
UnaryOperator<Stream<String>> ua = s -> s.map(StringUtilities::safelyUpperCase);
48+
return t -> ua.apply(valueModifier.apply(t));
49+
}
3950
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2016-2018 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+
* 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 org.mybatis.dynamic.sql.where.condition;
17+
18+
import java.util.function.Supplier;
19+
20+
/**
21+
* Utility class supporting the "and" part of a between condition. This class supports builders,
22+
* so it is mutable.
23+
*
24+
* @author Jeff Butler
25+
*
26+
* @param <T> the type of field for the between condition
27+
* @param <R> the type of condition being built
28+
*/
29+
public abstract class AndGatherer<T, R> {
30+
protected Supplier<T> valueSupplier1;
31+
protected Supplier<T> valueSupplier2;
32+
33+
protected AndGatherer(Supplier<T> valueSupplier1) {
34+
this.valueSupplier1 = valueSupplier1;
35+
}
36+
37+
public R and(T value2) {
38+
return and(() -> value2);
39+
}
40+
41+
public R and(Supplier<T> valueSupplier2) {
42+
this.valueSupplier2 = valueSupplier2;
43+
return build();
44+
}
45+
46+
protected abstract R build();
47+
}

0 commit comments

Comments
 (0)