Skip to content

Commit 31535c1

Browse files
committed
feat(11): Updated findAll by Pageable and Sorting.
1 parent 492edd2 commit 31535c1

5 files changed

Lines changed: 104 additions & 261 deletions

File tree

src/bangmaple/jdbc/paging/AbstractPageRequest.java

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.io.Serializable;
44

5-
public abstract class AbstractPageRequest implements Serializable {
5+
public abstract class AbstractPageRequest implements Pageable, Serializable {
66

77
private static final long serialVersionUID = 1232825578694716871L;
88

@@ -16,7 +16,7 @@ public abstract class AbstractPageRequest implements Serializable {
1616
* @param page must not be less than zero.
1717
* @param size must not be less than one.
1818
*/
19-
public AbstractPageRequest(int page, int size) {
19+
protected AbstractPageRequest(int page, int size) {
2020

2121
if (page < 0) {
2222
throw new IllegalArgumentException("Page index must not be less than zero!");
@@ -54,73 +54,4 @@ public long getOffset() {
5454
return (long) page * (long) size;
5555
}
5656

57-
/*
58-
* (non-Javadoc)
59-
* @see org.springframework.data.domain.Pageable#hasPrevious()
60-
*/
61-
public boolean hasPrevious() {
62-
return page > 0;
63-
}
64-
65-
/*
66-
* (non-Javadoc)
67-
* @see org.springframework.data.domain.Pageable#previousOrFirst()
68-
*/
69-
public Pageable previousOrFirst() {
70-
return hasPrevious() ? previous() : first();
71-
}
72-
73-
/*
74-
* (non-Javadoc)
75-
* @see org.springframework.data.domain.Pageable#next()
76-
*/
77-
public abstract Pageable next();
78-
79-
/**
80-
* Returns the {@link Pageable} requesting the previous {@link Page}.
81-
*
82-
* @return
83-
*/
84-
public abstract Pageable previous();
85-
86-
/*
87-
* (non-Javadoc)
88-
* @see org.springframework.data.domain.Pageable#first()
89-
*/
90-
public abstract Pageable first();
91-
92-
/*
93-
* (non-Javadoc)
94-
* @see java.lang.Object#hashCode()
95-
*/
96-
@Override
97-
public int hashCode() {
98-
99-
final int prime = 31;
100-
int result = 1;
101-
102-
result = prime * result + page;
103-
result = prime * result + size;
104-
105-
return result;
106-
}
107-
108-
/*
109-
* (non-Javadoc)
110-
* @see java.lang.Object#equals(java.lang.Object)
111-
*/
112-
@Override
113-
public boolean equals(Object obj) {
114-
115-
if (this == obj) {
116-
return true;
117-
}
118-
119-
if (obj == null || getClass() != obj.getClass()) {
120-
return false;
121-
}
122-
123-
AbstractPageRequest other = (AbstractPageRequest) obj;
124-
return this.page == other.page && this.size == other.size;
125-
}
12657
}
Lines changed: 32 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package bangmaple.jdbc.paging;
22

33
import java.io.Serializable;
4+
import java.util.Arrays;
45

56
public class PageRequest extends AbstractPageRequest implements Serializable {
67

78
private static final long serialVersionUID = -4541509938956089562L;
89

9-
private final Sort sort;
10+
private boolean ordering = true;
11+
private String[] properties = {};
1012

1113
/**
1214
* Creates a new {@link PageRequest} with sort parameters applied.
1315
*
1416
* @param page zero-based page index, must not be negative.
1517
* @param size the size of the page to be returned, must be greater than 0.
16-
* @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead.
1718
*/
18-
protected PageRequest(int page, int size, Sort sort) {
19+
protected PageRequest(int page, int size, boolean ordering, String... properties) {
1920

2021
super(page, size);
2122

22-
this.sort = sort;
23+
this.ordering = ordering;
24+
this.properties = properties;
2325
}
2426

2527
/**
@@ -29,30 +31,30 @@ protected PageRequest(int page, int size, Sort sort) {
2931
* @param size the size of the page to be returned, must be greater than 0.
3032
*/
3133
public static PageRequest of(int page, int size) {
32-
return of(page, size, Sort.unsorted());
34+
return new PageRequest(page, size, Pageable.SORT_ASC, "");
3335
}
3436

3537
/**
3638
* Creates a new {@link PageRequest} with sort parameters applied.
3739
*
38-
* @param page zero-based page index.
39-
* @param size the size of the page to be returned.
40-
* @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead.
41-
*/
42-
public static PageRequest of(int page, int size, Sort sort) {
43-
return new PageRequest(page, size, sort);
40+
* @param page zero-based page index.
41+
* @param size the size of the page to be returned.
42+
* @param ordering Pageable.ASC (true) for Ascending order, Pageable.DESC (false) for Descending order.
43+
*/
44+
public static PageRequest of(int page, int size, boolean ordering) {
45+
return new PageRequest(page, size, ordering, "");
4446
}
4547

4648
/**
4749
* Creates a new {@link PageRequest} with sort direction and properties applied.
4850
*
49-
* @param page zero-based page index, must not be negative.
50-
* @param size the size of the page to be returned, must be greater than 0.
51-
* @param direction must not be {@literal null}.
51+
* @param page zero-based page index, must not be negative.
52+
* @param size the size of the page to be returned, must be greater than 0.
53+
* @param ordering Pageable.ASC (true) for Ascending order, Pageable.DESC (false) for Descending order.
5254
* @param properties must not be {@literal null}.
5355
*/
54-
public static PageRequest of(int page, int size, Sort.Direction direction, String... properties) {
55-
return of(page, size, Sort.by(direction, properties));
56+
public static PageRequest of(int page, int size, boolean ordering, String... properties) {
57+
return new PageRequest(page, size, ordering, properties);
5658
}
5759

5860
/**
@@ -65,85 +67,31 @@ public static PageRequest ofSize(int pageSize) {
6567
return PageRequest.of(0, pageSize);
6668
}
6769

68-
public Sort getSort() {
69-
return sort;
70-
}
71-
72-
@Override
73-
public Pageable next() {
74-
return (Pageable) new PageRequest(getPageNumber() + 1, getPageSize(), getSort());
75-
}
76-
77-
/*
78-
* (non-Javadoc)
79-
*/
8070
@Override
81-
public Pageable previous() {
82-
return (Pageable) (getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort()));
71+
public boolean isAscending() {
72+
return ordering == Pageable.SORT_ASC;
8373
}
8474

8575
@Override
86-
public Pageable first() {
87-
return (Pageable) new PageRequest(0, getPageSize(), getSort());
76+
public boolean isDescending() {
77+
return ordering == Pageable.SORT_DESC;
8878
}
8979

90-
/*
91-
* (non-Javadoc)
92-
* @see java.lang.Object#equals(java.lang.Object)
93-
*/
94-
@Override
95-
public boolean equals( Object obj) {
96-
97-
if (this == obj) {
98-
return true;
99-
}
100-
101-
if (!(obj instanceof PageRequest)) {
102-
return false;
80+
public String getProperties() {
81+
StringBuilder result = new StringBuilder();
82+
for (int i = 0; i < properties.length; i++) {
83+
if (i == properties.length - 1) {
84+
result.append(properties[i]);
85+
break;
86+
}
87+
result.append(properties[i]).append(", ");
10388
}
104-
105-
PageRequest that = (PageRequest) obj;
106-
107-
return super.equals(that) && this.sort.equals(that.sort);
108-
}
109-
110-
/**
111-
* Creates a new {@link PageRequest} with {@link Sort.Direction} and {@code properties} applied.
112-
*
113-
* @param direction must not be {@literal null}.
114-
* @param properties must not be {@literal null}.
115-
* @return a new {@link PageRequest}.
116-
*/
117-
public PageRequest withSort(Sort.Direction direction, String... properties) {
118-
return new PageRequest(getPageNumber(), getPageSize(), Sort.by(direction, properties));
89+
return result.toString();
11990
}
12091

121-
/**
122-
* Creates a new {@link PageRequest} with {@link Sort} applied.
123-
*
124-
* @param sort must not be {@literal null}.
125-
* @return a new {@link PageRequest}.
126-
*/
127-
public PageRequest withSort(Sort sort) {
128-
return new PageRequest(getPageNumber(), getPageSize(), sort);
129-
}
130-
131-
/*
132-
* (non-Javadoc)
133-
* @see java.lang.Object#hashCode()
134-
*/
135-
@Override
136-
public int hashCode() {
137-
return 31 * super.hashCode() + sort.hashCode();
138-
}
139-
140-
/*
141-
* (non-Javadoc)
142-
* @see java.lang.Object#toString()
143-
*/
14492
@Override
14593
public String toString() {
146-
return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(), sort);
94+
return String.format("Page request [number: %d, size %d]", getPageNumber(), getPageSize());
14795
}
14896

14997
}

src/bangmaple/jdbc/paging/Pageable.java

Lines changed: 5 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@
44

55
public interface Pageable {
66

7-
/**
8-
* Returns a {@link Pageable} instance representing no pagination setup.
9-
*
10-
* @return
11-
*/
12-
static Pageable unpaged() {
13-
return null;
14-
// return Unpaged.INSTANCE;
15-
}
7+
boolean SORT_ASC = true;
8+
boolean SORT_DESC = false;
169

1710
/**
1811
* Creates a new {@link Pageable} for the first page (page number {@code 0}) given {@code pageSize} .
@@ -26,24 +19,6 @@ static Pageable ofSize(int pageSize) {
2619
// return PageRequest.of(0, pageSize);
2720
}
2821

29-
/**
30-
* Returns whether the current {@link Pageable} contains pagination information.
31-
*
32-
* @return
33-
*/
34-
default boolean isPaged() {
35-
return true;
36-
}
37-
38-
/**
39-
* Returns whether the current {@link Pageable} does not contain pagination information.
40-
*
41-
* @return
42-
*/
43-
default boolean isUnpaged() {
44-
return !isPaged();
45-
}
46-
4722
/**
4823
* Returns the page to be returned.
4924
*
@@ -65,68 +40,10 @@ default boolean isUnpaged() {
6540
*/
6641
long getOffset();
6742

68-
/**
69-
* Returns the sorting parameters.
70-
*
71-
* @return
72-
*/
73-
Sort getSort();
43+
boolean isAscending();
7444

75-
/**
76-
* Returns the current {@link Sort} or the given one if the current one is unsorted.
77-
*
78-
* @param sort must not be {@literal null}.
79-
* @return
80-
*/
81-
default Sort getSortOr(Sort sort) {
82-
return getSort().isSorted() ? getSort() : sort;
83-
}
45+
boolean isDescending();
8446

85-
/**
86-
* Returns the {@link Pageable} requesting the next {@link Page}.
87-
*
88-
* @return
89-
*/
90-
Pageable next();
91-
92-
/**
93-
* Returns the previous {@link Pageable} or the first {@link Pageable} if the current one already is the first one.
94-
*
95-
* @return
96-
*/
97-
Pageable previousOrFirst();
98-
99-
/**
100-
* Returns the {@link Pageable} requesting the first page.
101-
*
102-
* @return
103-
*/
104-
Pageable first();
105-
106-
/**
107-
* Creates a new {@link Pageable} with {@code pageNumber} applied.
108-
*
109-
* @param pageNumber
110-
* @return a new {@link PageRequest}.
111-
* @since 2.5
112-
*/
113-
Pageable withPage(int pageNumber);
114-
115-
/**
116-
* Returns whether there's a previous {@link Pageable} we can access from the current one. Will return
117-
* {@literal false} in case the current {@link Pageable} already refers to the first page.
118-
*
119-
* @return
120-
*/
121-
boolean hasPrevious();
122-
123-
/**
124-
* Returns an {@link Optional} so that it can easily be mapped on.
125-
*
126-
* @return
127-
*/
128-
default Optional<Pageable> toOptional() {
129-
return isUnpaged() ? Optional.empty() : Optional.of(this);
130-
}
47+
String getProperties();
13148

13249
}

0 commit comments

Comments
 (0)