Skip to content

Commit 21e4c96

Browse files
committed
Refactor filter/map algorithm into base class for single value Conditions
1 parent c6778d5 commit 21e4c96

13 files changed

+95
-233
lines changed

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

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

18-
public abstract class AbstractSingleValueCondition<T> implements VisitableCondition<T> {
18+
import java.util.function.Function;
19+
import java.util.function.Predicate;
20+
import java.util.function.Supplier;
21+
import java.util.function.UnaryOperator;
22+
23+
public abstract class AbstractSingleValueCondition<T, S extends AbstractSingleValueCondition<T, S>>
24+
implements VisitableCondition<T> {
1925
protected final T value;
2026

2127
protected AbstractSingleValueCondition(T value) {
@@ -31,5 +37,41 @@ public <R> R accept(ConditionVisitor<T, R> visitor) {
3137
return visitor.visit(this);
3238
}
3339

40+
protected S filter(Predicate<T> predicate, Supplier<S> empty, S self) {
41+
if (shouldRender()) {
42+
return predicate.test(value) ? self : empty.get();
43+
} else {
44+
return self;
45+
}
46+
}
47+
48+
protected S map(UnaryOperator<T> mapper, Function<T, S> constructor, S self) {
49+
if (shouldRender()) {
50+
return constructor.apply(mapper.apply(value));
51+
}else {
52+
return self;
53+
}
54+
}
55+
56+
/**
57+
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
58+
* that will not render.
59+
*
60+
* @param predicate predicate applied to the value, if renderable
61+
* @return this condition if renderable and the value matches the predicate, otherwise a condition
62+
* that will not render.
63+
*/
64+
public abstract S filter(Predicate<T> predicate);
65+
66+
/**
67+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
68+
* condition that will not render (this).
69+
*
70+
* @param mapper a mapping function to apply to the value, if renderable
71+
* @return a new condition with the result of applying the mapper to the value of this condition,
72+
* if renderable, otherwise a condition that will not render.
73+
*/
74+
public abstract S map(UnaryOperator<T> mapper);
75+
3476
public abstract String renderCondition(String columnName, String placeholder);
3577
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface ConditionVisitor<T, R> {
2020

2121
R visit(AbstractNoValueCondition<T> condition);
2222

23-
R visit(AbstractSingleValueCondition<T> condition);
23+
R visit(AbstractSingleValueCondition<T, ?> condition);
2424

2525
R visit(AbstractTwoValueCondition<T, ?> condition);
2626

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
public class IsEqualTo<T> extends AbstractSingleValueCondition<T> {
23+
public class IsEqualTo<T> extends AbstractSingleValueCondition<T, IsEqualTo<T>> {
2424

2525
protected IsEqualTo(T value) {
2626
super(value);
@@ -63,32 +63,14 @@ public IsEqualTo<T> then(UnaryOperator<T> mapper) {
6363
return map(mapper);
6464
}
6565

66-
/**
67-
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
68-
* that will not render.
69-
*
70-
* @param predicate predicate applied to the value, if renderable
71-
* @return this condition if renderable and the value matches the predicate, otherwise a condition
72-
* that will not render.
73-
*/
66+
@Override
7467
public IsEqualTo<T> filter(Predicate<T> predicate) {
75-
if (shouldRender()) {
76-
return predicate.test(value) ? this : EmptyIsEqualTo.empty();
77-
} else {
78-
return this;
79-
}
68+
return filter(predicate, EmptyIsEqualTo::empty, this);
8069
}
8170

82-
/**
83-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
84-
* condition that will not render (this).
85-
*
86-
* @param mapper a mapping function to apply to the value, if renderable
87-
* @return a new condition with the result of applying the mapper to the value of this condition,
88-
* if renderable, otherwise a condition that will not render.
89-
*/
71+
@Override
9072
public IsEqualTo<T> map(UnaryOperator<T> mapper) {
91-
return shouldRender() ? new IsEqualTo<>(mapper.apply(value)) : this;
73+
return map(mapper, IsEqualTo::new, this);
9274
}
9375

9476
public static class EmptyIsEqualTo<T> extends IsEqualTo<T> {

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
public class IsGreaterThan<T> extends AbstractSingleValueCondition<T> {
23+
public class IsGreaterThan<T> extends AbstractSingleValueCondition<T, IsGreaterThan<T>> {
2424

2525
protected IsGreaterThan(T value) {
2626
super(value);
@@ -63,32 +63,14 @@ public IsGreaterThan<T> then(UnaryOperator<T> mapper) {
6363
return map(mapper);
6464
}
6565

66-
/**
67-
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
68-
* that will not render.
69-
*
70-
* @param predicate predicate applied to the value, if renderable
71-
* @return this condition if renderable and the value matches the predicate, otherwise a condition
72-
* that will not render.
73-
*/
66+
@Override
7467
public IsGreaterThan<T> filter(Predicate<T> predicate) {
75-
if (shouldRender()) {
76-
return predicate.test(value) ? this : EmptyIsGreaterThan.empty();
77-
} else {
78-
return this;
79-
}
68+
return filter(predicate, EmptyIsGreaterThan::empty, this);
8069
}
8170

82-
/**
83-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
84-
* condition that will not render (this).
85-
*
86-
* @param mapper a mapping function to apply to the value, if renderable
87-
* @return a new condition with the result of applying the mapper to the value of this condition,
88-
* if renderable, otherwise a condition that will not render.
89-
*/
71+
@Override
9072
public IsGreaterThan<T> map(UnaryOperator<T> mapper) {
91-
return shouldRender() ? new IsGreaterThan<>(mapper.apply(value)) : this;
73+
return map(mapper, IsGreaterThan::new, this);
9274
}
9375

9476
public static class EmptyIsGreaterThan<T> extends IsGreaterThan<T> {

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
public class IsGreaterThanOrEqualTo<T> extends AbstractSingleValueCondition<T> {
23+
public class IsGreaterThanOrEqualTo<T> extends AbstractSingleValueCondition<T, IsGreaterThanOrEqualTo<T>> {
2424

2525
protected IsGreaterThanOrEqualTo(T value) {
2626
super(value);
@@ -63,32 +63,14 @@ public IsGreaterThanOrEqualTo<T> then(UnaryOperator<T> mapper) {
6363
return map(mapper);
6464
}
6565

66-
/**
67-
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
68-
* that will not render.
69-
*
70-
* @param predicate predicate applied to the value, if renderable
71-
* @return this condition if renderable and the value matches the predicate, otherwise a condition
72-
* that will not render.
73-
*/
66+
@Override
7467
public IsGreaterThanOrEqualTo<T> filter(Predicate<T> predicate) {
75-
if (shouldRender()) {
76-
return predicate.test(value) ? this : EmptyIsGreaterThanOrEqualTo.empty();
77-
} else {
78-
return this;
79-
}
68+
return filter(predicate, EmptyIsGreaterThanOrEqualTo::empty, this);
8069
}
8170

82-
/**
83-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
84-
* condition that will not render (this).
85-
*
86-
* @param mapper a mapping function to apply to the value, if renderable
87-
* @return a new condition with the result of applying the mapper to the value of this condition,
88-
* if renderable, otherwise a condition that will not render.
89-
*/
71+
@Override
9072
public IsGreaterThanOrEqualTo<T> map(UnaryOperator<T> mapper) {
91-
return shouldRender() ? new IsGreaterThanOrEqualTo<>(mapper.apply(value)) : this;
73+
return map(mapper, IsGreaterThanOrEqualTo::new, this);
9274
}
9375

9476
public static class EmptyIsGreaterThanOrEqualTo<T> extends IsGreaterThanOrEqualTo<T> {

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
public class IsLessThan<T> extends AbstractSingleValueCondition<T> {
23+
public class IsLessThan<T> extends AbstractSingleValueCondition<T, IsLessThan<T>> {
2424

2525
protected IsLessThan(T value) {
2626
super(value);
@@ -63,32 +63,14 @@ public IsLessThan<T> then(UnaryOperator<T> mapper) {
6363
return map(mapper);
6464
}
6565

66-
/**
67-
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
68-
* that will not render.
69-
*
70-
* @param predicate predicate applied to the value, if renderable
71-
* @return this condition if renderable and the value matches the predicate, otherwise a condition
72-
* that will not render.
73-
*/
66+
@Override
7467
public IsLessThan<T> filter(Predicate<T> predicate) {
75-
if (shouldRender()) {
76-
return predicate.test(value) ? this : EmptyIsLessThan.empty();
77-
} else {
78-
return this;
79-
}
68+
return filter(predicate, EmptyIsLessThan::empty, this);
8069
}
8170

82-
/**
83-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
84-
* condition that will not render (this).
85-
*
86-
* @param mapper a mapping function to apply to the value, if renderable
87-
* @return a new condition with the result of applying the mapper to the value of this condition,
88-
* if renderable, otherwise a condition that will not render.
89-
*/
71+
@Override
9072
public IsLessThan<T> map(UnaryOperator<T> mapper) {
91-
return shouldRender() ? new IsLessThan<>(mapper.apply(value)) : this;
73+
return map(mapper, IsLessThan::new, this);
9274
}
9375

9476
public static class EmptyIsLessThan<T> extends IsLessThan<T> {

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
public class IsLessThanOrEqualTo<T> extends AbstractSingleValueCondition<T> {
23+
public class IsLessThanOrEqualTo<T> extends AbstractSingleValueCondition<T, IsLessThanOrEqualTo<T>> {
2424

2525
protected IsLessThanOrEqualTo(T value) {
2626
super(value);
@@ -63,32 +63,14 @@ public IsLessThanOrEqualTo<T> then(UnaryOperator<T> mapper) {
6363
return map(mapper);
6464
}
6565

66-
/**
67-
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
68-
* that will not render.
69-
*
70-
* @param predicate predicate applied to the value, if renderable
71-
* @return this condition if renderable and the value matches the predicate, otherwise a condition
72-
* that will not render.
73-
*/
66+
@Override
7467
public IsLessThanOrEqualTo<T> filter(Predicate<T> predicate) {
75-
if (shouldRender()) {
76-
return predicate.test(value) ? this : EmptyIsLessThanOrEqualTo.empty();
77-
} else {
78-
return this;
79-
}
68+
return filter(predicate, EmptyIsLessThanOrEqualTo::empty, this);
8069
}
8170

82-
/**
83-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
84-
* condition that will not render (this).
85-
*
86-
* @param mapper a mapping function to apply to the value, if renderable
87-
* @return a new condition with the result of applying the mapper to the value of this condition,
88-
* if renderable, otherwise a condition that will not render.
89-
*/
71+
@Override
9072
public IsLessThanOrEqualTo<T> map(UnaryOperator<T> mapper) {
91-
return shouldRender() ? new IsLessThanOrEqualTo<>(mapper.apply(value)) : this;
73+
return map(mapper, IsLessThanOrEqualTo::new, this);
9274
}
9375

9476
public static class EmptyIsLessThanOrEqualTo<T> extends IsLessThanOrEqualTo<T> {

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
public class IsLike<T> extends AbstractSingleValueCondition<T> {
23+
public class IsLike<T> extends AbstractSingleValueCondition<T, IsLike<T>> {
2424

2525
protected IsLike(T value) {
2626
super(value);
@@ -63,32 +63,14 @@ public IsLike<T> then(UnaryOperator<T> mapper) {
6363
return map(mapper);
6464
}
6565

66-
/**
67-
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
68-
* that will not render.
69-
*
70-
* @param predicate predicate applied to the value, if renderable
71-
* @return this condition if renderable and the value matches the predicate, otherwise a condition
72-
* that will not render.
73-
*/
66+
@Override
7467
public IsLike<T> filter(Predicate<T> predicate) {
75-
if (shouldRender()) {
76-
return predicate.test(value) ? this : EmptyIsLike.empty();
77-
} else {
78-
return this;
79-
}
68+
return filter(predicate, EmptyIsLike::empty, this);
8069
}
8170

82-
/**
83-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
84-
* condition that will not render (this).
85-
*
86-
* @param mapper a mapping function to apply to the value, if renderable
87-
* @return a new condition with the result of applying the mapper to the value of this condition,
88-
* if renderable, otherwise a condition that will not render.
89-
*/
71+
@Override
9072
public IsLike<T> map(UnaryOperator<T> mapper) {
91-
return shouldRender() ? new IsLike<>(mapper.apply(value)) : this;
73+
return map(mapper, IsLike::new, this);
9274
}
9375

9476
public static class EmptyIsLike<T> extends IsLike<T> {

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2222
import org.mybatis.dynamic.sql.util.StringUtilities;
2323

24-
public class IsLikeCaseInsensitive extends AbstractSingleValueCondition<String> {
24+
public class IsLikeCaseInsensitive extends AbstractSingleValueCondition<String, IsLikeCaseInsensitive> {
2525
protected IsLikeCaseInsensitive(String value) {
2626
super(value);
2727
}
@@ -68,32 +68,14 @@ public IsLikeCaseInsensitive then(UnaryOperator<String> mapper) {
6868
return map(mapper);
6969
}
7070

71-
/**
72-
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
73-
* that will not render.
74-
*
75-
* @param predicate predicate applied to the value, if renderable
76-
* @return this condition if renderable and the value matches the predicate, otherwise a condition
77-
* that will not render.
78-
*/
71+
@Override
7972
public IsLikeCaseInsensitive filter(Predicate<String> predicate) {
80-
if (shouldRender()) {
81-
return predicate.test(value) ? this : EmptyIsLikeCaseInsensitive.empty();
82-
} else {
83-
return this;
84-
}
73+
return filter(predicate, EmptyIsLikeCaseInsensitive::empty, this);
8574
}
8675

87-
/**
88-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
89-
* condition that will not render (this).
90-
*
91-
* @param mapper a mapping function to apply to the value, if renderable
92-
* @return a new condition with the result of applying the mapper to the value of this condition,
93-
* if renderable, otherwise a condition that will not render.
94-
*/
76+
@Override
9577
public IsLikeCaseInsensitive map(UnaryOperator<String> mapper) {
96-
return shouldRender() ? new IsLikeCaseInsensitive(mapper.apply(value)) : this;
78+
return map(mapper, IsLikeCaseInsensitive::new, this);
9779
}
9880

9981
public static class EmptyIsLikeCaseInsensitive extends IsLikeCaseInsensitive {

0 commit comments

Comments
 (0)