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.mybatis.dynamic.sql.AbstractTwoValueCondition;
25  
26  public class IsBetween<T> extends AbstractTwoValueCondition<@NonNull T>
27          implements AbstractTwoValueCondition.Filterable<T>, AbstractTwoValueCondition.Mappable<T> {
28      private static final IsBetween<?> EMPTY = new IsBetween<Object>(-1, -1) {
29          @Override
30          public Object value1() {
31              throw new NoSuchElementException("No value present"); //$NON-NLS-1$
32          }
33  
34          @Override
35          public Object value2() {
36              throw new NoSuchElementException("No value present"); //$NON-NLS-1$
37          }
38  
39          @Override
40          public boolean isEmpty() {
41              return true;
42          }
43      };
44  
45      public static <T> IsBetween<T> empty() {
46          @SuppressWarnings("unchecked")
47          IsBetween<T> t = (IsBetween<T>) EMPTY;
48          return t;
49      }
50  
51      protected IsBetween(T value1, T value2) {
52          super(value1, value2);
53      }
54  
55      @Override
56      public String operator1() {
57          return "between"; //$NON-NLS-1$
58      }
59  
60      @Override
61      public String operator2() {
62          return "and"; //$NON-NLS-1$
63      }
64  
65      @Override
66      public IsBetween<T> filter(BiPredicate<? super @NonNull T, ? super @NonNull T> predicate) {
67          return filterSupport(predicate, IsBetween::empty, this);
68      }
69  
70      @Override
71      public IsBetween<T> filter(Predicate<? super @NonNull T> predicate) {
72          return filterSupport(predicate, IsBetween::empty, this);
73      }
74  
75      @Override
76      public <R> IsBetween<R> map(Function<? super @NonNull T, ? extends @NonNull R> mapper1,
77                                  Function<? super @NonNull T, ? extends @NonNull R> mapper2) {
78          return mapSupport(mapper1, mapper2, IsBetween::new, IsBetween::empty);
79      }
80  
81      @Override
82      public <R> IsBetween<R> map(Function<? super @NonNull T, ? extends @NonNull R> mapper) {
83          return map(mapper, mapper);
84      }
85  
86      public static <T> Builder<T> isBetween(T value1) {
87          return new Builder<>(value1);
88      }
89  
90      public static class Builder<T> extends AndGatherer<T, IsBetween<T>> {
91          private Builder(T value1) {
92              super(value1);
93          }
94  
95          @Override
96          protected IsBetween<T> build(T value2) {
97              return new IsBetween<>(value1, value2);
98          }
99      }
100 }