Skip to content

Commit 1c4bdbb

Browse files
committed
Map functions can change data types - Two Value Conditions
1 parent edbcb02 commit 1c4bdbb

File tree

5 files changed

+51
-41
lines changed

5 files changed

+51
-41
lines changed

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

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
*/
1616
package org.mybatis.dynamic.sql;
1717

18-
import java.util.function.BiFunction;
19-
import java.util.function.BiPredicate;
20-
import java.util.function.Supplier;
21-
import java.util.function.UnaryOperator;
18+
import java.util.function.*;
2219

23-
public abstract class AbstractTwoValueCondition<T, S extends AbstractTwoValueCondition<T, S>>
20+
public abstract class AbstractTwoValueCondition<T>
2421
implements VisitableCondition<T> {
2522
protected final T value1;
2623
protected final T value2;
@@ -43,42 +40,34 @@ public <R> R accept(ConditionVisitor<T, R> visitor) {
4340
return visitor.visit(this);
4441
}
4542

46-
protected S filter(BiPredicate<T, T> predicate, Supplier<S> empty, S self) {
43+
protected <S> S filterSupport(BiPredicate<? super T, ? super T> predicate, Supplier<S> empty, S self) {
4744
if (shouldRender()) {
4845
return predicate.test(value1, value2) ? self : empty.get();
4946
} else {
5047
return self;
5148
}
5249
}
5350

54-
/**
55-
* If renderable and the values match the predicate, returns this condition. Else returns a condition
56-
* that will not render.
57-
*
58-
* @param predicate predicate applied to the values, if renderable
59-
* @return this condition if renderable and the values match the predicate, otherwise a condition
60-
* that will not render.
61-
*/
62-
public abstract S filter(BiPredicate<T, T> predicate);
63-
64-
protected S map(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2, BiFunction<T, T, S> constructor, S self) {
51+
protected <R, S> S mapSupport(Function<? super T, ? extends R> mapper1,
52+
Function<? super T, ? extends R> mapper2,
53+
BiFunction<R, R, S> constructor,
54+
Supplier<S> empty) {
6555
if (shouldRender()) {
6656
return constructor.apply(mapper1.apply(value1), mapper2.apply(value2));
6757
} else {
68-
return self;
58+
return empty.get();
6959
}
7060
}
7161

7262
/**
73-
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
74-
* condition that will not render (this).
63+
* If renderable and the values match the predicate, returns this condition. Else returns a condition
64+
* that will not render.
7565
*
76-
* @param mapper1 a mapping function to apply to the first value, if renderable
77-
* @param mapper2 a mapping function to apply to the second value, if renderable
78-
* @return a new condition with the result of applying the mappers to the values of this condition,
79-
* if renderable, otherwise a condition that will not render.
66+
* @param predicate predicate applied to the values, if renderable
67+
* @return this condition if renderable and the values match the predicate, otherwise a condition
68+
* that will not render.
8069
*/
81-
public abstract S map(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2);
70+
public abstract AbstractTwoValueCondition<T> filter(BiPredicate<? super T, ? super T> predicate);
8271

8372
public abstract String renderCondition(String columnName, String placeholder1, String placeholder2);
8473
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public interface ConditionVisitor<T, R> {
2222

2323
R visit(AbstractSingleValueCondition<T> condition);
2424

25-
R visit(AbstractTwoValueCondition<T, ?> condition);
25+
R visit(AbstractTwoValueCondition<T> condition);
2626

2727
R visit(AbstractSubselectCondition<T> condition);
2828

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
package org.mybatis.dynamic.sql.where.condition;
1717

1818
import java.util.function.BiPredicate;
19+
import java.util.function.Function;
1920
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractTwoValueCondition;
2223
import org.mybatis.dynamic.sql.util.Predicates;
2324

24-
public class IsBetween<T> extends AbstractTwoValueCondition<T, IsBetween<T>> {
25+
public class IsBetween<T> extends AbstractTwoValueCondition<T> {
2526
private static final IsBetween<?> EMPTY = new IsBetween<Object>(null, null) {
2627
@Override
2728
public boolean shouldRender() {
@@ -62,7 +63,7 @@ public IsBetween<T> when(BiPredicate<T, T> predicate) {
6263
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
6364
* condition that will not render (this).
6465
*
65-
* @deprecated replaced by {@link IsBetween#map(UnaryOperator, UnaryOperator)}
66+
* @deprecated replaced by {@link IsBetween#map(Function, Function)}
6667
* @param mapper1 a mapping function to apply to the first value, if renderable
6768
* @param mapper2 a mapping function to apply to the second value, if renderable
6869
* @return a new condition with the result of applying the mappers to the values of this condition,
@@ -74,13 +75,22 @@ public IsBetween<T> then(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2) {
7475
}
7576

7677
@Override
77-
public IsBetween<T> filter(BiPredicate<T, T> predicate) {
78-
return filter(predicate, IsBetween::empty, this);
78+
public IsBetween<T> filter(BiPredicate<? super T, ? super T> predicate) {
79+
return filterSupport(predicate, IsBetween::empty, this);
7980
}
8081

81-
@Override
82-
public IsBetween<T> map(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2) {
83-
return map(mapper1, mapper2, IsBetween::new, this);
82+
/**
83+
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
84+
* condition that will not render (this).
85+
*
86+
* @param mapper1 a mapping function to apply to the first value, if renderable
87+
* @param mapper2 a mapping function to apply to the second value, if renderable
88+
* @param <R> type of the new condition
89+
* @return a new condition with the result of applying the mappers to the values of this condition,
90+
* if renderable, otherwise a condition that will not render.
91+
*/
92+
public <R> IsBetween<R> map(Function<? super T, ? extends R> mapper1, Function<? super T, ? extends R> mapper2) {
93+
return mapSupport(mapper1, mapper2, IsBetween::new, IsBetween::empty);
8494
}
8595

8696
public static <T> Builder<T> isBetween(T value1) {

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
package org.mybatis.dynamic.sql.where.condition;
1717

1818
import java.util.function.BiPredicate;
19+
import java.util.function.Function;
1920
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractTwoValueCondition;
2223
import org.mybatis.dynamic.sql.util.Predicates;
2324

24-
public class IsNotBetween<T> extends AbstractTwoValueCondition<T, IsNotBetween<T>> {
25+
public class IsNotBetween<T> extends AbstractTwoValueCondition<T> {
2526
private static final IsNotBetween<?> EMPTY = new IsNotBetween<Object>(null, null) {
2627
@Override
2728
public boolean shouldRender() {
@@ -62,7 +63,7 @@ public IsNotBetween<T> when(BiPredicate<T, T> predicate) {
6263
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
6364
* condition that will not render (this).
6465
*
65-
* @deprecated replaced by {@link IsNotBetween#map(UnaryOperator, UnaryOperator)}
66+
* @deprecated replaced by {@link IsNotBetween#map(Function, Function)}
6667
* @param mapper1 a mapping function to apply to the first value, if renderable
6768
* @param mapper2 a mapping function to apply to the second value, if renderable
6869
* @return a new condition with the result of applying the mappers to the values of this condition,
@@ -74,13 +75,23 @@ public IsNotBetween<T> then(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2)
7475
}
7576

7677
@Override
77-
public IsNotBetween<T> filter(BiPredicate<T, T> predicate) {
78-
return filter(predicate, IsNotBetween::empty, this);
78+
public IsNotBetween<T> filter(BiPredicate<? super T, ? super T> predicate) {
79+
return filterSupport(predicate, IsNotBetween::empty, this);
7980
}
8081

81-
@Override
82-
public IsNotBetween<T> map(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2) {
83-
return map(mapper1, mapper2, IsNotBetween::new, this);
82+
/**
83+
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
84+
* condition that will not render (this).
85+
*
86+
* @param mapper1 a mapping function to apply to the first value, if renderable
87+
* @param mapper2 a mapping function to apply to the second value, if renderable
88+
* @param <R> type of the new condition
89+
* @return a new condition with the result of applying the mappers to the values of this condition,
90+
* if renderable, otherwise a condition that will not render.
91+
*/
92+
public <R> IsNotBetween<R> map(Function<? super T, ? extends R> mapper1,
93+
Function<? super T, ? extends R> mapper2) {
94+
return mapSupport(mapper1, mapper2, IsNotBetween::new, IsNotBetween::empty);
8495
}
8596

8697
public static <T> Builder<T> isNotBetween(T value1) {

src/main/java/org/mybatis/dynamic/sql/where/render/WhereConditionVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public FragmentAndParameters visit(AbstractSingleValueCondition<T> condition) {
7777
}
7878

7979
@Override
80-
public FragmentAndParameters visit(AbstractTwoValueCondition<T, ?> condition) {
80+
public FragmentAndParameters visit(AbstractTwoValueCondition<T> condition) {
8181
String mapKey1 = RenderingStrategy.formatParameterMapKey(sequence);
8282
String mapKey2 = RenderingStrategy.formatParameterMapKey(sequence);
8383
String fragment = condition.renderCondition(columnName(),

0 commit comments

Comments
 (0)