Skip to content

Commit dafaafc

Browse files
committed
Remove Suppliers from the single and two value conditions
This made the conditions inconsistent with the list conditions, and also inconsistent with the function of the map methods.
1 parent e0e76bf commit dafaafc

23 files changed

+560
-213
lines changed

CHANGELOG.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ The major themes of this release include the following:
2222
All built-in conditions have been rafactored. The changes should have no impact for the vast majority of users.
2323
However, there are some changes in behavior and one breaking change.
2424

25+
1. Internally, the conditions no longer hold value Suppliers, they now hold the values themselves. The SqlBuilder
26+
methods that accept Suppliers will call the `Supplier.get()` method when the condition is constructed. This should
27+
have no impact unless you were somehow relying on the delay in obtaining a value until the condition was rendered.
2528
1. The existing "then" and "when" methods have been deprecated and replaced with "map" and "filter" respectively.
2629
The new method names are more familiar and more representative of what these methods actually do. In effect,
2730
these methods mimic the function of the "map" and "filter" methods on "java.util.Optional" and they are used
@@ -33,8 +36,8 @@ However, there are some changes in behavior and one breaking change.
3336
in the SqlBuilder remain, and they will now produce a condition with a "NotNull" filter applied. So at the API level,
3437
things will function exactly as before, but the intermediate classes will be different.
3538
1. One breaking change is that the builder for List value conditions has been removed without replacement. If you
36-
were using this builder, then the replacement is to build a new List value condition and then call the "map" and
37-
"filter" methods as needed. For example, prior code looked like this
39+
were using this builder to supply a "value stream transformer", then the replacement is to build a new List value
40+
condition and then call the "map" and "filter" methods as needed. For example, prior code looked like this
3841

3942
```java
4043
public static IsIn<String> isIn(String...values) {
@@ -46,9 +49,7 @@ However, there are some changes in behavior and one breaking change.
4649
.build();
4750
}
4851
```
49-
50-
New code should look like this:
51-
52+
New code should look like this:
5253
```java
5354
public static IsIn<String> isIn(String...values) {
5455
return SqlBuilder.isIn(values)
@@ -57,7 +58,6 @@ However, there are some changes in behavior and one breaking change.
5758
.filter(st -> !st.isEmpty());
5859
}
5960
```
60-
6161
We think this is a marked improvement!
6262

6363
### Breaking Change for Kotlin

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ For example, a typical search can be coded with a query like this (the following
2323
select(Customer.id, Customer.firstName, Customer.lastName) {
2424
from(Customer)
2525
where(Customer.active, isEqualTo(true))
26-
and(Customer.id, isEqualToWhenPresent(id).then{ it?.padStart(5, '0') })
26+
and(Customer.id, isEqualToWhenPresent(id).map{ it?.padStart(5, '0') })
2727
and(Customer.firstName, isLikeCaseInsensitiveWhenPresent(firstName)
28-
.then{ "%" + it.trim() + "%" })
28+
.map{ "%" + it.trim() + "%" })
2929
and(Customer.lastName, isLikeCaseInsensitiveWhenPresent(lastName)
30-
.then{ "%" + it.trim() + "%" })
30+
.map{ "%" + it.trim() + "%" })
3131
orderBy(Customer.lastName, Customer.firstName)
3232
limit(500)
3333
}

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

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

