• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-mirana / #112

19 Mar 2025 04:46AM UTC coverage: 87.128% (+2.7%) from 84.4%
#112

push

web-flow
Merge pull request #53 from trydofor/develop

bump 3.0.0 to semver

1645 of 1747 new or added lines in 25 files covered. (94.16%)

2 existing lines in 1 file now uncovered.

7141 of 8196 relevant lines covered (87.13%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.54
/src/main/java/pro/fessional/mirana/page/PageResult.java
1
package pro.fessional.mirana.page;
2

3
import org.jetbrains.annotations.Contract;
4
import org.jetbrains.annotations.NotNull;
5
import org.jetbrains.annotations.Nullable;
6
import pro.fessional.mirana.data.Null;
7
import pro.fessional.mirana.data.R;
8

9
import java.beans.Transient;
10
import java.util.ArrayList;
11
import java.util.Collection;
12
import java.util.Collections;
13
import java.util.HashMap;
14
import java.util.Iterator;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Objects;
18
import java.util.function.Function;
19

20
/**
21
 * <pre>
22
 * It is not recommended to change the page data after construction.
23
 *
24
 * page - 1-based, not less than 1.
25
 * size - 1-based, not less than 1.
26
 * sort - sort string
27
 * totalPage - 1-based, not less than 1, calculated by data and size
28
 * totalData - 0-based, not less than 0, not more than 2.1 billion.
29
 * meta - in addition to data information
30
 * </pre>.
31
 *
32
 * @author trydofor
33
 * @since 2020-09-29
34
 */
35
public class PageResult<E> extends R<Collection<E>> implements Iterable<E> {
36
    private static final long serialVersionUID = 19791023L;
37

38
    private final List<E> empty = Collections.emptyList();
1✔
39

40
    private int page = 1;
1✔
41
    private int size = 1;
1✔
42
    private String sort = Null.Str;
1✔
43
    private int totalPage = Null.Int32;
1✔
44
    private int totalData = Null.Int32;
1✔
45

46
    private Map<String, Object> meta = null;
1✔
47

48
    public PageResult() {
1✔
49
        setData(empty);
1✔
50
    }
1✔
51

52
    /**
53
     * current page, 1-based, not less than 1.
54
     */
55
    public int getPage() {
56
        return page;
1✔
57
    }
58

59
    @Contract("_->this")
60
    public PageResult<E> setPage(int page) {
61
        this.page = Math.max(page, 1);
1✔
62
        return this;
1✔
63
    }
64

65
    /**
66
     * page size, 1-based, not less than 1.
67
     */
68
    public int getSize() {
69
        return size;
1✔
70
    }
71

72
    public void setSize(int size) {
73
        this.size = size;
×
74
    }
×
75

76
    /**
77
     * Sorting String
78
     *
79
     * @return Sorting string
80
     * @see PageUtil#sort(String)
81
     */
82
    @Nullable
83
    public String getSort() {
84
        return sort;
×
85
    }
86

87
    /**
88
     * Sorting String
89
     *
90
     * @param sort Sorting string
91
     * @return this
92
     * @see PageUtil#sort(String)
93
     */
94
    @Contract("_->this")
95
    public PageResult<E> setSort(String sort) {
96
        this.sort = sort;
×
97
        return this;
×
98
    }
99

100
    /**
101
     * 1-based, not less than 1, calculated by data and size.
102
     */
103
    public int getTotalPage() {
104
        return totalPage;
1✔
105
    }
106

107
    public void setTotalPage(int totalPage) {
108
        this.totalPage = totalPage;
×
109
    }
×
110

111
    /**
112
     * 0-based, not less than 0, not more than 2.1 billion.
113
     */
114
    public int getTotalData() {
115
        return totalData;
1✔
116
    }
117

118
    public void setTotalData(int totalData) {
119
        this.totalData = totalData;
×
120
    }
×
121

122
    @Transient
123
    @NotNull
124
    public List<E> toList() {
125
        final Collection<E> data = getData();
1✔
126
        if (data instanceof List<?>) {
1✔
127
            return (List<E>) data;
1✔
128
        }
129
        else {
130
            return new ArrayList<>(data);
×
131
        }
132
    }
133

134
    /**
135
     * Set the total data  and page size, then calculate the total page
136
     *
137
     * @param totalData total count
138
     * @param pageSize  page size
139
     * @return this
140
     */
141
    @Contract("_,_->this")
142
    public PageResult<E> setTotalInfo(int totalData, int pageSize) {
143
        this.size = Math.max(pageSize, 1);
1✔
144
        this.totalData = Math.max(totalData, 0);
1✔
145
        this.totalPage = PageUtil.totalPage(totalData, pageSize);
1✔
146
        return this;
1✔
147
    }
148

149
    @Override
150
    @Contract("_->this")
151
    public PageResult<E> setData(Collection<E> ds) {
152
        if (ds == null || ds.isEmpty()) {
1✔
153
            super.setData(empty);
1✔
154
        }
155
        else {
156
            super.setData(ds);
1✔
157
        }
158
        return this;
1✔
159
    }
160

161
    @Override
162
    @NotNull
163
    public Collection<E> getData() {
164
        final Collection<E> data = super.getData();
1✔
165
        return data == null ? empty : data;
1✔
166
    }
167

168
    @Contract("_->this")
169
    public PageResult<E> addData(E e) {
170
        if (e != null) {
×
171
            Collection<E> data = getData();
×
172
            if (data == empty) {
×
173
                data = new ArrayList<>(size > 0 ? size : 20);
×
174
            }
175
            data.add(e);
×
176
        }
177
        return this;
×
178
    }
179

180
    @Contract("_->this")
181
    public PageResult<E> addData(Collection<E> ds) {
182
        if (ds != null && !ds.isEmpty()) {
×
183
            Collection<E> data = getData();
×
184
            if (data == empty) {
×
185
                data = new ArrayList<>(size > 0 ? size : 20);
×
186
            }
187
            data.addAll(ds);
×
188
        }
189
        return this;
×
190
    }
191

192
    @NotNull
193
    @Override
194
    public Iterator<E> iterator() {
195
        final Collection<E> data = getData();
×
196
        return data.iterator();
×
197
    }
198

199
    public Map<String, ?> getMeta() {
200
        return meta;
×
201
    }
202

203
    @Contract("_->this")
204
    public PageResult<E> setMeta(Map<String, Object> meta) {
205
        this.meta = meta;
×
206
        return this;
×
207
    }
208

209
    @Contract("_,_->this")
210
    public PageResult<E> addMeta(String key, Object value) {
211
        if (meta == null) {
×
212
            meta = new HashMap<>();
×
213
        }
214
        meta.put(key, value);
×
215
        return this;
×
216
    }
217

218
    @SuppressWarnings("unchecked")
219
    @Transient
220
    public <T> T getMeta(String key) {
221
        return meta == null ? null : (T) meta.get(key);
×
222
    }
223

224
    public <T> PageResult<T> into(Function<E, T> fun) {
225
        final List<E> es = toList();
1✔
226
        final ArrayList<T> dd = new ArrayList<>(es.size());
1✔
227
        for (E e : es) {
1✔
228
            dd.add(fun.apply(e));
1✔
229
        }
1✔
230

231
        return new PageResult<T>()
1✔
232
            .setPage(page)
1✔
233
            .setTotalInfo(totalData, size)
1✔
234
            .setData(dd)
1✔
235
            .setSuccess(success)
1✔
236
            .cast();
1✔
237
    }
238

239
    @Override
240
    public String toString() {
241
        return "PageResult{" +
×
242
               "success=" + success +
243
               ", message='" + message + '\'' +
244
               ", code='" + code + '\'' +
245
               ", data=" + data +
246
               ", page=" + page +
247
               ", size=" + size +
248
               ", sort='" + sort + '\'' +
249
               ", totalPage=" + totalPage +
250
               ", totalData=" + totalData +
251
               ", meta=" + meta +
252
               '}';
253
    }
254

255
    @Override
256
    public boolean equals(Object o) {
257
        if (this == o) return true;
1✔
258
        if (!(o instanceof PageResult)) return false;
1✔
259
        if (!super.equals(o)) return false;
1✔
260
        PageResult<?> that = (PageResult<?>) o;
1✔
261
        return page == that.page && size == that.size
1✔
262
               && totalPage == that.totalPage && totalData == that.totalData
263
               && Objects.equals(empty, that.empty) && Objects.equals(sort, that.sort)
1✔
264
               && Objects.equals(meta, that.meta);
1✔
265
    }
266

267
    @Override
268
    public int hashCode() {
269
        return Objects.hash(super.hashCode(), empty, page, size, sort, totalPage, totalData, meta);
×
270
    }
271

272
    // ////////
273

274
    /**
275
     * constructor
276
     *
277
     * @param total total data count
278
     * @param data  current page of data
279
     * @param pg    query of page
280
     * @param <T>   data type
281
     * @return page result
282
     */
283
    public static <T> PageResult<T> ok(int total, Collection<T> data, PageQuery pg) {
284
        return new PageResult<T>()
×
285
            .setPage(pg.getPage())
×
286
            .setTotalInfo(total, pg.getSize())
×
287
            .setSort(pg.getSort())
×
288
            .setData(data)
×
NEW
289
            .setSuccess(true)
×
NEW
290
            .cast();
×
291
    }
292

293
    /**
294
     * constructor
295
     *
296
     * @param total total data count
297
     * @param data  current page of data
298
     * @param page  current page
299
     * @param size  page size
300
     * @param <T>   data type
301
     * @return page result
302
     */
303
    public static <T> PageResult<T> ok(int total, Collection<T> data, int page, int size) {
304
        return new PageResult<T>()
1✔
305
            .setPage(page)
1✔
306
            .setTotalInfo(total, size)
1✔
307
            .setData(data)
1✔
308
            .setSuccess(true)
1✔
309
            .cast();
1✔
310
    }
311

312
    /**
313
     * constructor an empty page result of total is 0, size is 1.
314
     */
315
    public static <T> PageResult<T> empty() {
316
        return new PageResult<T>()
×
317
            .setPage(1)
×
318
            .setTotalInfo(0, 1)
×
NEW
319
            .setSuccess(true)
×
NEW
320
            .cast();
×
321
    }
322
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc