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.render;
17  
18  import java.util.Objects;
19  import java.util.stream.Collectors;
20  
21  import org.jspecify.annotations.Nullable;
22  import org.mybatis.dynamic.sql.BindableColumn;
23  import org.mybatis.dynamic.sql.RenderableCondition;
24  import org.mybatis.dynamic.sql.render.RenderingContext;
25  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
26  import org.mybatis.dynamic.sql.util.FragmentCollector;
27  
28  public class ColumnAndConditionRenderer<T> {
29      private final BindableColumn<T> column;
30      private final RenderableCondition<T> condition;
31      private final RenderingContext renderingContext;
32  
33      private ColumnAndConditionRenderer(Builder<T> builder) {
34          column = Objects.requireNonNull(builder.column);
35          condition = Objects.requireNonNull(builder.condition);
36          renderingContext = Objects.requireNonNull(builder.renderingContext);
37      }
38  
39      public FragmentAndParameters render() {
40          FragmentCollector fc = new FragmentCollector();
41          fc.add(condition.renderLeftColumn(renderingContext, column));
42          fc.add(condition.renderCondition(renderingContext, column));
43          return fc.toFragmentAndParameters(Collectors.joining(" ")); //$NON-NLS-1$
44      }
45  
46      public static class Builder<T> {
47          private @Nullable BindableColumn<T> column;
48          private @Nullable RenderableCondition<T> condition;
49          private @Nullable RenderingContext renderingContext;
50  
51          public Builder<T> withColumn(BindableColumn<T> column) {
52              this.column = column;
53              return this;
54          }
55  
56          public Builder<T> withCondition(RenderableCondition<T> condition) {
57              this.condition = condition;
58              return this;
59          }
60  
61          public Builder<T> withRenderingContext(RenderingContext renderingContext) {
62              this.renderingContext = renderingContext;
63              return this;
64          }
65  
66          public ColumnAndConditionRenderer<T> build() {
67              return new ColumnAndConditionRenderer<>(this);
68          }
69      }
70  }