1 /*
2  * Copyright 2008-2020 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.springframework.data.domain;
17
18 import org.springframework.data.domain.Sort.Direction;
19 import org.springframework.lang.Nullable;
20 import org.springframework.util.Assert;
21
22 /**
23  * Basic Java Bean implementation of {@code Pageable}.
24  *
25  * @author Oliver Gierke
26  * @author Thomas Darimont
27  */

28 public class PageRequest extends AbstractPageRequest {
29
30     private static final long serialVersionUID = -4541509938956089562L;
31
32     private final Sort sort;
33
34     /**
35      * Creates a new {@link PageRequest} with sort parameters applied.
36      *
37      * @param page zero-based page index, must not be negative.
38      * @param size the size of the page to be returned, must be greater than 0.
39      * @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead.
40      */

41     protected PageRequest(int page, int size, Sort sort) {
42
43         super(page, size);
44
45         Assert.notNull(sort, "Sort must not be null!");
46
47         this.sort = sort;
48     }
49
50     /**
51      * Creates a new unsorted {@link PageRequest}.
52      *
53      * @param page zero-based page index, must not be negative.
54      * @param size the size of the page to be returned, must be greater than 0.
55      * @since 2.0
56      */

57     public static PageRequest of(int page, int size) {
58         return of(page, size, Sort.unsorted());
59     }
60
61     /**
62      * Creates a new {@link PageRequest} with sort parameters applied.
63      *
64      * @param page zero-based page index.
65      * @param size the size of the page to be returned.
66      * @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead.
67      * @since 2.0
68      */

69     public static PageRequest of(int page, int size, Sort sort) {
70         return new PageRequest(page, size, sort);
71     }
72
73     /**
74      * Creates a new {@link PageRequest} with sort direction and properties applied.
75      *
76      * @param page zero-based page index, must not be negative.
77      * @param size the size of the page to be returned, must be greater than 0.
78      * @param direction must not be {@literal null}.
79      * @param properties must not be {@literal null}.
80      * @since 2.0
81      */

82     public static PageRequest of(int page, int size, Direction direction, String... properties) {
83         return of(page, size, Sort.by(direction, properties));
84     }
85
86     /*
87      * (non-Javadoc)
88      * @see org.springframework.data.domain.Pageable#getSort()
89      */

90     public Sort getSort() {
91         return sort;
92     }
93
94     /*
95      * (non-Javadoc)
96      * @see org.springframework.data.domain.Pageable#next()
97      */

98     @Override
99     public Pageable next() {
100         return new PageRequest(getPageNumber() + 1, getPageSize(), getSort());
101     }
102
103     /*
104      * (non-Javadoc)
105      * @see org.springframework.data.domain.AbstractPageRequest#previous()
106      */

107     @Override
108     public PageRequest previous() {
109         return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort());
110     }
111
112     /*
113      * (non-Javadoc)
114      * @see org.springframework.data.domain.Pageable#first()
115      */

116     @Override
117     public Pageable first() {
118         return new PageRequest(0, getPageSize(), getSort());
119     }
120
121     /*
122      * (non-Javadoc)
123      * @see java.lang.Object#equals(java.lang.Object)
124      */

125     @Override
126     public boolean equals(@Nullable Object obj) {
127
128         if (this == obj) {
129             return true;
130         }
131
132         if (!(obj instanceof PageRequest)) {
133             return false;
134         }
135
136         PageRequest that = (PageRequest) obj;
137
138         return super.equals(that) && this.sort.equals(that.sort);
139     }
140
141     /*
142      * (non-Javadoc)
143      * @see java.lang.Object#hashCode()
144      */

145     @Override
146     public int hashCode() {
147         return 31 * super.hashCode() + sort.hashCode();
148     }
149
150     /*
151      * (non-Javadoc)
152      * @see java.lang.Object#toString()
153      */

154     @Override
155     public String toString() {
156         return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(), sort);
157     }
158 }
159