Skip to content

Commit 5094422

Browse files
committedMar 14, 2025
Make filter and map optional on single value conditions
1 parent 30fc7ef commit 5094422

13 files changed

+77
-43
lines changed
 

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

+50-21
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,6 @@ protected <R, S extends AbstractSingleValueCondition<R>> S mapSupport(Function<?
5454
}
5555
}
5656

57-
/**
58-
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
59-
* that will not render.
60-
*
61-
* @param predicate predicate applied to the value, if renderable
62-
* @return this condition if renderable and the value matches the predicate, otherwise a condition
63-
* that will not render.
64-
*/
65-
public abstract AbstractSingleValueCondition<T> filter(Predicate<? super T> predicate);
66-
67-
/**
68-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
69-
* condition that will not render (this).
70-
*
71-
* @param mapper a mapping function to apply to the value, if renderable
72-
* @param <R> type of the new condition
73-
* @return a new condition with the result of applying the mapper to the value of this condition,
74-
* if renderable, otherwise a condition that will not render.
75-
*/
76-
public abstract <R> AbstractSingleValueCondition<R> map(Function<? super T, ? extends R> mapper);
77-
7857
public abstract String operator();
7958

8059
@Override
@@ -86,4 +65,54 @@ public FragmentAndParameters renderCondition(RenderingContext renderingContext,
8665
.withParameter(parameterInfo.parameterMapKey(), leftColumn.convertParameterType(value()))
8766
.build();
8867
}
68+
69+
/**
70+
* Conditions may implement Filterable to add optionality to rendering.
71+
*
72+
* <p>If a condition is Filterable, then a user may add a filter to the usage of the condition that makes a decision
73+
* whether to render the condition at runtime. Conditions that fail the filter will be dropped from the
74+
* rendered SQL.
75+
*
76+
* <p>Implementations of Filterable may call
77+
* {@link AbstractSingleValueCondition#filterSupport(Predicate, Supplier, AbstractSingleValueCondition)} as
78+
* a common implementation of the filtering algorithm.
79+
*
80+
* @param <T> the Java type related to the database column type
81+
*/
82+
public interface Filterable<T> {
83+
/**
84+
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
85+
* that will not render.
86+
*
87+
* @param predicate predicate applied to the value, if renderable
88+
* @return this condition if renderable and the value matches the predicate, otherwise a condition
89+
* that will not render.
90+
*/
91+
AbstractSingleValueCondition<T> filter(Predicate<? super T> predicate);
92+
}
93+
94+
/**
95+
* Conditions may implement Mappable to alter condition values or types during rendering.
96+
*
97+
* <p>If a condition is Mappable, then a user may add a mapper to the usage of the condition that can alter the
98+
* values of a condition, or change that datatype.
99+
*
100+
* <p>Implementations of Mappable may call
101+
* {@link AbstractSingleValueCondition#mapSupport(Function, Function, Supplier)} as
102+
* a common implementation of the mapping algorithm.
103+
*
104+
* @param <T> the Java type related to the database column type
105+
*/
106+
public interface Mappable<T> {
107+
/**
108+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
109+
* condition that will not render (this).
110+
*
111+
* @param mapper a mapping function to apply to the value, if renderable
112+
* @param <R> type of the new condition
113+
* @return a new condition with the result of applying the mapper to the value of this condition,
114+
* if renderable, otherwise a condition that will not render.
115+
*/
116+
<R> AbstractSingleValueCondition<R> map(Function<? super T, ? extends R> mapper);
117+
}
89118
}

‎src/main/java/org/mybatis/dynamic/sql/where/condition/IsEqualTo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2323

