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 java.util.Optional;
19  
20  import org.jspecify.annotations.Nullable;
21  
22  /**
23   * This class represents a criteria group without an AND or an OR connector. This is useful
24   * in situations where the initial SqlCriterion in a list should be further grouped
25   * as in an expression like ((A < 5 and B > 6) or C = 3)
26   *
27   * @author Jeff Butler, inspired by @JoshuaJeme
28   *
29   * @since 1.4.0
30   */
31  public class CriteriaGroup extends SqlCriterion {
32      private final @Nullable SqlCriterion initialCriterion;
33  
34      protected CriteriaGroup(AbstractGroupBuilder<?> builder) {
35          super(builder);
36          initialCriterion = builder.initialCriterion;
37      }
38  
39      public Optional<SqlCriterion> initialCriterion() {
40          return Optional.ofNullable(initialCriterion);
41      }
42  
43      @Override
44      public <R> R accept(SqlCriterionVisitor<R> visitor) {
45          return visitor.visit(this);
46      }
47  
48      public abstract static class AbstractGroupBuilder<T extends AbstractGroupBuilder<T>> extends AbstractBuilder<T> {
49          private @Nullable SqlCriterion initialCriterion;
50  
51          public T withInitialCriterion(@Nullable SqlCriterion initialCriterion) {
52              this.initialCriterion = initialCriterion;
53              return getThis();
54          }
55      }
56  
57      public static class Builder extends AbstractGroupBuilder<Builder> {
58          public CriteriaGroup build() {
59              return new CriteriaGroup(this);
60          }
61  
62          @Override
63          protected Builder getThis() {
64              return this;
65          }
66      }
67  }