Skip to content

Commit 1da7647

Browse files
committed
Define a map template method for single value
1 parent ec526aa commit 1da7647

21 files changed

+86
-143
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ protected <R, S extends AbstractSingleValueCondition<R>> S mapSupport(Function<?
6464
*/
6565
public abstract AbstractSingleValueCondition<T> filter(Predicate<? super T> predicate);
6666

67+
/**
68+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
69+
* condition that will not render (this).
70+
*
71+
* @param mapper a mapping function to apply to the value, if renderable
72+
* @param <R> type of the new condition
73+
* @return a new condition with the result of applying the mapper to the value of this condition,
74+
* if renderable, otherwise a condition that will not render.
75+
*/
76+
public abstract <R> AbstractSingleValueCondition<R> map(Function<? super T, ? extends R> mapper);
77+
6778
public abstract String operator();
6879

6980
@Override

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -882,35 +882,36 @@ static IsEqualTo<Boolean> isFalse() {
882882
}
883883

884884
// conditions for strings only
885-
static IsLikeCaseInsensitive isLikeCaseInsensitive(String value) {
885+
static IsLikeCaseInsensitive<String> isLikeCaseInsensitive(String value) {
886886
return IsLikeCaseInsensitive.of(value);
887887
}
888888

889-
static IsLikeCaseInsensitive isLikeCaseInsensitive(Supplier<String> valueSupplier) {
889+
static IsLikeCaseInsensitive<String> isLikeCaseInsensitive(Supplier<String> valueSupplier) {
890890
return isLikeCaseInsensitive(valueSupplier.get());
891891
}
892892

893-
static IsLikeCaseInsensitive isLikeCaseInsensitiveWhenPresent(@Nullable String value) {
893+
static IsLikeCaseInsensitive<String> isLikeCaseInsensitiveWhenPresent(@Nullable String value) {
894894
return value == null ? IsLikeCaseInsensitive.empty() : IsLikeCaseInsensitive.of(value);
895895
}
896896

897-
static IsLikeCaseInsensitive isLikeCaseInsensitiveWhenPresent(Supplier<@Nullable String> valueSupplier) {
897+
static IsLikeCaseInsensitive<String> isLikeCaseInsensitiveWhenPresent(Supplier<@Nullable String> valueSupplier) {
898898
return isLikeCaseInsensitiveWhenPresent(valueSupplier.get());
899899
}
900900

901-
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitive(String value) {
901+
static IsNotLikeCaseInsensitive<String> isNotLikeCaseInsensitive(String value) {
902902
return IsNotLikeCaseInsensitive.of(value);
903903
}
904904

905-
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitive(Supplier<String> valueSupplier) {
905+
static IsNotLikeCaseInsensitive<String> isNotLikeCaseInsensitive(Supplier<String> valueSupplier) {
906906
return isNotLikeCaseInsensitive(valueSupplier.get());
907907
}
908908

909-
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitiveWhenPresent(@Nullable String value) {
909+
static IsNotLikeCaseInsensitive<String> isNotLikeCaseInsensitiveWhenPresent(@Nullable String value) {
910910
return value == null ? IsNotLikeCaseInsensitive.empty() : IsNotLikeCaseInsensitive.of(value);
911911
}
912912

913-
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitiveWhenPresent(Supplier<@Nullable String> valueSupplier) {
913+
static IsNotLikeCaseInsensitive<String> isNotLikeCaseInsensitiveWhenPresent(
914+
Supplier<@Nullable String> valueSupplier) {
914915
return isNotLikeCaseInsensitiveWhenPresent(valueSupplier.get());
915916
}
916917

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
import org.mybatis.dynamic.sql.render.RenderingContext;
2121
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
2222

23-
public interface CaseInsensitiveRenderableCondition extends RenderableCondition<String> {
23+
public interface CaseInsensitiveRenderableCondition<T> extends RenderableCondition<T> {
2424

2525
@Override
2626
default FragmentAndParameters renderLeftColumn(RenderingContext renderingContext,
27-
BindableColumn<String> leftColumn) {
27+
BindableColumn<T> leftColumn) {
2828
return RenderableCondition.super.renderLeftColumn(renderingContext, leftColumn)
2929
.mapFragment(s -> "upper(" + s + ")"); //$NON-NLS-1$ //$NON-NLS-2$
3030
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,7 @@ public IsEqualTo<T> filter(Predicate<? super T> predicate) {
5959
return filterSupport(predicate, IsEqualTo::empty, this);
6060
}
6161

62-
/**
63-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
64-
* condition that will not render (this).
65-
*
66-
* @param mapper a mapping function to apply to the value, if renderable
67-
* @param <R> type of the new condition
68-
* @return a new condition with the result of applying the mapper to the value of this condition,
69-
* if renderable, otherwise a condition that will not render.
70-
*/
62+
@Override
7163
public <R> IsEqualTo<R> map(Function<? super T, ? extends R> mapper) {
7264
return mapSupport(mapper, IsEqualTo::new, IsEqualTo::empty);
7365
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,7 @@ public IsGreaterThan<T> filter(Predicate<? super T> predicate) {
5858
return filterSupport(predicate, IsGreaterThan::empty, this);
5959
}
6060

61-
/**
62-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
63-
* condition that will not render (this).
64-
*
65-
* @param mapper a mapping function to apply to the value, if renderable
66-
* @param <R> type of the new condition
67-
* @return a new condition with the result of applying the mapper to the value of this condition,
68-
* if renderable, otherwise a condition that will not render.
69-
*/
61+
@Override
7062
public <R> IsGreaterThan<R> map(Function<? super T, ? extends R> mapper) {
7163
return mapSupport(mapper, IsGreaterThan::new, IsGreaterThan::empty);
7264
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,7 @@ public IsGreaterThanOrEqualTo<T> filter(Predicate<? super T> predicate) {
5858
return filterSupport(predicate, IsGreaterThanOrEqualTo::empty, this);
5959
}
6060

61-
/**
62-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
63-
* condition that will not render (this).
64-
*
65-
* @param mapper a mapping function to apply to the value, if renderable
66-
* @param <R> type of the new condition
67-
* @return a new condition with the result of applying the mapper to the value of this condition,
68-
* if renderable, otherwise a condition that will not render.
69-
*/
61+
@Override
7062
public <R> IsGreaterThanOrEqualTo<R> map(Function<? super T, ? extends R> mapper) {
7163
return mapSupport(mapper, IsGreaterThanOrEqualTo::new, IsGreaterThanOrEqualTo::empty);
7264
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.mybatis.dynamic.sql.util.Validator;
2828

2929
public class IsInCaseInsensitive extends AbstractListValueCondition<String>
30-
implements CaseInsensitiveRenderableCondition {
30+
implements CaseInsensitiveRenderableCondition<String> {
3131
private static final IsInCaseInsensitive EMPTY = new IsInCaseInsensitive(Collections.emptyList());
3232

3333
public static IsInCaseInsensitive empty() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.mybatis.dynamic.sql.util.Utilities;
2828

2929
public class IsInCaseInsensitiveWhenPresent extends AbstractListValueCondition<String>
30-
implements CaseInsensitiveRenderableCondition {
30+
implements CaseInsensitiveRenderableCondition<String> {
3131
private static final IsInCaseInsensitiveWhenPresent EMPTY =
3232
new IsInCaseInsensitiveWhenPresent(Collections.emptyList());
3333

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,7 @@ public IsLessThan<T> filter(Predicate<? super T> predicate) {
5858
return filterSupport(predicate, IsLessThan::empty, this);
5959
}
6060

61-
/**
62-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
63-
* condition that will not render (this).
64-
*
65-
* @param mapper a mapping function to apply to the value, if renderable
66-
* @param <R> type of the new condition
67-
* @return a new condition with the result of applying the mapper to the value of this condition,
68-
* if renderable, otherwise a condition that will not render.
69-
*/
61+
@Override
7062
public <R> IsLessThan<R> map(Function<? super T, ? extends R> mapper) {
7163
return mapSupport(mapper, IsLessThan::new, IsLessThan::empty);
7264
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,7 @@ public IsLessThanOrEqualTo<T> filter(Predicate<? super T> predicate) {
5858
return filterSupport(predicate, IsLessThanOrEqualTo::empty, this);
5959
}
6060

61-
/**
62-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
63-
* condition that will not render (this).
64-
*
65-
* @param mapper a mapping function to apply to the value, if renderable
66-
* @param <R> type of the new condition
67-
* @return a new condition with the result of applying the mapper to the value of this condition,
68-
* if renderable, otherwise a condition that will not render.
69-
*/
61+
@Override
7062
public <R> IsLessThanOrEqualTo<R> map(Function<? super T, ? extends R> mapper) {
7163
return mapSupport(mapper, IsLessThanOrEqualTo::new, IsLessThanOrEqualTo::empty);
7264
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,7 @@ public IsLike<T> filter(Predicate<? super T> predicate) {
5858
return filterSupport(predicate, IsLike::empty, this);
5959
}
6060

61-
/**
62-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
63-
* condition that will not render (this).
64-
*
65-
* @param mapper a mapping function to apply to the value, if renderable
66-
* @param <R> type of the new condition
67-
* @return a new condition with the result of applying the mapper to the value of this condition,
68-
* if renderable, otherwise a condition that will not render.
69-
*/
61+
@Override
7062
public <R> IsLike<R> map(Function<? super T, ? extends R> mapper) {
7163
return mapSupport(mapper, IsLike::new, IsLike::empty);
7264
}

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
package org.mybatis.dynamic.sql.where.condition;
1717

1818
import java.util.NoSuchElementException;
19+
import java.util.function.Function;
1920
import java.util.function.Predicate;
20-
import java.util.function.UnaryOperator;
2121

2222
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2323
import org.mybatis.dynamic.sql.util.StringUtilities;
2424

25-
public class IsLikeCaseInsensitive extends AbstractSingleValueCondition<String>
26-
implements CaseInsensitiveRenderableCondition {
27-
private static final IsLikeCaseInsensitive EMPTY = new IsLikeCaseInsensitive("") { //$NON-NLS-1$
25+
public class IsLikeCaseInsensitive<T> extends AbstractSingleValueCondition<T>
26+
implements CaseInsensitiveRenderableCondition<T> {
27+
private static final IsLikeCaseInsensitive<?> EMPTY = new IsLikeCaseInsensitive<>("") { //$NON-NLS-1$
2828
@Override
2929
public String value() {
3030
throw new NoSuchElementException("No value present"); //$NON-NLS-1$
@@ -36,11 +36,13 @@ public boolean isEmpty() {
3636
}
3737
};
3838

39-
public static IsLikeCaseInsensitive empty() {
40-
return EMPTY;
39+
public static <T> IsLikeCaseInsensitive<T> empty() {
40+
@SuppressWarnings("unchecked")
41+
IsLikeCaseInsensitive<T> t = (IsLikeCaseInsensitive<T>) EMPTY;
42+
return t;
4143
}
4244

43-
protected IsLikeCaseInsensitive(String value) {
45+
protected IsLikeCaseInsensitive(T value) {
4446
super(value);
4547
}
4648

@@ -50,25 +52,18 @@ public String operator() {
5052
}
5153

5254
@Override
53-
public IsLikeCaseInsensitive filter(Predicate<? super String> predicate) {
55+
public IsLikeCaseInsensitive<T> filter(Predicate<? super T> predicate) {
5456
return filterSupport(predicate, IsLikeCaseInsensitive::empty, this);
5557
}
5658

57-
/**
58-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
59-
* condition that will not render (this).
60-
*
61-
* @param mapper a mapping function to apply to the value, if renderable
62-
* @return a new condition with the result of applying the mapper to the value of this condition,
63-
* if renderable, otherwise a condition that will not render.
64-
*/
65-
public IsLikeCaseInsensitive map(UnaryOperator<String> mapper) {
59+
@Override
60+
public <R> IsLikeCaseInsensitive<R> map(Function<? super T, ? extends R> mapper) {
6661
return mapSupport(mapper, IsLikeCaseInsensitive::new, IsLikeCaseInsensitive::empty);
6762
}
6863

69-
public static IsLikeCaseInsensitive of(String value) {
64+
public static IsLikeCaseInsensitive<String> of(String value) {
7065
// Keep the null safe upper case utility for backwards compatibility
7166
//noinspection DataFlowIssue
72-
return new IsLikeCaseInsensitive(value).map(StringUtilities::safelyUpperCase);
67+
return new IsLikeCaseInsensitive<>(value).map(StringUtilities::safelyUpperCase);
7368
}
7469
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,7 @@ public IsNotEqualTo<T> filter(Predicate<? super T> predicate) {
5858
return filterSupport(predicate, IsNotEqualTo::empty, this);
5959
}
6060

61-
/**
62-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
63-
* condition that will not render (this).
64-
*
65-
* @param mapper a mapping function to apply to the value, if renderable
66-
* @param <R> type of the new condition
67-
* @return a new condition with the result of applying the mapper to the value of this condition,
68-
* if renderable, otherwise a condition that will not render.
69-
*/
61+
@Override
7062
public <R> IsNotEqualTo<R> map(Function<? super T, ? extends R> mapper) {
7163
return mapSupport(mapper, IsNotEqualTo::new, IsNotEqualTo::empty);
7264
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.mybatis.dynamic.sql.util.Validator;
2828

2929
public class IsNotInCaseInsensitive extends AbstractListValueCondition<String>
30-
implements CaseInsensitiveRenderableCondition {
30+
implements CaseInsensitiveRenderableCondition<String> {
3131
private static final IsNotInCaseInsensitive EMPTY = new IsNotInCaseInsensitive(Collections.emptyList());
3232

3333
public static IsNotInCaseInsensitive empty() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.mybatis.dynamic.sql.util.Utilities;
2828

2929
public class IsNotInCaseInsensitiveWhenPresent extends AbstractListValueCondition<String>
30-
implements CaseInsensitiveRenderableCondition {
30+
implements CaseInsensitiveRenderableCondition<String> {
3131
private static final IsNotInCaseInsensitiveWhenPresent EMPTY =
3232
new IsNotInCaseInsensitiveWhenPresent(Collections.emptyList());
3333

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,7 @@ public IsNotLike<T> filter(Predicate<? super T> predicate) {
5858
return filterSupport(predicate, IsNotLike::empty, this);
5959
}
6060

61-
/**
62-
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
63-
* condition that will not render (this).
64-
*
65-
* @param mapper
66-
* a mapping function to apply to the value, if renderable
67-
* @param <R>
68-
* type of the new condition
69-
*
70-
* @return a new condition with the result of applying the mapper to the value of this condition, if renderable,
71-
* otherwise a condition that will not render.
72-
*/
61+
@Override
7362
public <R> IsNotLike<R> map(Function<? super T, ? extends R> mapper) {
7463
return mapSupport(mapper, IsNotLike::new, IsNotLike::empty);
7564
}

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

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

1818
import java.util.NoSuchElementException;
19+
import java.util.function.Function;
1920
import java.util.function.Predicate;
20-
import java.util.function.UnaryOperator;
2121

2222
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2323
import org.mybatis.dynamic.sql.util.StringUtilities;
2424

25-
public class IsNotLikeCaseInsensitive extends AbstractSingleValueCondition<String>
26-
implements CaseInsensitiveRenderableCondition {
27-
private static final IsNotLikeCaseInsensitive EMPTY = new IsNotLikeCaseInsensitive("") { //$NON-NLS-1$
25+
public class IsNotLikeCaseInsensitive<T> extends AbstractSingleValueCondition<T>
26+
implements CaseInsensitiveRenderableCondition<T> {
27+
private static final IsNotLikeCaseInsensitive<?> EMPTY = new IsNotLikeCaseInsensitive<>("") { //$NON-NLS-1$
2828
@Override
2929
public String value() {
3030
throw new NoSuchElementException("No value present"); //$NON-NLS-1$
@@ -36,11 +36,13 @@ public boolean isEmpty() {
3636
}
3737
};
3838

39-
public static IsNotLikeCaseInsensitive empty() {
40-
return EMPTY;
39+
public static <T> IsNotLikeCaseInsensitive<T> empty() {
40+
@SuppressWarnings("unchecked")
41+
IsNotLikeCaseInsensitive<T> t = (IsNotLikeCaseInsensitive<T>) EMPTY;
42+
return t;
4143
}
4244

43-
protected IsNotLikeCaseInsensitive(String value) {
45+
protected IsNotLikeCaseInsensitive(T value) {
4446
super(value);
4547
}
4648

@@ -50,7 +52,7 @@ public String operator() {
5052
}
5153

5254
@Override
53-
public IsNotLikeCaseInsensitive filter(Predicate<? super String> predicate) {
55+
public IsNotLikeCaseInsensitive<T> filter(Predicate<? super T> predicate) {
5456
return filterSupport(predicate, IsNotLikeCaseInsensitive::empty, this);
5557
}
5658

@@ -64,13 +66,13 @@ public IsNotLikeCaseInsensitive filter(Predicate<? super String> predicate) {
6466
* @return a new condition with the result of applying the mapper to the value of this condition, if renderable,
6567
* otherwise a condition that will not render.
6668
*/
67-
public IsNotLikeCaseInsensitive map(UnaryOperator<String> mapper) {
69+
public <R> IsNotLikeCaseInsensitive<R> map(Function<? super T, ? extends R> mapper) {
6870
return mapSupport(mapper, IsNotLikeCaseInsensitive::new, IsNotLikeCaseInsensitive::empty);
6971
}
7072

71-
public static IsNotLikeCaseInsensitive of(String value) {
73+
public static IsNotLikeCaseInsensitive<String> of(String value) {
7274
// Keep the null safe upper case utility for backwards compatibility
7375
//noinspection DataFlowIssue
74-
return new IsNotLikeCaseInsensitive(value).map(StringUtilities::safelyUpperCase);
76+
return new IsNotLikeCaseInsensitive<>(value).map(StringUtilities::safelyUpperCase);
7577
}
7678
}

0 commit comments

Comments
 (0)