1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
35
36
37
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 }