Skip to content

Commit 91a9c8b

Browse files
committed
Implement filter for no value conditions
1 parent cb4d3ae commit 91a9c8b

File tree

3 files changed

+91
-31
lines changed

3 files changed

+91
-31
lines changed

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,26 +15,8 @@
1515
*/
1616
package org.mybatis.dynamic.sql;
1717

18-
import java.util.Objects;
19-
import java.util.function.BooleanSupplier;
20-
2118
public abstract class AbstractNoValueCondition<T> implements VisitableCondition<T> {
2219

23-
private final BooleanSupplier booleanSupplier;
24-
25-
protected AbstractNoValueCondition() {
26-
booleanSupplier = () -> true;
27-
}
28-
29-
protected AbstractNoValueCondition(BooleanSupplier booleanSupplier) {
30-
this.booleanSupplier = Objects.requireNonNull(booleanSupplier);
31-
}
32-
33-
@Override
34-
public boolean shouldRender() {
35-
return booleanSupplier.getAsBoolean();
36-
}
37-
3820
@Override
3921
public <R> R accept(ConditionVisitor<T, R> visitor) {
4022
return visitor.visit(this);

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

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,16 +25,55 @@ public IsNotNull() {
2525
super();
2626
}
2727

28-
protected IsNotNull(BooleanSupplier booleanSupplier) {
29-
super(booleanSupplier);
30-
}
31-
3228
@Override
3329
public String renderCondition(String columnName) {
3430
return columnName + " is not null"; //$NON-NLS-1$
3531
}
3632

33+
/**
34+
* If the supplier returns true, returns this condition. Else returns a new condition that will not render.
35+
*
36+
* @deprecated replaced by {@link IsNotNull#filter(BooleanSupplier)}
37+
* @param booleanSupplier function that specifies whether the condition should render
38+
* @param <S> condition type - not used except for compilation compliance
39+
* @return If the condition should render, returns this condition. Else a new condition that will not
40+
* render.
41+
*/
42+
@Deprecated
3743
public <S> IsNotNull<S> when(BooleanSupplier booleanSupplier) {
38-
return new IsNotNull<>(booleanSupplier);
44+
return filter(booleanSupplier);
45+
}
46+
47+
/**
48+
* If the supplier returns true, returns this condition. Else returns a new condition that will not render.
49+
*
50+
* @param booleanSupplier function that specifies whether the condition should render
51+
* @param <S> condition type - not used except for compilation compliance
52+
* @return If the condition should render, returns this condition. Else a new condition that will not
53+
* render.
54+
*/
55+
public <S> IsNotNull<S> filter(BooleanSupplier booleanSupplier) {
56+
if (booleanSupplier.getAsBoolean()) {
57+
@SuppressWarnings("unchecked")
58+
IsNotNull<S> self = (IsNotNull<S>) this;
59+
return self;
60+
} else {
61+
return EmptyIsNotNull.empty();
62+
}
63+
}
64+
65+
public static class EmptyIsNotNull<T> extends IsNotNull<T> {
66+
private static final IsNotNull<?> EMPTY = new EmptyIsNotNull<>();
67+
68+
public static <T> EmptyIsNotNull<T> empty() {
69+
@SuppressWarnings("unchecked")
70+
EmptyIsNotNull<T> t = (EmptyIsNotNull<T>) EMPTY;
71+
return t;
72+
}
73+
74+
@Override
75+
public boolean shouldRender() {
76+
return false;
77+
}
3978
}
4079
}

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

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,16 +25,55 @@ public IsNull() {
2525
super();
2626
}
2727

28-
protected IsNull(BooleanSupplier booleanSupplier) {
29-
super(booleanSupplier);
30-
}
31-
3228
@Override
3329
public String renderCondition(String columnName) {
3430
return columnName + " is null"; //$NON-NLS-1$
3531
}
3632

33+
/**
34+
* If the supplier returns true, returns this condition. Else returns a new condition that will not render.
35+
*
36+
* @deprecated replaced by {@link IsNull#filter(BooleanSupplier)}
37+
* @param booleanSupplier function that specifies whether the condition should render
38+
* @param <S> condition type - not used except for compilation compliance
39+
* @return If the condition should render, returns this condition. Else a new condition that will not
40+
* render.
41+
*/
42+
@Deprecated
3743
public <S> IsNull<S> when(BooleanSupplier booleanSupplier) {
38-
return new IsNull<>(booleanSupplier);
44+
return filter(booleanSupplier);
45+
}
46+
47+
/**
48+
* If the supplier returns true, returns this condition. Else returns a new condition that will not render.
49+
*
50+
* @param booleanSupplier function that specifies whether the condition should render
51+
* @param <S> condition type - not used except for compilation compliance
52+
* @return If the condition should render, returns this condition. Else a new condition that will not
53+
* render.
54+
*/
55+
public <S> IsNull<S> filter(BooleanSupplier booleanSupplier) {
56+
if (booleanSupplier.getAsBoolean()) {
57+
@SuppressWarnings("unchecked")
58+
IsNull<S> self = (IsNull<S>) this;
59+
return self;
60+
} else {
61+
return EmptyIsNull.empty();
62+
}
63+
}
64+
65+
public static class EmptyIsNull<T> extends IsNull<T> {
66+
private static final EmptyIsNull<?> EMPTY = new EmptyIsNull<>();
67+
68+
public static <T> EmptyIsNull<T> empty() {
69+
@SuppressWarnings("unchecked")
70+
EmptyIsNull<T> t = (EmptyIsNull<T>) EMPTY;
71+
return t;
72+
}
73+
74+
@Override
75+
public boolean shouldRender() {
76+
return false;
77+
}
3978
}
4079
}

0 commit comments

Comments
 (0)