Skip to content

Commit 239abff

Browse files
committed
Refactor filter algorithm into base class for no value Conditions
1 parent b7ea1a3 commit 239abff

File tree

4 files changed

+67
-17
lines changed

4 files changed

+67
-17
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@
1818
import java.util.function.BooleanSupplier;
1919
import java.util.function.Supplier;
2020

21-
public abstract class AbstractNoValueCondition<T>
22-
implements VisitableCondition<T> {
21+
public abstract class AbstractNoValueCondition<T> implements VisitableCondition<T> {
2322

2423
@Override
2524
public <R> R accept(ConditionVisitor<T, R> visitor) {
2625
return visitor.visit(this);
2726
}
2827

2928
protected <S> S filter(BooleanSupplier booleanSupplier, Supplier<S> empty, S self) {
30-
if (booleanSupplier.getAsBoolean()) {
31-
return self;
29+
if (shouldRender()) {
30+
return booleanSupplier.getAsBoolean() ? self : empty.get();
3231
} else {
33-
return empty.get();
32+
return self;
3433
}
3534
}
3635

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,28 @@ public String renderCondition(String columnName) {
4343
}
4444

4545
/**
46-
* If the supplier returns true, returns this condition. Else returns a condition that will not render.
46+
* If renderable and the supplier returns true, returns this condition. Else returns a condition
47+
* that will not render.
4748
*
4849
* @deprecated replaced by {@link IsNotNull#filter(BooleanSupplier)}
4950
* @param booleanSupplier function that specifies whether the condition should render
5051
* @param <S> condition type - not used except for compilation compliance
51-
* @return If the condition should render, returns this condition. Else a condition that will not
52-
* render.
52+
* @return this condition if renderable and the supplier returns true, otherwise a condition
53+
* that will not render.
5354
*/
5455
@Deprecated
5556
public <S> IsNotNull<S> when(BooleanSupplier booleanSupplier) {
5657
return filter(booleanSupplier);
5758
}
5859

5960
/**
60-
* If the supplier returns true, returns this condition. Else returns a condition that will not render.
61+
* If renderable and the supplier returns true, returns this condition. Else returns a condition
62+
* that will not render.
6163
*
6264
* @param booleanSupplier function that specifies whether the condition should render
6365
* @param <S> condition type - not used except for compilation compliance
64-
* @return If the condition should render, returns this condition. Else a condition that will not
65-
* render.
66+
* @return this condition if renderable and the supplier returns true, otherwise a condition
67+
* that will not render.
6668
*/
6769
public <S> IsNotNull<S> filter(BooleanSupplier booleanSupplier) {
6870
@SuppressWarnings("unchecked")

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,28 @@ public String renderCondition(String columnName) {
4343
}
4444

4545
/**
46-
* If the supplier returns true, returns this condition. Else returns a condition that will not render.
46+
* If renderable and the supplier returns true, returns this condition. Else returns a condition
47+
* that will not render.
4748
*
4849
* @deprecated replaced by {@link IsNull#filter(BooleanSupplier)}
4950
* @param booleanSupplier function that specifies whether the condition should render
5051
* @param <S> condition type - not used except for compilation compliance
51-
* @return If the condition should render, returns this condition. Else a condition that will not
52-
* render.
52+
* @return this condition if renderable and the supplier returns true, otherwise a condition
53+
* that will not render.
5354
*/
5455
@Deprecated
5556
public <S> IsNull<S> when(BooleanSupplier booleanSupplier) {
5657
return filter(booleanSupplier);
5758
}
5859

5960
/**
60-
* If the supplier returns true, returns this condition. Else returns a condition that will not render.
61+
* If renderable and the supplier returns true, returns this condition. Else returns a condition
62+
* that will not render.
6163
*
6264
* @param booleanSupplier function that specifies whether the condition should render
6365
* @param <S> condition type - not used except for compilation compliance
64-
* @return If the condition should render, returns this condition. Else a condition that will not
65-
* render.
66+
* @return this condition if renderable and the supplier returns true, otherwise a condition
67+
* that will not render.
6668
*/
6769
public <S> IsNull<S> filter(BooleanSupplier booleanSupplier) {
6870
@SuppressWarnings("unchecked")

src/test/java/org/mybatis/dynamic/sql/where/condition/FilterAndMapTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,53 @@
2525
import java.util.stream.Collectors;
2626

2727
class FilterAndMapTest {
28+
@Test
29+
void testIsNullRenderableTruePredicateShouldReturnSameObject() {
30+
IsNull<String> cond = SqlBuilder.isNull();
31+
IsNull<String> filtered = cond.filter(() -> true);
32+
assertThat(filtered.shouldRender()).isTrue();
33+
assertThat(cond).isSameAs(filtered);
34+
}
35+
36+
@Test
37+
void testIsNullRenderableFalsePredicate() {
38+
IsNull<String> cond = SqlBuilder.isNull();
39+
IsNull<String> filtered = cond.filter(() -> false);
40+
assertThat(cond.shouldRender()).isTrue();
41+
assertThat(filtered.shouldRender()).isFalse();
42+
}
43+
44+
@Test
45+
void testIsNullFilterUnRenderableShouldReturnSameObject() {
46+
IsNull<String> cond = SqlBuilder.isNull().filter(() -> false);
47+
IsNull<String> filtered = cond.filter(() -> true);
48+
assertThat(filtered.shouldRender()).isFalse();
49+
assertThat(cond).isSameAs(filtered);
50+
}
51+
52+
@Test
53+
void testIsNotNullRenderableTruePredicateShouldReturnSameObject() {
54+
IsNotNull<String> cond = SqlBuilder.isNotNull();
55+
IsNotNull<String> filtered = cond.filter(() -> true);
56+
assertThat(filtered.shouldRender()).isTrue();
57+
assertThat(cond).isSameAs(filtered);
58+
}
59+
60+
@Test
61+
void testIsNotNullRenderableFalsePredicate() {
62+
IsNotNull<String> cond = SqlBuilder.isNotNull();
63+
IsNotNull<String> filtered = cond.filter(() -> false);
64+
assertThat(cond.shouldRender()).isTrue();
65+
assertThat(filtered.shouldRender()).isFalse();
66+
}
67+
68+
@Test
69+
void testIsNotNullFilterUnRenderableShouldReturnSameObject() {
70+
IsNotNull<String> cond = SqlBuilder.isNotNull().filter(() -> false);
71+
IsNotNull<String> filtered = cond.filter(() -> true);
72+
assertThat(filtered.shouldRender()).isFalse();
73+
assertThat(cond).isSameAs(filtered);
74+
}
2875

2976
@Test
3077
void testIsEqualRenderableTruePredicateShouldReturnSameObject() {

0 commit comments

Comments
 (0)