View Javadoc
1   /*
2    * Copyright (c) 2023. Roland T. Lichti, Kaiserpfalz EDV-Service.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16   */
17  
18  package de.kaiserpfalzedv.commons.core.data;
19  
20  import org.eclipse.microprofile.openapi.annotations.media.Schema;
21  
22  import com.fasterxml.jackson.annotation.JsonIgnore;
23  import com.fasterxml.jackson.annotation.JsonInclude;
24  
25  import de.kaiserpfalzedv.commons.api.data.Paging;
26  import lombok.AllArgsConstructor;
27  import lombok.Builder;
28  import lombok.EqualsAndHashCode;
29  import lombok.Getter;
30  import lombok.ToString;
31  import lombok.extern.jackson.Jacksonized;
32  
33  /**
34   * DispatchmentFile contains all data to a single dispatchment
35   *
36   * @author klenkes {@literal <rlichti@kaiserpfalz-edv.de>}
37   * @since 2.0.0  2023-01-06
38   */
39  @Schema(
40          description = "Paging data including calculating next/previous and first/last pages available.",
41          title = "Pagination data for transfer lists"
42  )
43  @Jacksonized
44  @JsonInclude(JsonInclude.Include.NON_ABSENT)
45  @Builder(toBuilder = true)
46  @AllArgsConstructor
47  @Getter
48  @ToString
49  @EqualsAndHashCode
50  public class PagingImpl implements Paging {
51      private static final long serialVersionUID = 0L;
52  
53      @Builder.Default
54      private final long start = 0;
55  
56      @Builder.Default
57      private final long size = 20;
58  
59      private final long count;
60  
61      private final long total;
62  
63      @Override
64      @JsonIgnore
65      @Schema(hidden = true)
66      public Paging firstPage() {
67          return this.toBuilder()
68                  .start(0)
69                  .count(this.calculatePageSize(0))
70                  .build();
71      }
72  
73      @Override
74      @JsonIgnore
75      @Schema(hidden = true)
76      public Paging previousPage() {
77          if (this.start - this.size <= 0) {
78              return this.firstPage();
79          }
80  
81          return this.toBuilder()
82                  .start(this.start - this.size)
83                  .count(this.calculatePageSize(this.start - this.size))
84                  .build();
85      }
86  
87      @Override
88      @JsonIgnore
89      @Schema(hidden = true)
90      public Paging nextPage() {
91          return this.calculateNextPage(this.start + this.size);
92      }
93  
94      @Override
95      @JsonIgnore
96      @Schema(hidden = true)
97      public Paging lastPage() {
98          return this.calculateNextPage(this.total / this.size * this.size);
99      }
100 
101     private long calculatePageSize(final long start) {
102         if (start >= this.total) {
103             return 0;
104         }
105 
106         if (start + this.size >= this.total) {
107             return this.total - start;
108         }
109 
110         return  this.size;
111     }
112 
113     private Paging calculateNextPage(final long start) {
114         return this.toBuilder()
115                 .start(start)
116                 .count(this.calculatePageSize(start))
117                 .build();
118     }
119 }