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;
17  
18  import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19  
20  import java.util.function.Function;
21  import java.util.function.Predicate;
22  import java.util.function.Supplier;
23  
24  import org.mybatis.dynamic.sql.render.RenderedParameterInfo;
25  import org.mybatis.dynamic.sql.render.RenderingContext;
26  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
27  
28  public abstract class AbstractSingleValueCondition<T> implements RenderableCondition<T> {
29      protected final T value;
30  
31      protected AbstractSingleValueCondition(T value) {
32          this.value = value;
33      }
34  
35      public T value() {
36          return value;
37      }
38  
39      protected <S extends AbstractSingleValueCondition<T>> S filterSupport(Predicate<? super T> predicate,
40              Supplier<S> emptySupplier, S self) {
41          if (isEmpty()) {
42              return self;
43          } else {
44              return predicate.test(value) ? self : emptySupplier.get();
45          }
46      }
47  
48      protected <R, S extends AbstractSingleValueCondition<R>> S mapSupport(Function<? super T, ? extends R> mapper,
49              Function<R, S> constructor, Supplier<S> emptySupplier) {
50          if (isEmpty()) {
51              return emptySupplier.get();
52          } else {
53              return constructor.apply(mapper.apply(value));
54          }
55      }
56  
57      public abstract String operator();
58  
59      @Override
60      public FragmentAndParameters renderCondition(RenderingContext renderingContext, BindableColumn<T> leftColumn) {
61          RenderedParameterInfo parameterInfo = renderingContext.calculateParameterInfo(leftColumn);
62          String finalFragment = operator() + spaceBefore(parameterInfo.renderedPlaceHolder());
63  
64          return FragmentAndParameters.withFragment(finalFragment)
65                  .withParameter(parameterInfo.parameterMapKey(), leftColumn.convertParameterType(value()))
66                  .build();
67      }
68  
69      /**
70       * Conditions may implement Filterable to add optionality to rendering.
71       *
72       * <p>If a condition is Filterable, then a user may add a filter to the usage of the condition that makes a decision
73       * whether to render the condition at runtime. Conditions that fail the filter will be dropped from the
74       * rendered SQL.
75       *
76       * <p>Implementations of Filterable may call
77       * {@link AbstractSingleValueCondition#filterSupport(Predicate, Supplier, AbstractSingleValueCondition)} as
78       * a common implementation of the filtering algorithm.
79       *
80       * @param <T> the Java type related to the database column type
81       */
82      public interface Filterable<T> {
83          /**
84           * If renderable and the value matches the predicate, returns this condition. Else returns a condition
85           *     that will not render.
86           *
87           * @param predicate predicate applied to the value, if renderable
88           * @return this condition if renderable and the value matches the predicate, otherwise a condition
89           *     that will not render.
90           */
91          AbstractSingleValueCondition<T> filter(Predicate<? super T> predicate);
92      }
93  
94      /**
95       * Conditions may implement Mappable to alter condition values or types during rendering.
96       *
97       * <p>If a condition is Mappable, then a user may add a mapper to the usage of the condition that can alter the
98       * values of a condition, or change that datatype.
99       *
100      * <p>Implementations of Mappable may call
101      * {@link AbstractSingleValueCondition#mapSupport(Function, Function, Supplier)} as
102      * a common implementation of the mapping algorithm.
103      *
104      * @param <T> the Java type related to the database column type
105      */
106     public interface Mappable<T> {
107         /**
108          * If renderable, apply the mapping to the value and return a new condition with the new value. Else return a
109          * condition that will not render (this).
110          *
111          * @param mapper a mapping function to apply to the value, if renderable
112          * @param <R> type of the new condition
113          * @return a new condition with the result of applying the mapper to the value of this condition,
114          *     if renderable, otherwise a condition that will not render.
115          */
116         <R> AbstractSingleValueCondition<R> map(Function<? super T, ? extends R> mapper);
117     }
118 }