Skip to content

Commit 1a2b918

Browse files
committed
Make filter and map optional on two value conditions
1 parent 43fdf06 commit 1a2b918

File tree

3 files changed

+79
-47
lines changed

3 files changed

+79
-47
lines changed

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

Lines changed: 75 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -67,51 +67,6 @@ protected <R, S extends AbstractTwoValueCondition<R>> S mapSupport(Function<? su
6767
}
6868
}
6969

70-
/**
71-
* If renderable and the values match the predicate, returns this condition. Else returns a condition
72-
* that will not render.
73-
*
74-
* @param predicate predicate applied to the values, if renderable
75-
* @return this condition if renderable and the values match the predicate, otherwise a condition
76-
* that will not render.
77-
*/
78-
public abstract AbstractTwoValueCondition<T> filter(BiPredicate<? super T, ? super T> predicate);
79-
80-
/**
81-
* If renderable and both values match the predicate, returns this condition. Else returns a condition
82-
* that will not render. This function implements a short-circuiting test. If the
83-
* first value does not match the predicate, then the second value will not be tested.
84-
*
85-
* @param predicate predicate applied to both values, if renderable
86-
* @return this condition if renderable and the values match the predicate, otherwise a condition
87-
* that will not render.
88-
*/
89-
public abstract AbstractTwoValueCondition<T> filter(Predicate<? super T> predicate);
90-
91-
/**
92-
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
93-
* condition that will not render (this).
94-
*
95-
* @param mapper1 a mapping function to apply to the first value, if renderable
96-
* @param mapper2 a mapping function to apply to the second value, if renderable
97-
* @param <R> type of the new condition
98-
* @return a new condition with the result of applying the mappers to the values of this condition,
99-
* if renderable, otherwise a condition that will not render.
100-
*/
101-
public abstract <R> AbstractTwoValueCondition<R> map(Function<? super T, ? extends R> mapper1,
102-
Function<? super T, ? extends R> mapper2);
103-
104-
/**
105-
* If renderable, apply the mapping to both values and return a new condition with the new values. Else return a
106-
* condition that will not render (this).
107-
*
108-
* @param mapper a mapping function to apply to both values, if renderable
109-
* @param <R> type of the new condition
110-
* @return a new condition with the result of applying the mappers to the values of this condition,
111-
* if renderable, otherwise a condition that will not render.
112-
*/
113-
public abstract <R> AbstractTwoValueCondition<R> map(Function<? super T, ? extends R> mapper);
114-
11570
public abstract String operator1();
11671

11772
public abstract String operator2();
@@ -131,4 +86,79 @@ public FragmentAndParameters renderCondition(RenderingContext renderingContext,
13186
.withParameter(parameterInfo2.parameterMapKey(), leftColumn.convertParameterType(value2()))
13287
.build();
13388
}
89+
90+
/**
91+
* Conditions may implement Filterable to add optionality to rendering.
92+
*
93+
* <p>If a condition is Filterable, then a user may add a filter to the usage of the condition that makes a decision
94+
* whether to render the condition at runtime. Conditions that fail the filter will be dropped from the
95+
* rendered SQL.
96+
*
97+
* <p>Implementations of Filterable may call
98+
* {@link AbstractTwoValueCondition#filterSupport(Predicate, Supplier, AbstractTwoValueCondition)}
99+
* or {@link AbstractTwoValueCondition#filterSupport(BiPredicate, Supplier, AbstractTwoValueCondition)} as
100+
* a common implementation of the filtering algorithm.
101+
*
102+
* @param <T> the Java type related to the database column type
103+
*/
104+
public interface Filterable<T> {
105+
/**
106+
* If renderable and the values match the predicate, returns this condition. Else returns a condition
107+
* that will not render.
108+
*
109+
* @param predicate predicate applied to the values, if renderable
110+
* @return this condition if renderable and the values match the predicate, otherwise a condition
111+
* that will not render.
112+
*/
113+
AbstractTwoValueCondition<T> filter(BiPredicate<? super T, ? super T> predicate);
114+
115+
/**
116+
* If renderable and both values match the predicate, returns this condition. Else returns a condition
117+
* that will not render. This function implements a short-circuiting test. If the
118+
* first value does not match the predicate, then the second value will not be tested.
119+
*
120+
* @param predicate predicate applied to both values, if renderable
121+
* @return this condition if renderable and the values match the predicate, otherwise a condition
122+
* that will not render.
123+
*/
124+
AbstractTwoValueCondition<T> filter(Predicate<? super T> predicate);
125+
}
126+
127+
/**
128+
* Conditions may implement Mappable to alter condition values or types during rendering.
129+
*
130+
* <p>If a condition is Mappable, then a user may add a mapper to the usage of the condition that can alter the
131+
* values of a condition, or change that datatype.
132+
*
133+
* <p>Implementations of Mappable may call
134+
* {@link AbstractTwoValueCondition#mapSupport(Function, Function, BiFunction, Supplier)} as
135+
* a common implementation of the mapping algorithm.
136+
*
137+
* @param <T> the Java type related to the database column type
138+
*/
139+
public interface Mappable<T> {
140+
/**
141+
* If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
142+
* condition that will not render (this).
143+
*
144+
* @param mapper1 a mapping function to apply to the first value, if renderable
145+
* @param mapper2 a mapping function to apply to the second value, if renderable
146+
* @param <R> type of the new condition
147+
* @return a new condition with the result of applying the mappers to the values of this condition,
148+
* if renderable, otherwise a condition that will not render.
149+
*/
150+
<R> AbstractTwoValueCondition<R> map(Function<? super T, ? extends R> mapper1,
151+
Function<? super T, ? extends R> mapper2);
152+
153+
/**
154+
* If renderable, apply the mapping to both values and return a new condition with the new values. Else return a
155+
* condition that will not render (this).
156+
*
157+
* @param mapper a mapping function to apply to both values, if renderable
158+
* @param <R> type of the new condition
159+
* @return a new condition with the result of applying the mappers to the values of this condition,
160+
* if renderable, otherwise a condition that will not render.
161+
*/
162+
<R> AbstractTwoValueCondition<R> map(Function<? super T, ? extends R> mapper);
163+
}
134164
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import org.jspecify.annotations.Nullable;
2424
import org.mybatis.dynamic.sql.AbstractTwoValueCondition;
2525

26-
public class IsBetween<T> extends AbstractTwoValueCondition<T> {
26+
public class IsBetween<T> extends AbstractTwoValueCondition<T>
27+
implements AbstractTwoValueCondition.Filterable<T>, AbstractTwoValueCondition.Mappable<T> {
2728
private static final IsBetween<?> EMPTY = new IsBetween<Object>(-1, -1) {
2829
@Override
2930
public Object value1() {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import org.jspecify.annotations.Nullable;
2424
import org.mybatis.dynamic.sql.AbstractTwoValueCondition;
2525

26-
public class IsNotBetween<T> extends AbstractTwoValueCondition<T> {
26+
public class IsNotBetween<T> extends AbstractTwoValueCondition<T>
27+
implements AbstractTwoValueCondition.Filterable<T>, AbstractTwoValueCondition.Mappable<T> {
2728
private static final IsNotBetween<?> EMPTY = new IsNotBetween<Object>(-1, -1) {
2829
@Override
2930
public Object value1() {

0 commit comments

Comments
 (0)