Skip to content

Commit 12557c2

Browse files
committed
Implement map/filter for no single conditions
1 parent 91a9c8b commit 12557c2

25 files changed

+1102
-503
lines changed

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

Lines changed: 1 addition & 14 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.
@@ -16,32 +16,19 @@
1616
package org.mybatis.dynamic.sql;
1717

1818
import java.util.Objects;
19-
import java.util.function.Predicate;
2019
import java.util.function.Supplier;
2120

2221
public abstract class AbstractSingleValueCondition<T> implements VisitableCondition<T> {
2322
protected final Supplier<T> valueSupplier;
24-
private final Predicate<T> predicate;
2523

2624
protected AbstractSingleValueCondition(Supplier<T> valueSupplier) {
2725
this.valueSupplier = Objects.requireNonNull(valueSupplier);
28-
predicate = v -> true;
29-
}
30-
31-
protected AbstractSingleValueCondition(Supplier<T> valueSupplier, Predicate<T> predicate) {
32-
this.valueSupplier = Objects.requireNonNull(valueSupplier);
33-
this.predicate = Objects.requireNonNull(predicate);
3426
}
3527

3628
public T value() {
3729
return valueSupplier.get();
3830
}
3931

40-
@Override
41-
public boolean shouldRender() {
42-
return predicate.test(value());
43-
}
44-
4532
@Override
4633
public <R> R accept(ConditionVisitor<T, R> visitor) {
4734
return visitor.visit(this);

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

Lines changed: 31 additions & 41 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.
@@ -61,15 +61,12 @@
6161
import org.mybatis.dynamic.sql.where.condition.IsBetweenWhenPresent;
6262
import org.mybatis.dynamic.sql.where.condition.IsEqualTo;
6363
import org.mybatis.dynamic.sql.where.condition.IsEqualToColumn;
64-
import org.mybatis.dynamic.sql.where.condition.IsEqualToWhenPresent;
6564
import org.mybatis.dynamic.sql.where.condition.IsEqualToWithSubselect;
6665
import org.mybatis.dynamic.sql.where.condition.IsGreaterThan;
6766
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanColumn;
6867
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanOrEqualTo;
6968
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanOrEqualToColumn;
70-
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanOrEqualToWhenPresent;
7169
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanOrEqualToWithSubselect;
72-
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanWhenPresent;
7370
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanWithSubselect;
7471
import org.mybatis.dynamic.sql.where.condition.IsIn;
7572
import org.mybatis.dynamic.sql.where.condition.IsInCaseInsensitive;
@@ -80,19 +77,14 @@
8077
import org.mybatis.dynamic.sql.where.condition.IsLessThanColumn;
8178
import org.mybatis.dynamic.sql.where.condition.IsLessThanOrEqualTo;
8279
import org.mybatis.dynamic.sql.where.condition.IsLessThanOrEqualToColumn;
83-
import org.mybatis.dynamic.sql.where.condition.IsLessThanOrEqualToWhenPresent;
8480
import org.mybatis.dynamic.sql.where.condition.IsLessThanOrEqualToWithSubselect;
85-
import org.mybatis.dynamic.sql.where.condition.IsLessThanWhenPresent;
8681
import org.mybatis.dynamic.sql.where.condition.IsLessThanWithSubselect;
8782
import org.mybatis.dynamic.sql.where.condition.IsLike;
8883
import org.mybatis.dynamic.sql.where.condition.IsLikeCaseInsensitive;
89-
import org.mybatis.dynamic.sql.where.condition.IsLikeCaseInsensitiveWhenPresent;
90-
import org.mybatis.dynamic.sql.where.condition.IsLikeWhenPresent;
9184
import org.mybatis.dynamic.sql.where.condition.IsNotBetween;
9285
import org.mybatis.dynamic.sql.where.condition.IsNotBetweenWhenPresent;
9386
import org.mybatis.dynamic.sql.where.condition.IsNotEqualTo;
9487
import org.mybatis.dynamic.sql.where.condition.IsNotEqualToColumn;
95-
import org.mybatis.dynamic.sql.where.condition.IsNotEqualToWhenPresent;
9688
import org.mybatis.dynamic.sql.where.condition.IsNotEqualToWithSubselect;
9789
import org.mybatis.dynamic.sql.where.condition.IsNotIn;
9890
import org.mybatis.dynamic.sql.where.condition.IsNotInCaseInsensitive;
@@ -101,8 +93,6 @@
10193
import org.mybatis.dynamic.sql.where.condition.IsNotInWithSubselect;
10294
import org.mybatis.dynamic.sql.where.condition.IsNotLike;
10395
import org.mybatis.dynamic.sql.where.condition.IsNotLikeCaseInsensitive;
104-
import org.mybatis.dynamic.sql.where.condition.IsNotLikeCaseInsensitiveWhenPresent;
105-
import org.mybatis.dynamic.sql.where.condition.IsNotLikeWhenPresent;
10696
import org.mybatis.dynamic.sql.where.condition.IsNotNull;
10797
import org.mybatis.dynamic.sql.where.condition.IsNull;
10898

@@ -445,12 +435,12 @@ static <T> IsEqualToColumn<T> isEqualTo(BasicColumn column) {
445435
return IsEqualToColumn.of(column);
446436
}
447437

448-
static <T> IsEqualToWhenPresent<T> isEqualToWhenPresent(T value) {
438+
static <T> IsEqualTo<T> isEqualToWhenPresent(T value) {
449439
return isEqualToWhenPresent(() -> value);
450440
}
451441

452-
static <T> IsEqualToWhenPresent<T> isEqualToWhenPresent(Supplier<T> valueSupplier) {
453-
return IsEqualToWhenPresent.of(valueSupplier);
442+
static <T> IsEqualTo<T> isEqualToWhenPresent(Supplier<T> valueSupplier) {
443+
return IsEqualTo.of(valueSupplier).filter(Objects::nonNull);
454444
}
455445

456446
static <T> IsNotEqualTo<T> isNotEqualTo(T value) {
@@ -469,12 +459,12 @@ static <T> IsNotEqualToColumn<T> isNotEqualTo(BasicColumn column) {
469459
return IsNotEqualToColumn.of(column);
470460
}
471461

472-
static <T> IsNotEqualToWhenPresent<T> isNotEqualToWhenPresent(T value) {
462+
static <T> IsNotEqualTo<T> isNotEqualToWhenPresent(T value) {
473463
return isNotEqualToWhenPresent(() -> value);
474464
}
475465

476-
static <T> IsNotEqualToWhenPresent<T> isNotEqualToWhenPresent(Supplier<T> valueSupplier) {
477-
return IsNotEqualToWhenPresent.of(valueSupplier);
466+
static <T> IsNotEqualTo<T> isNotEqualToWhenPresent(Supplier<T> valueSupplier) {
467+
return IsNotEqualTo.of(valueSupplier).filter(Objects::nonNull);
478468
}
479469

480470
static <T> IsGreaterThan<T> isGreaterThan(T value) {
@@ -493,12 +483,12 @@ static <T> IsGreaterThanColumn<T> isGreaterThan(BasicColumn column) {
493483
return IsGreaterThanColumn.of(column);
494484
}
495485

496-
static <T> IsGreaterThanWhenPresent<T> isGreaterThanWhenPresent(T value) {
486+
static <T> IsGreaterThan<T> isGreaterThanWhenPresent(T value) {
497487
return isGreaterThanWhenPresent(() -> value);
498488
}
499489

500-
static <T> IsGreaterThanWhenPresent<T> isGreaterThanWhenPresent(Supplier<T> valueSupplier) {
501-
return IsGreaterThanWhenPresent.of(valueSupplier);
490+
static <T> IsGreaterThan<T> isGreaterThanWhenPresent(Supplier<T> valueSupplier) {
491+
return IsGreaterThan.of(valueSupplier).filter(Objects::nonNull);
502492
}
503493

504494
static <T> IsGreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T value) {
@@ -518,12 +508,12 @@ static <T> IsGreaterThanOrEqualToColumn<T> isGreaterThanOrEqualTo(BasicColumn co
518508
return IsGreaterThanOrEqualToColumn.of(column);
519509
}
520510

521-
static <T> IsGreaterThanOrEqualToWhenPresent<T> isGreaterThanOrEqualToWhenPresent(T value) {
511+
static <T> IsGreaterThanOrEqualTo<T> isGreaterThanOrEqualToWhenPresent(T value) {
522512
return isGreaterThanOrEqualToWhenPresent(() -> value);
523513
}
524514

525-
static <T> IsGreaterThanOrEqualToWhenPresent<T> isGreaterThanOrEqualToWhenPresent(Supplier<T> valueSupplier) {
526-
return IsGreaterThanOrEqualToWhenPresent.of(valueSupplier);
515+
static <T> IsGreaterThanOrEqualTo<T> isGreaterThanOrEqualToWhenPresent(Supplier<T> valueSupplier) {
516+
return IsGreaterThanOrEqualTo.of(valueSupplier).filter(Objects::nonNull);
527517
}
528518

529519
static <T> IsLessThan<T> isLessThan(T value) {
@@ -542,12 +532,12 @@ static <T> IsLessThanColumn<T> isLessThan(BasicColumn column) {
542532
return IsLessThanColumn.of(column);
543533
}
544534

545-
static <T> IsLessThanWhenPresent<T> isLessThanWhenPresent(T value) {
535+
static <T> IsLessThan<T> isLessThanWhenPresent(T value) {
546536
return isLessThanWhenPresent(() -> value);
547537
}
548538

549-
static <T> IsLessThanWhenPresent<T> isLessThanWhenPresent(Supplier<T> valueSupplier) {
550-
return IsLessThanWhenPresent.of(valueSupplier);
539+
static <T> IsLessThan<T> isLessThanWhenPresent(Supplier<T> valueSupplier) {
540+
return IsLessThan.of(valueSupplier).filter(Objects::nonNull);
551541
}
552542

553543
static <T> IsLessThanOrEqualTo<T> isLessThanOrEqualTo(T value) {
@@ -566,12 +556,12 @@ static <T> IsLessThanOrEqualToColumn<T> isLessThanOrEqualTo(BasicColumn column)
566556
return IsLessThanOrEqualToColumn.of(column);
567557
}
568558

569-
static <T> IsLessThanOrEqualToWhenPresent<T> isLessThanOrEqualToWhenPresent(T value) {
559+
static <T> IsLessThanOrEqualTo<T> isLessThanOrEqualToWhenPresent(T value) {
570560
return isLessThanOrEqualToWhenPresent(() -> value);
571561
}
572562

573-
static <T> IsLessThanOrEqualToWhenPresent<T> isLessThanOrEqualToWhenPresent(Supplier<T> valueSupplier) {
574-
return IsLessThanOrEqualToWhenPresent.of(valueSupplier);
563+
static <T> IsLessThanOrEqualTo<T> isLessThanOrEqualToWhenPresent(Supplier<T> valueSupplier) {
564+
return IsLessThanOrEqualTo.of(valueSupplier).filter(Objects::nonNull);
575565
}
576566

577567
@SafeVarargs
@@ -659,12 +649,12 @@ static <T> IsLike<T> isLike(Supplier<T> valueSupplier) {
659649
return IsLike.of(valueSupplier);
660650
}
661651

662-
static <T> IsLikeWhenPresent<T> isLikeWhenPresent(T value) {
652+
static <T> IsLike<T> isLikeWhenPresent(T value) {
663653
return isLikeWhenPresent(() -> value);
664654
}
665655

666-
static <T> IsLikeWhenPresent<T> isLikeWhenPresent(Supplier<T> valueSupplier) {
667-
return IsLikeWhenPresent.of(valueSupplier);
656+
static <T> IsLike<T> isLikeWhenPresent(Supplier<T> valueSupplier) {
657+
return IsLike.of(valueSupplier).filter(Objects::nonNull);
668658
}
669659

670660
static <T> IsNotLike<T> isNotLike(T value) {
@@ -675,12 +665,12 @@ static <T> IsNotLike<T> isNotLike(Supplier<T> valueSupplier) {
675665
return IsNotLike.of(valueSupplier);
676666
}
677667

678-
static <T> IsNotLikeWhenPresent<T> isNotLikeWhenPresent(T value) {
668+
static <T> IsNotLike<T> isNotLikeWhenPresent(T value) {
679669
return isNotLikeWhenPresent(() -> value);
680670
}
681671

682-
static <T> IsNotLikeWhenPresent<T> isNotLikeWhenPresent(Supplier<T> valueSupplier) {
683-
return IsNotLikeWhenPresent.of(valueSupplier);
672+
static <T> IsNotLike<T> isNotLikeWhenPresent(Supplier<T> valueSupplier) {
673+
return IsNotLike.of(valueSupplier).filter(Objects::nonNull);
684674
}
685675

686676
// shortcuts for booleans
@@ -701,12 +691,12 @@ static IsLikeCaseInsensitive isLikeCaseInsensitive(Supplier<String> valueSupplie
701691
return IsLikeCaseInsensitive.of(valueSupplier);
702692
}
703693

704-
static IsLikeCaseInsensitiveWhenPresent isLikeCaseInsensitiveWhenPresent(String value) {
694+
static IsLikeCaseInsensitive isLikeCaseInsensitiveWhenPresent(String value) {
705695
return isLikeCaseInsensitiveWhenPresent(() -> value);
706696
}
707697

708-
static IsLikeCaseInsensitiveWhenPresent isLikeCaseInsensitiveWhenPresent(Supplier<String> valueSupplier) {
709-
return IsLikeCaseInsensitiveWhenPresent.of(valueSupplier);
698+
static IsLikeCaseInsensitive isLikeCaseInsensitiveWhenPresent(Supplier<String> valueSupplier) {
699+
return IsLikeCaseInsensitive.of(valueSupplier).filter(Objects::nonNull);
710700
}
711701

712702
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitive(String value) {
@@ -717,12 +707,12 @@ static IsNotLikeCaseInsensitive isNotLikeCaseInsensitive(Supplier<String> valueS
717707
return IsNotLikeCaseInsensitive.of(valueSupplier);
718708
}
719709

720-
static IsNotLikeCaseInsensitiveWhenPresent isNotLikeCaseInsensitiveWhenPresent(String value) {
710+
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitiveWhenPresent(String value) {
721711
return isNotLikeCaseInsensitiveWhenPresent(() -> value);
722712
}
723713

724-
static IsNotLikeCaseInsensitiveWhenPresent isNotLikeCaseInsensitiveWhenPresent(Supplier<String> valueSupplier) {
725-
return IsNotLikeCaseInsensitiveWhenPresent.of(valueSupplier);
714+
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitiveWhenPresent(Supplier<String> valueSupplier) {
715+
return IsNotLikeCaseInsensitive.of(valueSupplier).filter(Objects::nonNull);
726716
}
727717

728718
static IsInCaseInsensitive isInCaseInsensitive(String...values) {

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

Lines changed: 71 additions & 8 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.
@@ -27,10 +27,6 @@ protected IsEqualTo(Supplier<T> valueSupplier) {
2727
super(valueSupplier);
2828
}
2929

30-
protected IsEqualTo(Supplier<T> valueSupplier, Predicate<T> predicate) {
31-
super(valueSupplier, predicate);
32-
}
33-
3430
@Override
3531
public String renderCondition(String columnName, String placeholder) {
3632
return columnName + " = " + placeholder; //$NON-NLS-1$
@@ -40,11 +36,78 @@ public static <T> IsEqualTo<T> of(Supplier<T> valueSupplier) {
4036
return new IsEqualTo<>(valueSupplier);
4137
}
4238

39+
/**
40+
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
41+
* that will not render.
42+
*
43+
* @deprecated replaced by {@link IsEqualTo#filter(Predicate)}
44+
* @param predicate predicate applied to the value, if renderable
45+
* @return this condition if renderable and the value matches the predicate, otherwise a condition
46+
* that will not render.
47+
*/
48+
@Deprecated
4349
public IsEqualTo<T> when(Predicate<T> predicate) {
44-
return new IsEqualTo<>(valueSupplier, predicate);
50+
return filter(predicate);
51+
}
52+
53+
/**
54+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
55+
* condition that will not render (this).
56+
*
57+
* @deprecated replaced by {@link IsEqualTo#map(UnaryOperator)}
58+
* @param mapper a mapping function to apply to the value, if renderable
59+
* @return a new condition with the result of applying the mapper to the value of this condition,
60+
* if renderable, otherwise a condition that will not render.
61+
*/
62+
@Deprecated
63+
public IsEqualTo<T> then(UnaryOperator<T> mapper) {
64+
return map(mapper);
65+
}
66+
67+
/**
68+
* If renderable and the value matches the predicate, returns this condition. Else returns a condition
69+
* that will not render.
70+
*
71+
* @param predicate predicate applied to the value, if renderable
72+
* @return this condition if renderable and the value matches the predicate, otherwise a condition
73+
* that will not render.
74+
*/
75+
public IsEqualTo<T> filter(Predicate<T> predicate) {
76+
if (shouldRender()) {
77+
return predicate.test(value()) ? this : EmptyIsEqualTo.empty();
78+
} else {
79+
return this;
80+
}
4581
}
4682

47-
public IsEqualTo<T> then(UnaryOperator<T> transformer) {
48-
return shouldRender() ? new IsEqualTo<>(() -> transformer.apply(value())) : this;
83+
/**
84+
* If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
85+
* condition that will not render (this).
86+
*
87+
* @param mapper a mapping function to apply to the value, if renderable
88+
* @return a new condition with the result of applying the mapper to the value of this condition,
89+
* if renderable, otherwise a condition that will not render.
90+
*/
91+
public IsEqualTo<T> map(UnaryOperator<T> mapper) {
92+
return shouldRender() ? new IsEqualTo<>(() -> mapper.apply(value())) : this;
93+
}
94+
95+
public static class EmptyIsEqualTo<T> extends IsEqualTo<T> {
96+
private static final EmptyIsEqualTo<?> EMPTY = new EmptyIsEqualTo<>();
97+
98+
public static <T> EmptyIsEqualTo<T> empty() {
99+
@SuppressWarnings("unchecked")
100+
EmptyIsEqualTo<T> t = (EmptyIsEqualTo<T>) EMPTY;
101+
return t;
102+
}
103+
104+
public EmptyIsEqualTo() {
105+
super(() -> null);
106+
}
107+
108+
@Override
109+
public boolean shouldRender() {
110+
return false;
111+
}
49112
}
50113
}

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)