View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.dynamic.sql.where.condition;
17  
18  import java.util.NoSuchElementException;
19  import java.util.function.BiPredicate;
20  import java.util.function.Function;
21  import java.util.function.Predicate;
22  
23  import org.jspecify.annotations.NonNull;
24  import org.jspecify.annotations.Nullable;
25  import org.mybatis.dynamic.sql.AbstractTwoValueCondition;
26  
27  public class IsBetweenWhenPresent<T> extends AbstractTwoValueCondition<T>
28          implements AbstractTwoValueCondition.Filterable<T>, AbstractTwoValueCondition.Mappable<T> {
29      private static final IsBetweenWhenPresent<?> EMPTY = new IsBetweenWhenPresent<Object>(-1, -1) {
30          @Override
31          public Object value1() {
32              throw new NoSuchElementException("No value present"); //$NON-NLS-1$
33          }
34  
35          @Override
36          public Object value2() {
37              throw new NoSuchElementException("No value present"); //$NON-NLS-1$
38          }
39  
40          @Override
41          public boolean isEmpty() {
42              return true;
43          }
44      };
45  
46      public static <T> IsBetweenWhenPresent<T> empty() {
47          @SuppressWarnings("unchecked")
48          IsBetweenWhenPresent<T> t = (IsBetweenWhenPresent<T>) EMPTY;
49          return t;
50      }
51  
52      protected IsBetweenWhenPresent(T value1, T value2) {
53          super(value1, value2);
54      }
55  
56      @Override
57      public String operator1() {
58          return "between"; //$NON-NLS-1$
59      }
60  
61      @Override
62      public String operator2() {
63          return "and"; //$NON-NLS-1$
64      }
65  
66      @Override
67      public IsBetweenWhenPresent<T> filter(BiPredicate<? super @NonNull T, ? super @NonNull T> predicate) {
68          return filterSupport(predicate, IsBetweenWhenPresent::empty, this);
69      }
70  
71      @Override
72      public IsBetweenWhenPresent<T> filter(Predicate<? super @NonNull T> predicate) {
73          return filterSupport(predicate, IsBetweenWhenPresent::empty, this);
74      }
75  
76      @Override
77      public <R> IsBetweenWhenPresent<R> map(Function<? super @NonNull T, ? extends @Nullable R> mapper1,
78                                             Function<? super @NonNull T, ? extends @Nullable R> mapper2) {
79          return mapSupport(mapper1, mapper2, IsBetweenWhenPresent::of, IsBetweenWhenPresent::empty);
80      }
81  
82      @Override
83      public <R> IsBetweenWhenPresent<R> map(Function<? super @NonNull T, ? extends @Nullable R> mapper) {
84          return map(mapper, mapper);
85      }
86  
87      public static <T> IsBetweenWhenPresent<T> of(@Nullable T value1, @Nullable T value2) {
88          if (value1 == null || value2 == null) {
89              return empty();
90          } else {
91              return new IsBetweenWhenPresent<>(value1, value2);
92          }
93      }
94  
95      public static <T> Builder<T> isBetweenWhenPresent(@Nullable T value1) {
96          return new Builder<>(value1);
97      }
98  
99      public static class Builder<T> extends AndWhenPresentGatherer<T, IsBetweenWhenPresent<T>> {
100         private Builder(@Nullable T value1) {
101             super(value1);
102         }
103 
104         @Override
105         protected IsBetweenWhenPresent<T> build(@Nullable T value2) {
106             return of(value1, value2);
107         }
108     }
109 }