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.
2020import java .util .function .UnaryOperator ;
2121
2222import org .mybatis .dynamic .sql .AbstractTwoValueCondition ;
23+ import org .mybatis .dynamic .sql .util .Predicates ;
2324
2425public class IsBetween <T > extends AbstractTwoValueCondition <T > {
2526
2627 protected IsBetween (Supplier <T > valueSupplier1 , Supplier <T > valueSupplier2 ) {
2728 super (valueSupplier1 , valueSupplier2 );
2829 }
2930
30- protected IsBetween (Supplier <T > valueSupplier1 , Supplier <T > valueSupplier2 , BiPredicate <T , T > predicate ) {
31- super (valueSupplier1 , valueSupplier2 , predicate );
32- }
33-
3431 @ Override
3532 public String renderCondition (String columnName , String placeholder1 , String placeholder2 ) {
3633 return columnName + " between " + placeholder1 + " and " + placeholder2 ; //$NON-NLS-1$ //$NON-NLS-2$
3734 }
3835
36+ /**
37+ * If renderable and the values match the predicate, returns this condition. Else returns a condition
38+ * that will not render.
39+ *
40+ * @deprecated replaced by {@link IsBetween#filter(BiPredicate)}
41+ * @param predicate predicate applied to the values, if renderable
42+ * @return this condition if renderable and the values match the predicate, otherwise a condition
43+ * that will not render.
44+ */
45+ @ Deprecated
46+ public IsBetween <T > when (BiPredicate <T , T > predicate ) {
47+ return filter (predicate );
48+ }
49+
50+ /**
51+ * If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
52+ * condition that will not render (this).
53+ *
54+ * @deprecated replaced by {@link IsBetween#map(UnaryOperator, UnaryOperator)}
55+ * @param mapper1 a mapping function to apply to the first value, if renderable
56+ * @param mapper2 a mapping function to apply to the second value, if renderable
57+ * @return a new condition with the result of applying the mappers to the values of this condition,
58+ * if renderable, otherwise a condition that will not render.
59+ */
60+ @ Deprecated
61+ public IsBetween <T > then (UnaryOperator <T > mapper1 , UnaryOperator <T > mapper2 ) {
62+ return map (mapper1 , mapper2 );
63+ }
64+
65+ /**
66+ * If renderable and the values match the predicate, returns this condition. Else returns a condition
67+ * that will not render.
68+ *
69+ * @param predicate predicate applied to the values, if renderable
70+ * @return this condition if renderable and the values match the predicate, otherwise a condition
71+ * that will not render.
72+ */
73+ public IsBetween <T > filter (BiPredicate <T , T > predicate ) {
74+ if (shouldRender ()) {
75+ return predicate .test (value1 (), value2 ()) ? this : EmptyIsBetween .empty ();
76+ } else {
77+ return this ;
78+ }
79+ }
80+
81+ /**
82+ * If renderable, apply the mappings to the values and return a new condition with the new values. Else return a
83+ * condition that will not render (this).
84+ *
85+ * @param mapper1 a mapping function to apply to the first value, if renderable
86+ * @param mapper2 a mapping function to apply to the second value, if renderable
87+ * @return a new condition with the result of applying the mappers to the values of this condition,
88+ * if renderable, otherwise a condition that will not render.
89+ */
90+ public IsBetween <T > map (UnaryOperator <T > mapper1 , UnaryOperator <T > mapper2 ) {
91+ return shouldRender () ? new IsBetween <>(() -> mapper1 .apply (value1 ()), () -> mapper2 .apply (value2 ())) : this ;
92+ }
93+
94+ public static <T > Builder <T > isBetween (Supplier <T > valueSupplier1 ) {
95+ return new Builder <>(valueSupplier1 );
96+ }
97+
98+ public static <T > WhenPresentBuilder <T > isBetweenWhenPresent (Supplier <T > valueSupplier1 ) {
99+ return new WhenPresentBuilder <>(valueSupplier1 );
100+ }
101+
39102 public static class Builder <T > extends AndGatherer <T , IsBetween <T >> {
40103 private Builder (Supplier <T > valueSupplier1 ) {
41104 super (valueSupplier1 );
@@ -47,16 +110,33 @@ protected IsBetween<T> build() {
47110 }
48111 }
49112
50- public static <T > Builder <T > isBetween (Supplier <T > valueSupplier1 ) {
51- return new Builder <>(valueSupplier1 );
52- }
113+ public static class WhenPresentBuilder <T > extends AndGatherer <T , IsBetween <T >> {
114+ private WhenPresentBuilder (Supplier <T > valueSupplier1 ) {
115+ super (valueSupplier1 );
116+ }
53117
54- public IsBetween <T > when (BiPredicate <T , T > predicate ) {
55- return new IsBetween <>(valueSupplier1 , valueSupplier2 , predicate );
118+ @ Override
119+ protected IsBetween <T > build () {
120+ return new IsBetween <>(valueSupplier1 , valueSupplier2 ).filter (Predicates .bothPresent ());
121+ }
56122 }
57123
58- public IsBetween <T > then (UnaryOperator <T > transformer1 , UnaryOperator <T > transformer2 ) {
59- return shouldRender () ? new IsBetween <>(() -> transformer1 .apply (value1 ()),
60- () -> transformer2 .apply (value2 ())) : this ;
124+ public static class EmptyIsBetween <T > extends IsBetween <T > {
125+ private static final EmptyIsBetween <?> EMPTY = new EmptyIsBetween <>();
126+
127+ public static <T > EmptyIsBetween <T > empty () {
128+ @ SuppressWarnings ("unchecked" )
129+ EmptyIsBetween <T > t = (EmptyIsBetween <T >) EMPTY ;
130+ return t ;
131+ }
132+
133+ public EmptyIsBetween () {
134+ super (() -> null , () -> null );
135+ }
136+
137+ @ Override
138+ public boolean shouldRender () {
139+ return false ;
140+ }
61141 }
62142}
0 commit comments