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