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.select;
17  
18  import java.util.Optional;
19  
20  import org.jspecify.annotations.Nullable;
21  
22  public class PagingModel {
23  
24      private final @Nullable Long limit;
25      private final @Nullable Long offset;
26      private final @Nullable Long fetchFirstRows;
27  
28      private PagingModel(Builder builder) {
29          super();
30          limit = builder.limit;
31          offset = builder.offset;
32          fetchFirstRows = builder.fetchFirstRows;
33      }
34  
35      public Optional<Long> limit() {
36          return Optional.ofNullable(limit);
37      }
38  
39      public Optional<Long> offset() {
40          return Optional.ofNullable(offset);
41      }
42  
43      public Optional<Long> fetchFirstRows() {
44          return Optional.ofNullable(fetchFirstRows);
45      }
46  
47      public static class Builder {
48          private @Nullable Long limit;
49          private @Nullable Long offset;
50          private @Nullable Long fetchFirstRows;
51  
52          public Builder withLimit(@Nullable Long limit) {
53              this.limit = limit;
54              return this;
55          }
56  
57          public Builder withOffset(@Nullable Long offset) {
58              this.offset = offset;
59              return this;
60          }
61  
62          public Builder withFetchFirstRows(@Nullable Long fetchFirstRows) {
63              this.fetchFirstRows = fetchFirstRows;
64              return this;
65          }
66  
67          public Optional<PagingModel> build() {
68              if (limit == null && offset == null && fetchFirstRows == null) {
69                  return Optional.empty();
70              }
71  
72              return Optional.of(new PagingModel(this));
73          }
74      }
75  }