24-
public class IsEqualTo<T> extends AbstractSingleValueCondition<T> {
24+
public class IsEqualTo<T> extends AbstractSingleValueCondition<T>
25+
implements AbstractSingleValueCondition.Filterable<T>, AbstractSingleValueCondition.Mappable<T> {
2526

2627
private static final IsEqualTo<?> EMPTY = new IsEqualTo<Object>(-1) {
2728
@Override

‎src/main/java/org/mybatis/dynamic/sql/where/condition/IsGreaterThan.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2323

24-
public class IsGreaterThan<T> extends AbstractSingleValueCondition<T> {
24+
public class IsGreaterThan<T> extends AbstractSingleValueCondition<T>
25+
implements AbstractSingleValueCondition.Filterable<T>, AbstractSingleValueCondition.Mappable<T> {
2526
private static final IsGreaterThan<?> EMPTY = new IsGreaterThan<Object>(-1) {
2627
@Override
2728
public Object value() {

‎src/main/java/org/mybatis/dynamic/sql/where/condition/IsGreaterThanOrEqualTo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2323

24-
public class IsGreaterThanOrEqualTo<T> extends AbstractSingleValueCondition<T> {
24+
public class IsGreaterThanOrEqualTo<T> extends AbstractSingleValueCondition<T>
25+
implements AbstractSingleValueCondition.Filterable<T>, AbstractSingleValueCondition.Mappable<T> {
2526
private static final IsGreaterThanOrEqualTo<?> EMPTY = new IsGreaterThanOrEqualTo<Object>(-1) {
2627
@Override
2728
public Object value() {

‎src/main/java/org/mybatis/dynamic/sql/where/condition/IsLessThan.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2323

24-
public class IsLessThan<T> extends AbstractSingleValueCondition<T> {
24+
public class IsLessThan<T> extends AbstractSingleValueCondition<T>
25+
implements AbstractSingleValueCondition.Filterable<T>, AbstractSingleValueCondition.Mappable<T> {
26+
2527
private static final IsLessThan<?> EMPTY = new IsLessThan<Object>(-1) {
2628
@Override
2729
public Object value() {

‎src/main/java/org/mybatis/dynamic/sql/where/condition/IsLessThanOrEqualTo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2323

24-
public class IsLessThanOrEqualTo<T> extends AbstractSingleValueCondition<T> {
24+
public class IsLessThanOrEqualTo<T> extends AbstractSingleValueCondition<T>
25+
implements AbstractSingleValueCondition.Filterable<T>, AbstractSingleValueCondition.Mappable<T> {
2526
private static final IsLessThanOrEqualTo<?> EMPTY = new IsLessThanOrEqualTo<Object>(-1) {
2627
@Override
2728
public Object value() {

‎src/main/java/org/mybatis/dynamic/sql/where/condition/IsLike.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2323

24-
public class IsLike<T> extends AbstractSingleValueCondition<T> {
24+
public class IsLike<T> extends AbstractSingleValueCondition<T>
25+
implements AbstractSingleValueCondition.Filterable<T>, AbstractSingleValueCondition.Mappable<T> {
26+
2527
private static final IsLike<?> EMPTY = new IsLike<Object>(-1) {
2628
@Override
2729
public Object value() {

‎src/main/java/org/mybatis/dynamic/sql/where/condition/IsLikeCaseInsensitive.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import org.mybatis.dynamic.sql.util.StringUtilities;
2424

2525
public class IsLikeCaseInsensitive<T> extends AbstractSingleValueCondition<T>
26-
implements CaseInsensitiveRenderableCondition<T> {
26+
implements CaseInsensitiveRenderableCondition<T>, AbstractSingleValueCondition.Filterable<T>,
27+
AbstractSingleValueCondition.Mappable<T> {
2728
private static final IsLikeCaseInsensitive<?> EMPTY = new IsLikeCaseInsensitive<>("") { //$NON-NLS-1$
2829
@Override
2930
public String value() {

‎src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotEqualTo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2323

24-
public class IsNotEqualTo<T> extends AbstractSingleValueCondition<T> {
24+
public class IsNotEqualTo<T> extends AbstractSingleValueCondition<T>
25+
implements AbstractSingleValueCondition.Filterable<T>, AbstractSingleValueCondition.Mappable<T> {
2526
private static final IsNotEqualTo<?> EMPTY = new IsNotEqualTo<Object>(-1) {
2627
@Override
2728
public Object value() {

‎src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotLike.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2323

24-
public class IsNotLike<T> extends AbstractSingleValueCondition<T> {
24+
public class IsNotLike<T> extends AbstractSingleValueCondition<T>
25+
implements AbstractSingleValueCondition.Filterable<T>, AbstractSingleValueCondition.Mappable<T> {
2526
private static final IsNotLike<?> EMPTY = new IsNotLike<Object>(-1) {
2627
@Override
2728
public Object value() {

‎src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotLikeCaseInsensitive.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import org.mybatis.dynamic.sql.util.StringUtilities;
2424

2525
public class IsNotLikeCaseInsensitive<T> extends AbstractSingleValueCondition<T>
26-
implements CaseInsensitiveRenderableCondition<T> {
26+
implements CaseInsensitiveRenderableCondition<T>, AbstractSingleValueCondition.Filterable<T>,
27+
AbstractSingleValueCondition.Mappable<T> {
2728
private static final IsNotLikeCaseInsensitive<?> EMPTY = new IsNotLikeCaseInsensitive<>("") { //$NON-NLS-1$
2829
@Override
2930
public String value() {
@@ -56,16 +57,7 @@ public IsNotLikeCaseInsensitive<T> filter(Predicate<? super T> predicate) {
5657
return filterSupport(predicate, IsNotLikeCaseInsensitive::empty, this);
5758
}
5859

59-
/**
60-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
61-
* condition that will not render (this).
62-
*
63-
* @param mapper
64-
* a mapping function to apply to the value, if renderable
65-
*
66-
* @return a new condition with the result of applying the mapper to the value of this condition, if renderable,
67-
* otherwise a condition that will not render.
68-
*/
60+
@Override
6961
public <R> IsNotLikeCaseInsensitive<R> map(Function<? super T, ? extends R> mapper) {
7062
return mapSupport(mapper, IsNotLikeCaseInsensitive::new, IsNotLikeCaseInsensitive::empty);
7163
}

‎src/test/java/examples/mysql/IsLikeEscape.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
2828

2929
@NullMarked
30-
public class IsLikeEscape<T> extends AbstractSingleValueCondition<T> {
30+
public class IsLikeEscape<T> extends AbstractSingleValueCondition<T>
31+
implements AbstractSingleValueCondition.Filterable<T>, AbstractSingleValueCondition.Mappable<T> {
3132
private static final IsLikeEscape<?> EMPTY = new IsLikeEscape<Object>(-1, null) {
3233
@Override
3334
public Object value() {

‎src/test/kotlin/examples/kotlin/mybatis3/mariadb/KIsLikeEscape.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import org.mybatis.dynamic.sql.util.FragmentAndParameters
1010
sealed class KIsLikeEscape<T : Any>(
1111
value: T,
1212
private val escapeCharacter: Char? = null
13-
) : AbstractSingleValueCondition<T>(value) {
13+
) : AbstractSingleValueCondition<T>(value), AbstractSingleValueCondition.Filterable<T>,
14+
AbstractSingleValueCondition.Mappable<T> {
1415

1516
override fun operator(): String = "like"
1617

0 commit comments

Comments
 (0)
Failed to load comments.