Skip to content

Commit b7ea1a3

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

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

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

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

18-
public abstract class AbstractNoValueCondition<T> implements VisitableCondition<T> {
18+
import java.util.function.BooleanSupplier;
19+
import java.util.function.Supplier;
20+
21+
public abstract class AbstractNoValueCondition<T>
22+
implements VisitableCondition<T> {
1923

2024
@Override
2125
public <R> R accept(ConditionVisitor<T, R> visitor) {
2226
return visitor.visit(this);
2327
}
2428

29+
protected <S> S filter(BooleanSupplier booleanSupplier, Supplier<S> empty, S self) {
30+
if (booleanSupplier.getAsBoolean()) {
31+
return self;
32+
} else {
33+
return empty.get();
34+
}
35+
}
36+
2537
public abstract String renderCondition(String columnName);
2638
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,8 @@ public <S> IsNotNull<S> when(BooleanSupplier booleanSupplier) {
6565
* render.
6666
*/
6767
public <S> IsNotNull<S> filter(BooleanSupplier booleanSupplier) {
68-
if (booleanSupplier.getAsBoolean()) {
69-
@SuppressWarnings("unchecked")
70-
IsNotNull<S> self = (IsNotNull<S>) this;
71-
return self;
72-
} else {
73-
return IsNotNull.empty();
74-
}
68+
@SuppressWarnings("unchecked")
69+
IsNotNull<S> self = (IsNotNull<S>) this;
70+
return filter(booleanSupplier, IsNotNull::empty, self);
7571
}
7672
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,8 @@ public <S> IsNull<S> when(BooleanSupplier booleanSupplier) {
6565
* render.
6666
*/
6767
public <S> IsNull<S> filter(BooleanSupplier booleanSupplier) {
68-
if (booleanSupplier.getAsBoolean()) {
69-
@SuppressWarnings("unchecked")
70-
IsNull<S> self = (IsNull<S>) this;
71-
return self;
72-
} else {
73-
return IsNull.empty();
74-
}
68+
@SuppressWarnings("unchecked")
69+
IsNull<S> self = (IsNull<S>) this;
70+
return filter(booleanSupplier, IsNull::empty, self);
7571
}
7672
}

0 commit comments

Comments
 (0)