View Javadoc
1   /*
2    *    Copyright 2016-2024 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.Objects;
22  import java.util.function.Predicate;
23  import java.util.function.UnaryOperator;
24  import java.util.stream.Collectors;
25  
26  import org.mybatis.dynamic.sql.AbstractListValueCondition;
27  import org.mybatis.dynamic.sql.util.StringUtilities;
28  
29  public class IsInCaseInsensitiveWhenPresent extends AbstractListValueCondition<String>
30          implements CaseInsensitiveVisitableCondition {
31      private static final IsInCaseInsensitiveWhenPresent EMPTY = new IsInCaseInsensitiveWhenPresent(Collections.emptyList());
32  
33      public static IsInCaseInsensitiveWhenPresent empty() {
34          return EMPTY;
35      }
36  
37      protected IsInCaseInsensitiveWhenPresent(Collection<String> values) {
38          super(values.stream().filter(Objects::nonNull).collect(Collectors.toList()));
39      }
40  
41      @Override
42      public String operator() {
43          return "in"; //$NON-NLS-1$
44      }
45  
46      @Override
47      public IsInCaseInsensitiveWhenPresent filter(Predicate<? super String> predicate) {
48          return filterSupport(predicate, IsInCaseInsensitiveWhenPresent::new, this, IsInCaseInsensitiveWhenPresent::empty);
49      }
50  
51      /**
52       * If not empty, apply the mapping to each value in the list return a new condition with the mapped values.
53       *     Else return an empty condition (this).
54       *
55       * @param mapper a mapping function to apply to the values, if not empty
56       * @return a new condition with mapped values if renderable, otherwise an empty condition
57       */
58      public IsInCaseInsensitiveWhenPresent map(UnaryOperator<String> mapper) {
59          return mapSupport(mapper, IsInCaseInsensitiveWhenPresent::new, IsInCaseInsensitiveWhenPresent::empty);
60      }
61  
62      public static IsInCaseInsensitiveWhenPresent of(String... values) {
63          return of(Arrays.asList(values));
64      }
65  
66      public static IsInCaseInsensitiveWhenPresent of(Collection<String> values) {
67          return new IsInCaseInsensitiveWhenPresent(values).map(StringUtilities::safelyUpperCase);
68      }
69  }