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