18-
import java.util.Objects;
19-
import java.util.function.Supplier;
20-
2118
public abstract class AbstractSingleValueCondition<T> implements VisitableCondition<T> {
22-
protected final Supplier<T> valueSupplier;
19+
protected final T value;
2320

24-
protected AbstractSingleValueCondition(Supplier<T> valueSupplier) {
25-
this.valueSupplier = Objects.requireNonNull(valueSupplier);
21+
protected AbstractSingleValueCondition(T value) {
22+
this.value = value;
2623
}
2724

2825
public T value() {
29-
return valueSupplier.get();
26+
return value;
3027
}
3128

3229
@Override

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,21 @@
1515
*/
1616
package org.mybatis.dynamic.sql;
1717

18-
import java.util.Objects;
19-
import java.util.function.Supplier;
20-
2118
public abstract class AbstractTwoValueCondition<T> implements VisitableCondition<T> {
22-
protected final Supplier<T> valueSupplier1;
23-
protected final Supplier<T> valueSupplier2;
19+
protected final T value1;
20+
protected final T value2;
2421

25-
protected AbstractTwoValueCondition(Supplier<T> valueSupplier1, Supplier<T> valueSupplier2) {
26-
this.valueSupplier1 = Objects.requireNonNull(valueSupplier1);
27-
this.valueSupplier2 = Objects.requireNonNull(valueSupplier2);
22+
protected AbstractTwoValueCondition(T value1, T value2) {
23+
this.value1 = value1;
24+
this.value2 = value2;
2825
}
2926

3027
public T value1() {
31-
return valueSupplier1.get();
28+
return value1;
3229
}
3330

3431
public T value2() {
35-
return valueSupplier2.get();
32+
return value2;
3633
}
3734

3835
@Override

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

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,11 @@ static <T> IsNotNull<T> isNotNull() {
414414
}
415415

416416
static <T> IsEqualTo<T> isEqualTo(T value) {
417-
return isEqualTo(() -> value);
417+
return IsEqualTo.of(value);
418418
}
419419

420420
static <T> IsEqualTo<T> isEqualTo(Supplier<T> valueSupplier) {
421-
return IsEqualTo.of(valueSupplier);
421+
return isEqualTo(valueSupplier.get());
422422
}
423423

424424
static <T> IsEqualToWithSubselect<T> isEqualTo(Buildable<SelectModel> selectModelBuilder) {
@@ -430,19 +430,19 @@ static <T> IsEqualToColumn<T> isEqualTo(BasicColumn column) {
430430
}
431431

432432
static <T> IsEqualTo<T> isEqualToWhenPresent(T value) {
433-
return isEqualToWhenPresent(() -> value);
433+
return IsEqualTo.of(value).filter(Objects::nonNull);
434434
}
435435

436436
static <T> IsEqualTo<T> isEqualToWhenPresent(Supplier<T> valueSupplier) {
437-
return IsEqualTo.of(valueSupplier).filter(Objects::nonNull);
437+
return isEqualToWhenPresent(valueSupplier.get());
438438
}
439439

440440
static <T> IsNotEqualTo<T> isNotEqualTo(T value) {
441-
return isNotEqualTo(() -> value);
441+
return IsNotEqualTo.of(value);
442442
}
443443

444444
static <T> IsNotEqualTo<T> isNotEqualTo(Supplier<T> valueSupplier) {
445-
return IsNotEqualTo.of(valueSupplier);
445+
return isNotEqualTo(valueSupplier.get());
446446
}
447447

448448
static <T> IsNotEqualToWithSubselect<T> isNotEqualTo(Buildable<SelectModel> selectModelBuilder) {
@@ -454,19 +454,19 @@ static <T> IsNotEqualToColumn<T> isNotEqualTo(BasicColumn column) {
454454
}
455455

456456
static <T> IsNotEqualTo<T> isNotEqualToWhenPresent(T value) {
457-
return isNotEqualToWhenPresent(() -> value);
457+
return IsNotEqualTo.of(value).filter(Objects::nonNull);
458458
}
459459

460460
static <T> IsNotEqualTo<T> isNotEqualToWhenPresent(Supplier<T> valueSupplier) {
461-
return IsNotEqualTo.of(valueSupplier).filter(Objects::nonNull);
461+
return isNotEqualToWhenPresent(valueSupplier.get());
462462
}
463463

464464
static <T> IsGreaterThan<T> isGreaterThan(T value) {
465-
return isGreaterThan(() -> value);
465+
return IsGreaterThan.of(value);
466466
}
467467

468468
static <T> IsGreaterThan<T> isGreaterThan(Supplier<T> valueSupplier) {
469-
return IsGreaterThan.of(valueSupplier);
469+
return isGreaterThan(valueSupplier.get());
470470
}
471471

472472
static <T> IsGreaterThanWithSubselect<T> isGreaterThan(Buildable<SelectModel> selectModelBuilder) {
@@ -478,19 +478,19 @@ static <T> IsGreaterThanColumn<T> isGreaterThan(BasicColumn column) {
478478
}
479479

480480
static <T> IsGreaterThan<T> isGreaterThanWhenPresent(T value) {
481-
return isGreaterThanWhenPresent(() -> value);
481+
return IsGreaterThan.of(value).filter(Objects::nonNull);
482482
}
483483

484484
static <T> IsGreaterThan<T> isGreaterThanWhenPresent(Supplier<T> valueSupplier) {
485-
return IsGreaterThan.of(valueSupplier).filter(Objects::nonNull);
485+
return isGreaterThanWhenPresent(valueSupplier.get());
486486
}
487487

488488
static <T> IsGreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T value) {
489-
return isGreaterThanOrEqualTo(() -> value);
489+
return IsGreaterThanOrEqualTo.of(value);
490490
}
491491

492492
static <T> IsGreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(Supplier<T> valueSupplier) {
493-
return IsGreaterThanOrEqualTo.of(valueSupplier);
493+
return isGreaterThanOrEqualTo(valueSupplier.get());
494494
}
495495

496496
static <T> IsGreaterThanOrEqualToWithSubselect<T> isGreaterThanOrEqualTo(
@@ -503,19 +503,19 @@ static <T> IsGreaterThanOrEqualToColumn<T> isGreaterThanOrEqualTo(BasicColumn co
503503
}
504504

505505
static <T> IsGreaterThanOrEqualTo<T> isGreaterThanOrEqualToWhenPresent(T value) {
506-
return isGreaterThanOrEqualToWhenPresent(() -> value);
506+
return IsGreaterThanOrEqualTo.of(value).filter(Objects::nonNull);
507507
}
508508

509509
static <T> IsGreaterThanOrEqualTo<T> isGreaterThanOrEqualToWhenPresent(Supplier<T> valueSupplier) {
510-
return IsGreaterThanOrEqualTo.of(valueSupplier).filter(Objects::nonNull);
510+
return isGreaterThanOrEqualToWhenPresent(valueSupplier.get());
511511
}
512512

513513
static <T> IsLessThan<T> isLessThan(T value) {
514-
return isLessThan(() -> value);
514+
return IsLessThan.of(value);
515515
}
516516

517517
static <T> IsLessThan<T> isLessThan(Supplier<T> valueSupplier) {
518-
return IsLessThan.of(valueSupplier);
518+
return isLessThan(valueSupplier.get());
519519
}
520520

521521
static <T> IsLessThanWithSubselect<T> isLessThan(Buildable<SelectModel> selectModelBuilder) {
@@ -527,19 +527,19 @@ static <T> IsLessThanColumn<T> isLessThan(BasicColumn column) {
527527
}
528528

529529
static <T> IsLessThan<T> isLessThanWhenPresent(T value) {
530-
return isLessThanWhenPresent(() -> value);
530+
return IsLessThan.of(value).filter(Objects::nonNull);
531531
}
532532

533533
static <T> IsLessThan<T> isLessThanWhenPresent(Supplier<T> valueSupplier) {
534-
return IsLessThan.of(valueSupplier).filter(Objects::nonNull);
534+
return isLessThanWhenPresent(valueSupplier.get());
535535
}
536536

537537
static <T> IsLessThanOrEqualTo<T> isLessThanOrEqualTo(T value) {
538-
return isLessThanOrEqualTo(() -> value);
538+
return IsLessThanOrEqualTo.of(value);
539539
}
540540

541541
static <T> IsLessThanOrEqualTo<T> isLessThanOrEqualTo(Supplier<T> valueSupplier) {
542-
return IsLessThanOrEqualTo.of(valueSupplier);
542+
return isLessThanOrEqualTo(valueSupplier.get());
543543
}
544544

545545
static <T> IsLessThanOrEqualToWithSubselect<T> isLessThanOrEqualTo(Buildable<SelectModel> selectModelBuilder) {
@@ -551,11 +551,11 @@ static <T> IsLessThanOrEqualToColumn<T> isLessThanOrEqualTo(BasicColumn column)
551551
}
552552

553553
static <T> IsLessThanOrEqualTo<T> isLessThanOrEqualToWhenPresent(T value) {
554-
return isLessThanOrEqualToWhenPresent(() -> value);
554+
return IsLessThanOrEqualTo.of(value).filter(Objects::nonNull);
555555
}
556556

557557
static <T> IsLessThanOrEqualTo<T> isLessThanOrEqualToWhenPresent(Supplier<T> valueSupplier) {
558-
return IsLessThanOrEqualTo.of(valueSupplier).filter(Objects::nonNull);
558+
return isLessThanOrEqualToWhenPresent(valueSupplier.get());
559559
}
560560

561561
@SafeVarargs
@@ -603,68 +603,68 @@ static <T> IsNotIn<T> isNotInWhenPresent(Collection<T> values) {
603603
}
604604

605605
static <T> IsBetween.Builder<T> isBetween(T value1) {
606-
return isBetween(() -> value1);
606+
return IsBetween.isBetween(value1);
607607
}
608608

609609
static <T> IsBetween.Builder<T> isBetween(Supplier<T> valueSupplier1) {
610-
return IsBetween.isBetween(valueSupplier1);
610+
return isBetween(valueSupplier1.get());
611611
}
612612

613613
static <T> IsBetween.WhenPresentBuilder<T> isBetweenWhenPresent(T value1) {
614-
return isBetweenWhenPresent(() -> value1);
614+
return IsBetween.isBetweenWhenPresent(value1);
615615
}
616616

617617
static <T> IsBetween.WhenPresentBuilder<T> isBetweenWhenPresent(Supplier<T> valueSupplier1) {
618-
return IsBetween.isBetweenWhenPresent(valueSupplier1);
618+
return isBetweenWhenPresent(valueSupplier1.get());
619619
}
620620

621621
static <T> IsNotBetween.Builder<T> isNotBetween(T value1) {
622-
return isNotBetween(() -> value1);
622+
return IsNotBetween.isNotBetween(value1);
623623
}
624624

625625
static <T> IsNotBetween.Builder<T> isNotBetween(Supplier<T> valueSupplier1) {
626-
return IsNotBetween.isNotBetween(valueSupplier1);
626+
return isNotBetween(valueSupplier1.get());
627627
}
628628

629629
static <T> IsNotBetween.WhenPresentBuilder<T> isNotBetweenWhenPresent(T value1) {
630-
return isNotBetweenWhenPresent(() -> value1);
630+
return IsNotBetween.isNotBetweenWhenPresent(value1);
631631
}
632632

633633
static <T> IsNotBetween.WhenPresentBuilder<T> isNotBetweenWhenPresent(Supplier<T> valueSupplier1) {
634-
return IsNotBetween.isNotBetweenWhenPresent(valueSupplier1);
634+
return isNotBetweenWhenPresent(valueSupplier1.get());
635635
}
636636

637637
// for string columns, but generic for columns with type handlers
638638
static <T> IsLike<T> isLike(T value) {
639-
return isLike(() -> value);
639+
return IsLike.of(value);
640640
}
641641

642642
static <T> IsLike<T> isLike(Supplier<T> valueSupplier) {
643-
return IsLike.of(valueSupplier);
643+
return isLike(valueSupplier.get());
644644
}
645645

646646
static <T> IsLike<T> isLikeWhenPresent(T value) {
647-
return isLikeWhenPresent(() -> value);
647+
return IsLike.of(value).filter(Objects::nonNull);
648648
}
649649

650650
static <T> IsLike<T> isLikeWhenPresent(Supplier<T> valueSupplier) {
651-
return IsLike.of(valueSupplier).filter(Objects::nonNull);
651+
return isLikeWhenPresent(valueSupplier.get());
652652
}
653653

654654
static <T> IsNotLike<T> isNotLike(T value) {
655-
return isNotLike(() -> value);
655+
return IsNotLike.of(value);
656656
}
657657

658658
static <T> IsNotLike<T> isNotLike(Supplier<T> valueSupplier) {
659-
return IsNotLike.of(valueSupplier);
659+
return isNotLike(valueSupplier.get());
660660
}
661661

662662
static <T> IsNotLike<T> isNotLikeWhenPresent(T value) {
663-
return isNotLikeWhenPresent(() -> value);
663+
return IsNotLike.of(value).filter(Objects::nonNull);
664664
}
665665

666666
static <T> IsNotLike<T> isNotLikeWhenPresent(Supplier<T> valueSupplier) {
667-
return IsNotLike.of(valueSupplier).filter(Objects::nonNull);
667+
return isNotLikeWhenPresent(valueSupplier.get());
668668
}
669669

670670
// shortcuts for booleans
@@ -678,35 +678,35 @@ static IsEqualTo<Boolean> isFalse() {
678678

679679
// conditions for strings only
680680
static IsLikeCaseInsensitive isLikeCaseInsensitive(String value) {
681-
return isLikeCaseInsensitive(() -> value);
681+
return IsLikeCaseInsensitive.of(value);
682682
}
683683

684684
static IsLikeCaseInsensitive isLikeCaseInsensitive(Supplier<String> valueSupplier) {
685-
return IsLikeCaseInsensitive.of(valueSupplier);
685+
return isLikeCaseInsensitive(valueSupplier.get());
686686
}
687687

688688
static IsLikeCaseInsensitive isLikeCaseInsensitiveWhenPresent(String value) {
689-
return isLikeCaseInsensitiveWhenPresent(() -> value);
689+
return IsLikeCaseInsensitive.of(value).filter(Objects::nonNull);
690690
}
691691

692692
static IsLikeCaseInsensitive isLikeCaseInsensitiveWhenPresent(Supplier<String> valueSupplier) {
693-
return IsLikeCaseInsensitive.of(valueSupplier).filter(Objects::nonNull);
693+
return isLikeCaseInsensitiveWhenPresent(valueSupplier.get());
694694
}
695695

696696
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitive(String value) {
697-
return isNotLikeCaseInsensitive(() -> value);
697+
return IsNotLikeCaseInsensitive.of(value);
698698
}
699699

700700
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitive(Supplier<String> valueSupplier) {
701-
return IsNotLikeCaseInsensitive.of(valueSupplier);
701+
return isNotLikeCaseInsensitive(valueSupplier.get());
702702
}
703703

704704
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitiveWhenPresent(String value) {
705-
return isNotLikeCaseInsensitiveWhenPresent(() -> value);
705+
return IsNotLikeCaseInsensitive.of(value).filter(Objects::nonNull);
706706
}
707707

708708
static IsNotLikeCaseInsensitive isNotLikeCaseInsensitiveWhenPresent(Supplier<String> valueSupplier) {
709-
return IsNotLikeCaseInsensitive.of(valueSupplier).filter(Objects::nonNull);
709+
return isNotLikeCaseInsensitiveWhenPresent(valueSupplier.get());
710710
}
711711

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

0 commit comments

Comments
 (0)