Skip to content

Commit edbcb02

Browse files
committed
Map functions can change data types - Single Value Conditions
1 parent d468f1f commit edbcb02

File tree

15 files changed

+214
-94
lines changed

15 files changed

+214
-94
lines changed

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

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
import java.util.function.Function;
1919
import java.util.function.Predicate;
2020
import java.util.function.Supplier;
21-
import java.util.function.UnaryOperator;
2221

23-
public abstract class AbstractSingleValueCondition<T, S extends AbstractSingleValueCondition<T, S>>
24-
implements VisitableCondition<T> {
22+
public abstract class AbstractSingleValueCondition<T> implements VisitableCondition<T> {
2523
protected final T value;
2624

2725
protected AbstractSingleValueCondition(T value) {
@@ -37,41 +35,32 @@ public <R> R accept(ConditionVisitor<T, R> visitor) {
3735
return visitor.visit(this);
3836
}
3937

40-
protected S filter(Predicate<T> predicate, Supplier<S> empty, S self) {
38+
protected <S> S filterSupport(Predicate<? super T> predicate, Supplier<S> empty, S self) {
4139
if (shouldRender()) {
4240
return predicate.test(value) ? self : empty.get();
4341
} else {
4442
return self;
4543
}
4644
}
4745

48-
/**
49-
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
50-
* that will not render.
51-
*
52-
* @param predicate predicate applied to the value, if renderable
53-
* @return this condition if renderable and the value matches the predicate, otherwise a condition
54-
* that will not render.
55-
*/
56-
public abstract S filter(Predicate<T> predicate);
57-
58-
protected S map(UnaryOperator<T> mapper, Function<T, S> constructor, S self) {
46+
protected <R, S> S mapSupport(Function<? super T, ? extends R> mapper, Function<R, S> constructor,
47+
Supplier<S> empty) {
5948
if (shouldRender()) {
6049
return constructor.apply(mapper.apply(value));
6150
} else {
62-
return self;
51+
return empty.get();
6352
}
6453
}
6554

6655
/**
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).
56+
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
57+
* that will not render.
6958
*
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.
59+
* @param predicate predicate applied to the value, if renderable
60+
* @return this condition if renderable and the value matches the predicate, otherwise a condition
61+
* that will not render.
7362
*/
74-
public abstract S map(UnaryOperator<T> mapper);
63+
public abstract AbstractSingleValueCondition<T> filter(Predicate<? super T> predicate);
7564

7665
public abstract String renderCondition(String columnName, String placeholder);
7766
}

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: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package org.mybatis.dynamic.sql.where.condition;
1717

18+
import java.util.function.Function;
1819
import java.util.function.Predicate;
1920
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

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

2526
private static final IsEqualTo<?> EMPTY = new IsEqualTo<Object>(null) {
2627
@Override
@@ -66,7 +67,7 @@ public IsEqualTo<T> when(Predicate<T> predicate) {
6667
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
6768
* condition that will not render (this).
6869
*
69-
* @deprecated replaced by {@link IsEqualTo#map(UnaryOperator)}
70+
* @deprecated replaced by {@link IsEqualTo#map(Function)}
7071
* @param mapper a mapping function to apply to the value, if renderable
7172
* @return a new condition with the result of applying the mapper to the value of this condition,
7273
* if renderable, otherwise a condition that will not render.
@@ -77,12 +78,20 @@ public IsEqualTo<T> then(UnaryOperator<T> mapper) {
7778
}
7879

7980
@Override
80-
public IsEqualTo<T> filter(Predicate<T> predicate) {
81-
return filter(predicate, IsEqualTo::empty, this);
81+
public IsEqualTo<T> filter(Predicate<? super T> predicate) {
82+
return filterSupport(predicate, IsEqualTo::empty, this);
8283
}
8384

84-
@Override
85-
public IsEqualTo<T> map(UnaryOperator<T> mapper) {
86-
return map(mapper, IsEqualTo::new, this);
85+
/**
86+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
87+
* condition that will not render (this).
88+
*
89+
* @param mapper a mapping function to apply to the value, if renderable
90+
* @param <R> type of the new condition
91+
* @return a new condition with the result of applying the mapper to the value of this condition,
92+
* if renderable, otherwise a condition that will not render.
93+
*/
94+
public <R> IsEqualTo<R> map(Function<? super T, ? extends R> mapper) {
95+
return mapSupport(mapper, IsEqualTo::new, IsEqualTo::empty);
8796
}
8897
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package org.mybatis.dynamic.sql.where.condition;
1717

18+
import java.util.function.Function;
1819
import java.util.function.Predicate;
1920
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

23-
public class IsGreaterThan<T> extends AbstractSingleValueCondition<T, IsGreaterThan<T>> {
24+
public class IsGreaterThan<T> extends AbstractSingleValueCondition<T> {
2425
private static final IsGreaterThan<?> EMPTY = new IsGreaterThan<Object>(null) {
2526
@Override
2627
public boolean shouldRender() {
@@ -65,7 +66,7 @@ public IsGreaterThan<T> when(Predicate<T> predicate) {
6566
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
6667
* condition that will not render (this).
6768
*
68-
* @deprecated replaced by {@link IsGreaterThan#map(UnaryOperator)}
69+
* @deprecated replaced by {@link IsGreaterThan#map(Function)}
6970
* @param mapper a mapping function to apply to the value, if renderable
7071
* @return a new condition with the result of applying the mapper to the value of this condition,
7172
* if renderable, otherwise a condition that will not render.
@@ -76,12 +77,20 @@ public IsGreaterThan<T> then(UnaryOperator<T> mapper) {
7677
}
7778

7879
@Override
79-
public IsGreaterThan<T> filter(Predicate<T> predicate) {
80-
return filter(predicate, IsGreaterThan::empty, this);
80+
public IsGreaterThan<T> filter(Predicate<? super T> predicate) {
81+
return filterSupport(predicate, IsGreaterThan::empty, this);
8182
}
8283

83-
@Override
84-
public IsGreaterThan<T> map(UnaryOperator<T> mapper) {
85-
return map(mapper, IsGreaterThan::new, this);
84+
/**
85+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
86+
* condition that will not render (this).
87+
*
88+
* @param mapper a mapping function to apply to the value, if renderable
89+
* @param <R> type of the new condition
90+
* @return a new condition with the result of applying the mapper to the value of this condition,
91+
* if renderable, otherwise a condition that will not render.
92+
*/
93+
public <R> IsGreaterThan<R> map(Function<? super T, ? extends R> mapper) {
94+
return mapSupport(mapper, IsGreaterThan::new, IsGreaterThan::empty);
8695
}
8796
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package org.mybatis.dynamic.sql.where.condition;
1717

18+
import java.util.function.Function;
1819
import java.util.function.Predicate;
1920
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

23-
public class IsGreaterThanOrEqualTo<T> extends AbstractSingleValueCondition<T, IsGreaterThanOrEqualTo<T>> {
24+
public class IsGreaterThanOrEqualTo<T> extends AbstractSingleValueCondition<T> {
2425
private static final IsGreaterThanOrEqualTo<?> EMPTY = new IsGreaterThanOrEqualTo<Object>(null) {
2526
@Override
2627
public boolean shouldRender() {
@@ -65,7 +66,7 @@ public IsGreaterThanOrEqualTo<T> when(Predicate<T> predicate) {
6566
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
6667
* condition that will not render (this).
6768
*
68-
* @deprecated replaced by {@link IsGreaterThanOrEqualTo#map(UnaryOperator)}
69+
* @deprecated replaced by {@link IsGreaterThanOrEqualTo#map(Function)}
6970
* @param mapper a mapping function to apply to the value, if renderable
7071
* @return a new condition with the result of applying the mapper to the value of this condition,
7172
* if renderable, otherwise a condition that will not render.
@@ -76,12 +77,20 @@ public IsGreaterThanOrEqualTo<T> then(UnaryOperator<T> mapper) {
7677
}
7778

7879
@Override
79-
public IsGreaterThanOrEqualTo<T> filter(Predicate<T> predicate) {
80-
return filter(predicate, IsGreaterThanOrEqualTo::empty, this);
80+
public IsGreaterThanOrEqualTo<T> filter(Predicate<? super T> predicate) {
81+
return filterSupport(predicate, IsGreaterThanOrEqualTo::empty, this);
8182
}
8283

83-
@Override
84-
public IsGreaterThanOrEqualTo<T> map(UnaryOperator<T> mapper) {
85-
return map(mapper, IsGreaterThanOrEqualTo::new, this);
84+
/**
85+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
86+
* condition that will not render (this).
87+
*
88+
* @param mapper a mapping function to apply to the value, if renderable
89+
* @param <R> type of the new condition
90+
* @return a new condition with the result of applying the mapper to the value of this condition,
91+
* if renderable, otherwise a condition that will not render.
92+
*/
93+
public <R> IsGreaterThanOrEqualTo<R> map(Function<? super T, ? extends R> mapper) {
94+
return mapSupport(mapper, IsGreaterThanOrEqualTo::new, IsGreaterThanOrEqualTo::empty);
8695
}
8796
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package org.mybatis.dynamic.sql.where.condition;
1717

18+
import java.util.function.Function;
1819
import java.util.function.Predicate;
1920
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

23-
public class IsLessThan<T> extends AbstractSingleValueCondition<T, IsLessThan<T>> {
24+
public class IsLessThan<T> extends AbstractSingleValueCondition<T> {
2425
private static final IsLessThan<?> EMPTY = new IsLessThan<Object>(null) {
2526
@Override
2627
public boolean shouldRender() {
@@ -65,7 +66,7 @@ public IsLessThan<T> when(Predicate<T> predicate) {
6566
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
6667
* condition that will not render (this).
6768
*
68-
* @deprecated replaced by {@link IsLessThan#map(UnaryOperator)}
69+
* @deprecated replaced by {@link IsLessThan#map(Function)}
6970
* @param mapper a mapping function to apply to the value, if renderable
7071
* @return a new condition with the result of applying the mapper to the value of this condition,
7172
* if renderable, otherwise a condition that will not render.
@@ -76,12 +77,20 @@ public IsLessThan<T> then(UnaryOperator<T> mapper) {
7677
}
7778

7879
@Override
79-
public IsLessThan<T> filter(Predicate<T> predicate) {
80-
return filter(predicate, IsLessThan::empty, this);
80+
public IsLessThan<T> filter(Predicate<? super T> predicate) {
81+
return filterSupport(predicate, IsLessThan::empty, this);
8182
}
8283

83-
@Override
84-
public IsLessThan<T> map(UnaryOperator<T> mapper) {
85-
return map(mapper, IsLessThan::new, this);
84+
/**
85+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
86+
* condition that will not render (this).
87+
*
88+
* @param mapper a mapping function to apply to the value, if renderable
89+
* @param <R> type of the new condition
90+
* @return a new condition with the result of applying the mapper to the value of this condition,
91+
* if renderable, otherwise a condition that will not render.
92+
*/
93+
public <R> IsLessThan<R> map(Function<? super T, ? extends R> mapper) {
94+
return mapSupport(mapper, IsLessThan::new, IsLessThan::empty);
8695
}
8796
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package org.mybatis.dynamic.sql.where.condition;
1717

18+
import java.util.function.Function;
1819
import java.util.function.Predicate;
1920
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

23-
public class IsLessThanOrEqualTo<T> extends AbstractSingleValueCondition<T, IsLessThanOrEqualTo<T>> {
24+
public class IsLessThanOrEqualTo<T> extends AbstractSingleValueCondition<T> {
2425
private static final IsLessThanOrEqualTo<?> EMPTY = new IsLessThanOrEqualTo<Object>(null) {
2526
@Override
2627
public boolean shouldRender() {
@@ -65,7 +66,7 @@ public IsLessThanOrEqualTo<T> when(Predicate<T> predicate) {
6566
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
6667
* condition that will not render (this).
6768
*
68-
* @deprecated replaced by {@link IsLessThanOrEqualTo#map(UnaryOperator)}
69+
* @deprecated replaced by {@link IsLessThanOrEqualTo#map(Function)}
6970
* @param mapper a mapping function to apply to the value, if renderable
7071
* @return a new condition with the result of applying the mapper to the value of this condition,
7172
* if renderable, otherwise a condition that will not render.
@@ -76,12 +77,20 @@ public IsLessThanOrEqualTo<T> then(UnaryOperator<T> mapper) {
7677
}
7778

7879
@Override
79-
public IsLessThanOrEqualTo<T> filter(Predicate<T> predicate) {
80-
return filter(predicate, IsLessThanOrEqualTo::empty, this);
80+
public IsLessThanOrEqualTo<T> filter(Predicate<? super T> predicate) {
81+
return filterSupport(predicate, IsLessThanOrEqualTo::empty, this);
8182
}
8283

83-
@Override
84-
public IsLessThanOrEqualTo<T> map(UnaryOperator<T> mapper) {
85-
return map(mapper, IsLessThanOrEqualTo::new, this);
84+
/**
85+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
86+
* condition that will not render (this).
87+
*
88+
* @param mapper a mapping function to apply to the value, if renderable
89+
* @param <R> type of the new condition
90+
* @return a new condition with the result of applying the mapper to the value of this condition,
91+
* if renderable, otherwise a condition that will not render.
92+
*/
93+
public <R> IsLessThanOrEqualTo<R> map(Function<? super T, ? extends R> mapper) {
94+
return mapSupport(mapper, IsLessThanOrEqualTo::new, IsLessThanOrEqualTo::empty);
8695
}
8796
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package org.mybatis.dynamic.sql.where.condition;
1717

18+
import java.util.function.Function;
1819
import java.util.function.Predicate;
1920
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

23-
public class IsLike<T> extends AbstractSingleValueCondition<T, IsLike<T>> {
24+
public class IsLike<T> extends AbstractSingleValueCondition<T> {
2425
private static final IsLike<?> EMPTY = new IsLike<Object>(null) {
2526
@Override
2627
public boolean shouldRender() {
@@ -65,7 +66,7 @@ public IsLike<T> when(Predicate<T> predicate) {
6566
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
6667
* condition that will not render (this).
6768
*
68-
* @deprecated replaced by {@link IsLike#map(UnaryOperator)}
69+
* @deprecated replaced by {@link IsLike#map(Function)}
6970
* @param mapper a mapping function to apply to the value, if renderable
7071
* @return a new condition with the result of applying the mapper to the value of this condition,
7172
* if renderable, otherwise a condition that will not render.
@@ -76,12 +77,20 @@ public IsLike<T> then(UnaryOperator<T> mapper) {
7677
}
7778

7879
@Override
79-
public IsLike<T> filter(Predicate<T> predicate) {
80-
return filter(predicate, IsLike::empty, this);
80+
public IsLike<T> filter(Predicate<? super T> predicate) {
81+
return filterSupport(predicate, IsLike::empty, this);
8182
}
8283

83-
@Override
84-
public IsLike<T> map(UnaryOperator<T> mapper) {
85-
return map(mapper, IsLike::new, this);
84+
/**
85+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
86+
* condition that will not render (this).
87+
*
88+
* @param mapper a mapping function to apply to the value, if renderable
89+
* @param <R> type of the new condition
90+
* @return a new condition with the result of applying the mapper to the value of this condition,
91+
* if renderable, otherwise a condition that will not render.
92+
*/
93+
public <R> IsLike<R> map(Function<? super T, ? extends R> mapper) {
94+
return mapSupport(mapper, IsLike::new, IsLike::empty);
8695
}
8796
}

0 commit comments

Comments
 (0)