DATACMNS-286 - Extended Page/Pageable APIs.
Pageable and Page interfaces now expose API to easily access Pageables pointing to the previous and next Page instance if available.
This commit is contained in:
@@ -90,6 +90,24 @@ public interface Page<T> extends Iterable<T> {
|
||||
*/
|
||||
boolean isLastPage();
|
||||
|
||||
/**
|
||||
* Returns the {@link Pageable} to request the next {@link Page}. Can be {@literal null} in case the current
|
||||
* {@link Page} is already the last one. Clients should check {@link #hasNextPage()} before calling this method to
|
||||
* make sure they receive a non-{@literal null} value.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Pageable nextPageable();
|
||||
|
||||
/**
|
||||
* Returns the {@link Pageable} to request the previous page. Can be {@literal null} in case the current {@link Page}
|
||||
* is already the first one. Clients should check {@link #hasPreviousPage()} before calling this method make sure
|
||||
* receive a non-{@literal null} value.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Pageable previousPageable();
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Iterable#iterator()
|
||||
|
||||
@@ -135,6 +135,27 @@ public class PageImpl<T> implements Page<T>, Serializable {
|
||||
return !hasNextPage();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Page#nextPageable()
|
||||
*/
|
||||
public Pageable nextPageable() {
|
||||
return hasNextPage() ? pageable.next() : null;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Page#previousOrFirstPageable()
|
||||
*/
|
||||
public Pageable previousPageable() {
|
||||
|
||||
if (hasPreviousPage()) {
|
||||
return pageable.previousOrFirst();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Page#iterator()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2012 the original author or authors.
|
||||
* Copyright 2008-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -40,7 +40,6 @@ public class PageRequest implements Pageable, Serializable {
|
||||
* @param page
|
||||
*/
|
||||
public PageRequest(int page, int size) {
|
||||
|
||||
this(page, size, null);
|
||||
}
|
||||
|
||||
@@ -53,7 +52,6 @@ public class PageRequest implements Pageable, Serializable {
|
||||
* @param properties
|
||||
*/
|
||||
public PageRequest(int page, int size, Direction direction, String... properties) {
|
||||
|
||||
this(page, size, new Sort(direction, properties));
|
||||
}
|
||||
|
||||
@@ -62,16 +60,16 @@ public class PageRequest implements Pageable, Serializable {
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @param sort
|
||||
* @param sort can be {@literal null}.
|
||||
*/
|
||||
public PageRequest(int page, int size, Sort sort) {
|
||||
|
||||
if (0 > page) {
|
||||
if (page < 0) {
|
||||
throw new IllegalArgumentException("Page index must not be less than zero!");
|
||||
}
|
||||
|
||||
if (0 >= size) {
|
||||
throw new IllegalArgumentException("Page size must not be less than or equal to zero!");
|
||||
if (size < 0) {
|
||||
throw new IllegalArgumentException("Page size must not be less than zero!");
|
||||
}
|
||||
|
||||
this.page = page;
|
||||
@@ -80,50 +78,74 @@ public class PageRequest implements Pageable, Serializable {
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.domain.Pageable#getPageSize()
|
||||
*/
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getPageSize()
|
||||
*/
|
||||
public int getPageSize() {
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.domain.Pageable#getPageNumber()
|
||||
*/
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getPageNumber()
|
||||
*/
|
||||
public int getPageNumber() {
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.domain.Pageable#getFirstItem()
|
||||
*/
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getOffset()
|
||||
*/
|
||||
public int getOffset() {
|
||||
|
||||
return page * size;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.domain.Pageable#getSort()
|
||||
*/
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getSort()
|
||||
*/
|
||||
public Sort getSort() {
|
||||
|
||||
return sort;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#hasPrevious()
|
||||
*/
|
||||
public boolean hasPrevious() {
|
||||
return page > 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#next()
|
||||
*/
|
||||
public Pageable next() {
|
||||
return new PageRequest(page + 1, size, sort);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#previousOrFirst()
|
||||
*/
|
||||
public Pageable previousOrFirst() {
|
||||
return hasPrevious() ? new PageRequest(page - 1, size, sort) : this;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#first()
|
||||
*/
|
||||
public Pageable first() {
|
||||
return new PageRequest(0, size, sort);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
|
||||
@@ -146,10 +168,9 @@ public class PageRequest implements Pageable, Serializable {
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
@@ -161,4 +182,14 @@ public class PageRequest implements Pageable, Serializable {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Page request [number: %d, size %d, sort: %s]", page, size,
|
||||
sort == null ? null : sort.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2010 the original author or authors.
|
||||
* Copyright 2008-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,4 +49,33 @@ public interface Pageable {
|
||||
* @return
|
||||
*/
|
||||
Sort getSort();
|
||||
|
||||
/**
|
||||
* Returns the {@link Pageable} requesting the next {@link Page}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Pageable next();
|
||||
|
||||
/**
|
||||
* Returns the previous {@link Pageable} or the first {@link Pageable} if the current one already is the first one.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Pageable previousOrFirst();
|
||||
|
||||
/**
|
||||
* Returns the {@link Pageable} requesting the first page.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Pageable first();
|
||||
|
||||
/**
|
||||
* Returns whether there's a previous {@link Pageable} we can access from the current one. Will return
|
||||
* {@literal false} in case the current {@link Pageable} already refers to the first page.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
boolean hasPrevious();
|
||||
}
|
||||
|
||||
@@ -199,6 +199,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
* Returns the {@link Direction} enum for the given {@link String} value.
|
||||
*
|
||||
* @param value
|
||||
* @throws IllegalArgumentException in case the given value cannot be parsed into an enum value.
|
||||
* @return
|
||||
*/
|
||||
public static Direction fromString(String value) {
|
||||
@@ -210,6 +211,22 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
"Invalid value '%s' for orders given! Has to be either 'desc' or 'asc' (case insensitive).", value), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Direction} enum for the given {@link String} or null if it cannot be parsed into an enum
|
||||
* value.
|
||||
*
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public static Direction fromStringOrNull(String value) {
|
||||
|
||||
try {
|
||||
return fromString(value);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user