1 package org.oxerr.viagogo.model.request;
2
3 import java.beans.Transient;
4 import java.io.Serializable;
5 import java.time.Instant;
6 import java.util.Arrays;
7 import java.util.stream.Collectors;
8
9 import org.apache.commons.lang3.builder.EqualsBuilder;
10 import org.apache.commons.lang3.builder.HashCodeBuilder;
11 import org.oxerr.viagogo.model.Link;
12
13 public abstract class PagedRequest implements Serializable {
14
15 public interface Sort {
16
17 String getCode();
18
19 }
20
21 private static final long serialVersionUID = 2023112401L;
22
23
24
25
26 private Integer page;
27
28
29
30
31 private Integer pageSize;
32
33
34
35
36
37 private Instant updatedSince;
38
39
40
41
42
43 private String sort;
44
45 protected PagedRequest() {
46 }
47
48 protected PagedRequest(Link link) {
49 this.setPage(link.getFirstAsInteger("page"));
50 this.setPageSize(link.getFirstAsInteger("page_size"));
51 this.setUpdatedSince(link.getFirstAsInstant("updated_since"));
52 this.setSort(link.getFirstAsString("sort"));
53 }
54
55 public Integer getPage() {
56 return page;
57 }
58
59 public void setPage(Integer page) {
60 this.page = page;
61 }
62
63 public Integer getPageSize() {
64 return pageSize;
65 }
66
67 public void setPageSize(Integer pageSize) {
68 this.pageSize = pageSize;
69 }
70
71 public Instant getUpdatedSince() {
72 return updatedSince;
73 }
74
75 public void setUpdatedSince(Instant updatedSince) {
76 this.updatedSince = updatedSince;
77 }
78
79 public String getSort() {
80 return sort;
81 }
82
83 public void setSort(String sort) {
84 this.sort = sort;
85 }
86
87 @Transient
88 public void setSort(Sort... sorts) {
89 this.setSort(Arrays.stream(sorts).map(Sort::getCode).collect(Collectors.joining(",")));
90 }
91
92 @Override
93 public int hashCode() {
94 return HashCodeBuilder.reflectionHashCode(this);
95 }
96
97 @Override
98 public boolean equals(Object obj) {
99 if (this == obj) {
100 return true;
101 }
102 if (!(obj instanceof PagedRequest)) {
103 return false;
104 }
105 PagedRequest rhs = (PagedRequest) obj;
106 return EqualsBuilder.reflectionEquals(this, rhs);
107 }
108
109 }