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