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.Arrays;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.function.Function;
22  import java.util.function.Predicate;
23  
24  import org.mybatis.dynamic.sql.AbstractListValueCondition;
25  import org.mybatis.dynamic.sql.render.RenderingContext;
26  import org.mybatis.dynamic.sql.util.StringUtilities;
27  import org.mybatis.dynamic.sql.util.Validator;
28  
29  public class IsInCaseInsensitive<T> extends AbstractListValueCondition<T>
30          implements CaseInsensitiveRenderableCondition<T>, AbstractListValueCondition.Filterable<T>,
31          AbstractListValueCondition.Mappable<T> {
32      private static final IsInCaseInsensitive<?> EMPTY = new IsInCaseInsensitive<>(Collections.emptyList());
33  
34      public static <T> IsInCaseInsensitive<T> empty() {
35          @SuppressWarnings("unchecked")
36          IsInCaseInsensitive<T> t = (IsInCaseInsensitive<T>) EMPTY;
37          return t;
38      }
39  
40      protected IsInCaseInsensitive(Collection<T> values) {
41          super(values.stream().map(StringUtilities::upperCaseIfPossible).toList());
42      }
43  
44      @Override
45      public boolean shouldRender(RenderingContext renderingContext) {
46          Validator.assertNotEmpty(values, "ERROR.44", "IsInCaseInsensitive"); //$NON-NLS-1$ //$NON-NLS-2$
47          return true;
48      }
49  
50      @Override
51      public String operator() {
52          return "in"; //$NON-NLS-1$
53      }
54  
55      @Override
56      public IsInCaseInsensitive<T> filter(Predicate<? super T> predicate) {
57          return filterSupport(predicate, IsInCaseInsensitive::new, this, IsInCaseInsensitive::empty);
58      }
59  
60      @Override
61      public <R> IsInCaseInsensitive<R> map(Function<? super T, ? extends R> mapper) {
62          return mapSupport(mapper, IsInCaseInsensitive::new, IsInCaseInsensitive::empty);
63      }
64  
65      @SafeVarargs
66      public static <T> IsInCaseInsensitive<T> of(T... values) {
67          return of(Arrays.asList(values));
68      }
69  
70      public static <T> IsInCaseInsensitive<T> of(Collection<T> values) {
71          return new IsInCaseInsensitive<>(values);
72      }
73  }