Skip to content

Commit 3723182

Browse files
committed
Implement filter/map for the between conditions
1 parent 15a2f38 commit 3723182

File tree

7 files changed

+210
-149
lines changed

7 files changed

+210
-149
lines changed

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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,25 +16,15 @@
1616
package org.mybatis.dynamic.sql;
1717

1818
import java.util.Objects;
19-
import java.util.function.BiPredicate;
2019
import java.util.function.Supplier;
2120

2221
public abstract class AbstractTwoValueCondition<T> implements VisitableCondition<T> {
2322
protected final Supplier<T> valueSupplier1;
2423
protected final Supplier<T> valueSupplier2;
25-
private final BiPredicate<T, T> predicate;
2624

2725
protected AbstractTwoValueCondition(Supplier<T> valueSupplier1, Supplier<T> valueSupplier2) {
2826
this.valueSupplier1 = Objects.requireNonNull(valueSupplier1);
2927
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 = Objects.requireNonNull(valueSupplier1);
36-
this.valueSupplier2 = Objects.requireNonNull(valueSupplier2);
37-
this.predicate = Objects.requireNonNull(predicate);
3828
}
3929

4030
public T value1() {
@@ -45,11 +35,6 @@ public T value2() {
4535
return valueSupplier2.get();
4636
}
4737

48-
@Override
49-
public boolean shouldRender() {
50-
return predicate.test(value1(), value2());
51-
}
52-
5338
@Override
5439
public <R> R accept(ConditionVisitor<T, R> visitor) {
5540
return visitor.visit(this);

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import org.mybatis.dynamic.sql.util.Buildable;
5959
import org.mybatis.dynamic.sql.where.WhereDSL;
6060
import org.mybatis.dynamic.sql.where.condition.IsBetween;
61-
import org.mybatis.dynamic.sql.where.condition.IsBetweenWhenPresent;
6261
import org.mybatis.dynamic.sql.where.condition.IsEqualTo;
6362
import org.mybatis.dynamic.sql.where.condition.IsEqualToColumn;
6463
import org.mybatis.dynamic.sql.where.condition.IsEqualToWithSubselect;
@@ -80,7 +79,6 @@
8079
import org.mybatis.dynamic.sql.where.condition.IsLike;
8180
import org.mybatis.dynamic.sql.where.condition.IsLikeCaseInsensitive;
8281
import org.mybatis.dynamic.sql.where.condition.IsNotBetween;
83-
import org.mybatis.dynamic.sql.where.condition.IsNotBetweenWhenPresent;
8482
import org.mybatis.dynamic.sql.where.condition.IsNotEqualTo;
8583
import org.mybatis.dynamic.sql.where.condition.IsNotEqualToColumn;
8684
import org.mybatis.dynamic.sql.where.condition.IsNotEqualToWithSubselect;
@@ -612,12 +610,12 @@ static <T> IsBetween.Builder<T> isBetween(Supplier<T> valueSupplier1) {
612610
return IsBetween.isBetween(valueSupplier1);
613611
}
614612

615-
static <T> IsBetweenWhenPresent.Builder<T> isBetweenWhenPresent(T value1) {
613+
static <T> IsBetween.WhenPresentBuilder<T> isBetweenWhenPresent(T value1) {
616614
return isBetweenWhenPresent(() -> value1);
617615
}
618616

619-
static <T> IsBetweenWhenPresent.Builder<T> isBetweenWhenPresent(Supplier<T> valueSupplier1) {
620-
return IsBetweenWhenPresent.isBetweenWhenPresent(valueSupplier1);
617+
static <T> IsBetween.WhenPresentBuilder<T> isBetweenWhenPresent(Supplier<T> valueSupplier1) {
618+
return IsBetween.isBetweenWhenPresent(valueSupplier1);
621619
}
622620

623621
static <T> IsNotBetween.Builder<T> isNotBetween(T value1) {
@@ -628,12 +626,12 @@ static <T> IsNotBetween.Builder<T> isNotBetween(Supplier<T> valueSupplier1) {
628626
return IsNotBetween.isNotBetween(valueSupplier1);
629627
}
630628

631-
static <T> IsNotBetweenWhenPresent.Builder<T> isNotBetweenWhenPresent(T value1) {
629+
static <T> IsNotBetween.WhenPresentBuilder<T> isNotBetweenWhenPresent(T value1) {
632630
return isNotBetweenWhenPresent(() -> value1);
633631
}
634632

635-
static <T> IsNotBetweenWhenPresent.Builder<T> isNotBetweenWhenPresent(Supplier<T> valueSupplier1) {
636-
return IsNotBetweenWhenPresent.isNotBetweenWhenPresent(valueSupplier1);
633+
static <T> IsNotBetween.WhenPresentBuilder<T> isNotBetweenWhenPresent(Supplier<T> valueSupplier1) {
634+
return IsNotBetween.isNotBetweenWhenPresent(valueSupplier1);
637635
}
638636

639637
// for string columns, but generic for columns with type handlers

src/main/java/org/mybatis/dynamic/sql/where/condition/IsBetween.java

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -20,22 +20,85 @@
2020
import java.util.function.UnaryOperator;
2121

2222
import org.mybatis.dynamic.sql.AbstractTwoValueCondition;
23+
import org.mybatis.dynamic.sql.util.Predicates;
2324

2425
public class IsBetween<T> extends AbstractTwoValueCondition<T> {
2526

2627
protected IsBetween(Supplier<T> valueSupplier1, Supplier<T> valueSupplier2) {
2728
super(valueSupplier1, valueSupplier2);
2829
}
2930

30-
protected IsBetween(Supplier<T> valueSupplier1, Supplier<T> valueSupplier2, BiPredicate<T, T> predicate) {
31-
super(valueSupplier1, valueSupplier2, predicate);
32-
}
33-
3431
@Override
3532
public String renderCondition(String columnName, String placeholder1, String placeholder2) {
3633
return columnName + " between " + placeholder1 + " and " + placeholder2; //$NON-NLS-1$ //$NON-NLS-2$
3734
}
3835

36+
/**
37+
* If renderable and the values match the predicate, returns this condition. Else returns a condition
38+
* that will not render.
39+
*
40+
* @deprecated replaced by {@link IsBetween#filter(BiPredicate)}
41+
* @param predicate predicate applied to the values, if renderable
42+
* @return this condition if renderable and the values match the predicate, otherwise a condition
43+
* that will not render.
44+
*/
45+
@Deprecated
46+
public IsBetween<T> when(BiPredicate<T, T> predicate) {
47+
return filter(predicate);
48+
}
49+
50+
/**
51+
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
52+
* condition that will not render (this).
53+
*
54+
* @deprecated replaced by {@link IsBetween#map(UnaryOperator, UnaryOperator)}
55+
* @param mapper1 a mapping function to apply to the first value, if renderable
56+
* @param mapper2 a mapping function to apply to the second value, if renderable
57+
* @return a new condition with the result of applying the mappers to the values of this condition,
58+
* if renderable, otherwise a condition that will not render.
59+
*/
60+
@Deprecated
61+
public IsBetween<T> then(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2) {
62+
return map(mapper1, mapper2);
63+
}
64+
65+
/**
66+
* If renderable and the values match the predicate, returns this condition. Else returns a condition
67+
* that will not render.
68+
*
69+
* @param predicate predicate applied to the values, if renderable
70+
* @return this condition if renderable and the values match the predicate, otherwise a condition
71+
* that will not render.
72+
*/
73+
public IsBetween<T> filter(BiPredicate<T, T> predicate) {
74+
if (shouldRender()) {
75+
return predicate.test(value1(), value2()) ? this : EmptyIsBetween.empty();
76+
} else {
77+
return this;
78+
}
79+
}
80+
81+
/**
82+
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
83+
* condition that will not render (this).
84+
*
85+
* @param mapper1 a mapping function to apply to the first value, if renderable
86+
* @param mapper2 a mapping function to apply to the second value, if renderable
87+
* @return a new condition with the result of applying the mappers to the values of this condition,
88+
* if renderable, otherwise a condition that will not render.
89+
*/
90+
public IsBetween<T> map(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2) {
91+
return shouldRender() ? new IsBetween<>(() -> mapper1.apply(value1()), () -> mapper2.apply(value2())) : this;
92+
}
93+
94+
public static <T> Builder<T> isBetween(Supplier<T> valueSupplier1) {
95+
return new Builder<>(valueSupplier1);
96+
}
97+
98+
public static <T> WhenPresentBuilder<T> isBetweenWhenPresent(Supplier<T> valueSupplier1) {
99+
return new WhenPresentBuilder<>(valueSupplier1);
100+
}
101+
39102
public static class Builder<T> extends AndGatherer<T, IsBetween<T>> {
40103
private Builder(Supplier<T> valueSupplier1) {
41104
super(valueSupplier1);
@@ -47,16 +110,33 @@ protected IsBetween<T> build() {
47110
}
48111
}
49112

50-
public static <T> Builder<T> isBetween(Supplier<T> valueSupplier1) {
51-
return new Builder<>(valueSupplier1);
52-
}
113+
public static class WhenPresentBuilder<T> extends AndGatherer<T, IsBetween<T>> {
114+
private WhenPresentBuilder(Supplier<T> valueSupplier1) {
115+
super(valueSupplier1);
116+
}
53117

54-
public IsBetween<T> when(BiPredicate<T, T> predicate) {
55-
return new IsBetween<>(valueSupplier1, valueSupplier2, predicate);
118+
@Override
119+
protected IsBetween<T> build() {
120+
return new IsBetween<>(valueSupplier1, valueSupplier2).filter(Predicates.bothPresent());
121+
}
56122
}
57123

58-
public IsBetween<T> then(UnaryOperator<T> transformer1, UnaryOperator<T> transformer2) {
59-
return shouldRender() ? new IsBetween<>(() -> transformer1.apply(value1()),
60-
() -> transformer2.apply(value2())) : this;
124+
public static class EmptyIsBetween<T> extends IsBetween<T> {
125+
private static final EmptyIsBetween<?> EMPTY = new EmptyIsBetween<>();
126+
127+
public static <T> EmptyIsBetween<T> empty() {
128+
@SuppressWarnings("unchecked")
129+
EmptyIsBetween<T> t = (EmptyIsBetween<T>) EMPTY;
130+
return t;
131+
}
132+
133+
public EmptyIsBetween() {
134+
super(() -> null, () -> null);
135+
}
136+
137+
@Override
138+
public boolean shouldRender() {
139+
return false;
140+
}
61141
}
62142
}

src/main/java/org/mybatis/dynamic/sql/where/condition/IsBetweenWhenPresent.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)