Skip to content

Commit 1376bd0

Browse files
committed
Refactor filter/map algorithm into base class for two value Conditions
1 parent 3f85165 commit 1376bd0

File tree

5 files changed

+55
-51
lines changed

5 files changed

+55
-51
lines changed

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
*/
1616
package org.mybatis.dynamic.sql;
1717

18-
public abstract class AbstractTwoValueCondition<T> implements VisitableCondition<T> {
18+
import java.util.function.BiFunction;
19+
import java.util.function.BiPredicate;
20+
import java.util.function.UnaryOperator;
21+
22+
public abstract class AbstractTwoValueCondition<T, S extends AbstractTwoValueCondition<T, S>>
23+
implements VisitableCondition<T> {
1924
protected final T value1;
2025
protected final T value2;
2126

@@ -37,5 +42,42 @@ public <R> R accept(ConditionVisitor<T, R> visitor) {
3742
return visitor.visit(this);
3843
}
3944

45+
protected S filter(BiPredicate<T, T> predicate, S self, S empty) {
46+
if (shouldRender()) {
47+
return predicate.test(value1, value2) ? self : empty;
48+
} else {
49+
return self;
50+
}
51+
}
52+
53+
protected S map(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2, S self, BiFunction<T, T, S> constructor) {
54+
if (shouldRender()) {
55+
return constructor.apply(mapper1.apply(value1), mapper2.apply(value2));
56+
}else {
57+
return self;
58+
}
59+
}
60+
61+
/**
62+
* If renderable and the values match the predicate, returns this condition. Else returns a condition
63+
* that will not render.
64+
*
65+
* @param predicate predicate applied to the values, if renderable
66+
* @return this condition if renderable and the values match the predicate, otherwise a condition
67+
* that will not render.
68+
*/
69+
public abstract S filter(BiPredicate<T, T> predicate);
70+
71+
/**
72+
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
73+
* condition that will not render (this).
74+
*
75+
* @param mapper1 a mapping function to apply to the first value, if renderable
76+
* @param mapper2 a mapping function to apply to the second value, if renderable
77+
* @return a new condition with the result of applying the mappers to the values of this condition,
78+
* if renderable, otherwise a condition that will not render.
79+
*/
80+
public abstract S map(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2);
81+
4082
public abstract String renderCondition(String columnName, String placeholder1, String placeholder2);
4183
}

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: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.mybatis.dynamic.sql.AbstractTwoValueCondition;
2222
import org.mybatis.dynamic.sql.util.Predicates;
2323

24-
public class IsBetween<T> extends AbstractTwoValueCondition<T> {
24+
public class IsBetween<T> extends AbstractTwoValueCondition<T, IsBetween<T>> {
2525

2626
protected IsBetween(T value1, T value2) {
2727
super(value1, value2);
@@ -61,33 +61,14 @@ public IsBetween<T> then(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2) {
6161
return map(mapper1, mapper2);
6262
}
6363

64-
/**
65-
* If renderable and the values match the predicate, returns this condition. Else returns a condition
66-
* that will not render.
67-
*
68-
* @param predicate predicate applied to the values, if renderable
69-
* @return this condition if renderable and the values match the predicate, otherwise a condition
70-
* that will not render.
71-
*/
64+
@Override
7265
public IsBetween<T> filter(BiPredicate<T, T> predicate) {
73-
if (shouldRender()) {
74-
return predicate.test(value1, value2) ? this : EmptyIsBetween.empty();
75-
} else {
76-
return this;
77-
}
66+
return filter(predicate, this, EmptyIsBetween.empty());
7867
}
7968

80-
/**
81-
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
82-
* condition that will not render (this).
83-
*
84-
* @param mapper1 a mapping function to apply to the first value, if renderable
85-
* @param mapper2 a mapping function to apply to the second value, if renderable
86-
* @return a new condition with the result of applying the mappers to the values of this condition,
87-
* if renderable, otherwise a condition that will not render.
88-
*/
69+
@Override
8970
public IsBetween<T> map(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2) {
90-
return shouldRender() ? new IsBetween<>(mapper1.apply(value1), mapper2.apply(value2)) : this;
71+
return map(mapper1, mapper2, this, IsBetween::new);
9172
}
9273

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

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

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.mybatis.dynamic.sql.AbstractTwoValueCondition;
2222
import org.mybatis.dynamic.sql.util.Predicates;
2323

24-
public class IsNotBetween<T> extends AbstractTwoValueCondition<T> {
24+
public class IsNotBetween<T> extends AbstractTwoValueCondition<T, IsNotBetween<T>> {
2525

2626
protected IsNotBetween(T value1, T value2) {
2727
super(value1, value2);
@@ -61,33 +61,14 @@ public IsNotBetween<T> then(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2)
6161
return map(mapper1, mapper2);
6262
}
6363

64-
/**
65-
* If renderable and the values match the predicate, returns this condition. Else returns a condition
66-
* that will not render.
67-
*
68-
* @param predicate predicate applied to the values, if renderable
69-
* @return this condition if renderable and the values match the predicate, otherwise a condition
70-
* that will not render.
71-
*/
64+
@Override
7265
public IsNotBetween<T> filter(BiPredicate<T, T> predicate) {
73-
if (shouldRender()) {
74-
return predicate.test(value1, value2) ? this : EmptyIsNotBetween.empty();
75-
} else {
76-
return this;
77-
}
66+
return filter(predicate, this, EmptyIsNotBetween.empty());
7867
}
7968

80-
/**
81-
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
82-
* condition that will not render (this).
83-
*
84-
* @param mapper1 a mapping function to apply to the first value, if renderable
85-
* @param mapper2 a mapping function to apply to the second value, if renderable
86-
* @return a new condition with the result of applying the mappers to the values of this condition,
87-
* if renderable, otherwise a condition that will not render.
88-
*/
69+
@Override
8970
public IsNotBetween<T> map(UnaryOperator<T> mapper1, UnaryOperator<T> mapper2) {
90-
return shouldRender() ? new IsNotBetween<>(mapper1.apply(value1), mapper2.apply(value2)) : this;
71+
return map(mapper1, mapper2, this, IsNotBetween::new);
9172
}
9273

9374
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)