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.Function;
20  import java.util.function.Predicate;
21  
22  import org.jspecify.annotations.NonNull;
23  import org.jspecify.annotations.Nullable;
24  import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
25  import org.mybatis.dynamic.sql.util.StringUtilities;
26  
27  public class IsLikeCaseInsensitiveWhenPresent<T> extends AbstractSingleValueCondition<T>
28          implements CaseInsensitiveRenderableCondition<T>, AbstractSingleValueCondition.Filterable<T>,
29          AbstractSingleValueCondition.Mappable<T> {
30      private static final IsLikeCaseInsensitiveWhenPresent<?> EMPTY =
31              new IsLikeCaseInsensitiveWhenPresent<>("") { //$NON-NLS-1$
32          @Override
33          public String value() {
34              throw new NoSuchElementException("No value present"); //$NON-NLS-1$
35          }
36  
37          @Override
38          public boolean isEmpty() {
39              return true;
40          }
41      };
42  
43      public static <T> IsLikeCaseInsensitiveWhenPresent<T> empty() {
44          @SuppressWarnings("unchecked")
45          IsLikeCaseInsensitiveWhenPresent<T> t = (IsLikeCaseInsensitiveWhenPresent<T>) EMPTY;
46          return t;
47      }
48  
49      protected IsLikeCaseInsensitiveWhenPresent(T value) {
50          super(StringUtilities.upperCaseIfPossible(value));
51      }
52  
53      @Override
54      public String operator() {
55          return "like"; //$NON-NLS-1$
56      }
57  
58      @Override
59      public IsLikeCaseInsensitiveWhenPresent<T> filter(Predicate<? super @NonNull T> predicate) {
60          return filterSupport(predicate, IsLikeCaseInsensitiveWhenPresent::empty, this);
61      }
62  
63      @Override
64      public <R> IsLikeCaseInsensitiveWhenPresent<R> map(Function<? super @NonNull T, ? extends @Nullable R> mapper) {
65          return mapSupport(mapper, IsLikeCaseInsensitiveWhenPresent::of, IsLikeCaseInsensitiveWhenPresent::empty);
66      }
67  
68      public static <T> IsLikeCaseInsensitiveWhenPresent<T> of(@Nullable T value) {
69          if (value == null) {
70              return empty();
71          } else {
72              return new IsLikeCaseInsensitiveWhenPresent<>(value);
73          }
74      }
75  }