Skip to content

Commit 3f85165

Browse files
committed
Refactor filter/map algorithm into base class for List Conditions
1 parent 2f1d1a8 commit 3f85165

File tree

5 files changed

+56
-110
lines changed

5 files changed

+56
-110
lines changed

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Collection;
1919
import java.util.Objects;
20+
import java.util.function.BiFunction;
2021
import java.util.function.Function;
2122
import java.util.function.Predicate;
2223
import java.util.function.UnaryOperator;
@@ -56,16 +57,53 @@ public <R> R accept(ConditionVisitor<T, R> visitor) {
5657
return visitor.visit(this);
5758
}
5859

59-
protected Collection<T> applyMapper(UnaryOperator<T> mapper) {
60+
private Collection<T> applyMapper(UnaryOperator<T> mapper) {
6061
Objects.requireNonNull(mapper);
6162
return values.stream().map(mapper).collect(Collectors.toList());
6263
}
6364

64-
protected Collection<T> applyFilter(Predicate<T> predicate) {
65+
private Collection<T> applyFilter(Predicate<T> predicate) {
6566
Objects.requireNonNull(predicate);
6667
return values.stream().filter(predicate).collect(Collectors.toList());
6768
}
6869

70+
protected S filter(Predicate<T> predicate, BiFunction<Collection<T>, Callback, S> constructor, S self) {
71+
if (shouldRender()) {
72+
return constructor.apply(applyFilter(predicate), emptyCallback);
73+
} else {
74+
return self;
75+
}
76+
}
77+
78+
protected S map(UnaryOperator<T> mapper, BiFunction<Collection<T>, Callback, S> constructor, S self) {
79+
if (shouldRender()) {
80+
return constructor.apply(applyMapper(mapper), emptyCallback);
81+
} else {
82+
return self;
83+
}
84+
}
85+
86+
/**
87+
* If renderable, apply the predicate to each value in the list and return a new condition with the filtered values.
88+
* Else returns a condition that will not render (this). If all values are filtered out of the value
89+
* list, then the condition will not render.
90+
*
91+
* @param predicate predicate applied to the values, if renderable
92+
* @return a new condition with filtered values if renderable, otherwise a condition
93+
* that will not render.
94+
*/
95+
public abstract S filter(Predicate<T> predicate);
96+
97+
/**
98+
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
99+
* Else return a condition that will not render (this).
100+
*
101+
* @param mapper a mapping function to apply to the values, if renderable
102+
* @return a new condition with mapped values if renderable, otherwise a condition
103+
* that will not render.
104+
*/
105+
public abstract S map(UnaryOperator<T> mapper);
106+
69107
public abstract S withListEmptyCallback(Callback callback);
70108

71109
public abstract String renderCondition(String columnName, Stream<String> placeholders);

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,37 +67,14 @@ public IsIn<T> then(UnaryOperator<Stream<T>> valueStreamTransformer) {
6767
return new IsIn<>(mapped, emptyCallback);
6868
}
6969

70-
/**
71-
* If renderable, apply the predicate to each value in the list and return a new condition with the filtered values.
72-
* Else returns a condition that will not render (this). If all values are filtered out of the value
73-
* list, then the condition will not render.
74-
*
75-
* @param predicate predicate applied to the values, if renderable
76-
* @return a new condition with filtered values if renderable, otherwise a condition
77-
* that will not render.
78-
*/
70+
@Override
7971
public IsIn<T> filter(Predicate<T> predicate) {
80-
if (shouldRender()) {
81-
return new IsIn<>(applyFilter(predicate), emptyCallback);
82-
} else {
83-
return this;
84-
}
72+
return filter(predicate, IsIn::new, this);
8573
}
8674

87-
/**
88-
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
89-
* Else return a condition that will not render (this).
90-
*
91-
* @param mapper a mapping function to apply to the values, if renderable
92-
* @return a new condition with mapped values if renderable, otherwise a condition
93-
* that will not render.
94-
*/
75+
@Override
9576
public IsIn<T> map(UnaryOperator<T> mapper) {
96-
if (shouldRender()) {
97-
return new IsIn<>(applyMapper(mapper), emptyCallback);
98-
} else {
99-
return this;
100-
}
77+
return map(mapper, IsIn::new, this);
10178
}
10279

10380
@SafeVarargs

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,14 @@ public IsInCaseInsensitive withListEmptyCallback(Callback callback) {
4747
return new IsInCaseInsensitive(values, callback);
4848
}
4949

50-
/**
51-
* If renderable apply the predicate to each value in the list and return a new condition with the filtered values.
52-
* Else returns a condition that will not render (this). If all values are filtered out of the value
53-
* list, then the condition will not render.
54-
*
55-
* @param predicate predicate applied to the values, if renderable
56-
* @return a new condition with filtered values if renderable, otherwise a condition
57-
* that will not render.
58-
*/
50+
@Override
5951
public IsInCaseInsensitive filter(Predicate<String> predicate) {
60-
if (shouldRender()) {
61-
return new IsInCaseInsensitive(applyFilter(predicate), emptyCallback);
62-
} else {
63-
return this;
64-
}
52+
return filter(predicate, IsInCaseInsensitive::new, this);
6553
}
6654

67-
/**
68-
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
69-
* Else return a condition that will not render (this).
70-
*
71-
* @param mapper a mapping function to apply to the values, if renderable
72-
* @return a new condition with mapped values if renderable, otherwise a condition
73-
* that will not render.
74-
*/
55+
@Override
7556
public IsInCaseInsensitive map(UnaryOperator<String> mapper) {
76-
if (shouldRender()) {
77-
return new IsInCaseInsensitive(applyMapper(mapper), emptyCallback);
78-
} else {
79-
return this;
80-
}
57+
return map(mapper, IsInCaseInsensitive::new, this);
8158
}
8259

8360
public static IsInCaseInsensitive of(String... values) {

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,14 @@ public IsNotIn<T> then(UnaryOperator<Stream<T>> valueStreamTransformer) {
6969
return new IsNotIn<>(mapped, emptyCallback);
7070
}
7171

72-
/**
73-
* If renderable apply the predicate to each value in the list and return a new condition with the filtered values.
74-
* Else returns a condition that will not render (this). If all values are filtered out of the value
75-
* list, then the condition will not render.
76-
*
77-
* @param predicate predicate applied to the values, if renderable
78-
* @return a new condition with filtered values if renderable, otherwise a condition
79-
* that will not render.
80-
*/
72+
@Override
8173
public IsNotIn<T> filter(Predicate<T> predicate) {
82-
if (shouldRender()) {
83-
return new IsNotIn<>(applyFilter(predicate), emptyCallback);
84-
} else {
85-
return this;
86-
}
74+
return filter(predicate, IsNotIn::new, this);
8775
}
8876

89-
/**
90-
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
91-
* Else return a condition that will not render (this).
92-
*
93-
* @param mapper a mapping function to apply to the values, if renderable
94-
* @return a new condition with mapped values if renderable, otherwise a condition
95-
* that will not render.
96-
*/
77+
@Override
9778
public IsNotIn<T> map(UnaryOperator<T> mapper) {
98-
if (shouldRender()) {
99-
return new IsNotIn<>(applyMapper(mapper), emptyCallback);
100-
} else {
101-
return this;
102-
}
79+
return map(mapper, IsNotIn::new, this);
10380
}
10481

10582
@SafeVarargs

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,14 @@ public IsNotInCaseInsensitive withListEmptyCallback(Callback callback) {
4848
return new IsNotInCaseInsensitive(values, callback);
4949
}
5050

51-
/**
52-
* If renderable apply the predicate to each value in the list and return a new condition with the filtered values.
53-
* Else returns a condition that will not render (this). If all values are filtered out of the value
54-
* list, then the condition will not render.
55-
*
56-
* @param predicate predicate applied to the values, if renderable
57-
* @return a new condition with filtered values if renderable, otherwise a condition
58-
* that will not render.
59-
*/
51+
@Override
6052
public IsNotInCaseInsensitive filter(Predicate<String> predicate) {
61-
if (shouldRender()) {
62-
return new IsNotInCaseInsensitive(applyFilter(predicate), emptyCallback);
63-
} else {
64-
return this;
65-
}
53+
return filter(predicate, IsNotInCaseInsensitive::new, this);
6654
}
6755

68-
/**
69-
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
70-
* Else return a condition that will not render (this).
71-
*
72-
* @param mapper a mapping function to apply to the values, if renderable
73-
* @return a new condition with mapped values if renderable, otherwise a condition
74-
* that will not render.
75-
*/
56+
@Override
7657
public IsNotInCaseInsensitive map(UnaryOperator<String> mapper) {
77-
if (shouldRender()) {
78-
return new IsNotInCaseInsensitive(applyMapper(mapper), emptyCallback);
79-
} else {
80-
return this;
81-
}
58+
return map(mapper, IsNotInCaseInsensitive::new, this);
8259
}
8360

8461
public static IsNotInCaseInsensitive of(String... values) {

0 commit comments

Comments
 (0)