DATACMNS-867 - Cleanups in Pageable API.
Dropped Pageable.NONE in favor of ….unpaged() for consistency with Sort.unsorted(). Extracted the implementation for the value backing it into an Unpaged enum instance. Introduced defaulted isPaged() / isUnpaged() to avoid having to compare instances. JavaDoc in Sort and a couple of API polishes.
This commit is contained in:
@@ -58,7 +58,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
|
||||
* @see org.springframework.data.domain.Slice#getNumber()
|
||||
*/
|
||||
public int getNumber() {
|
||||
return pageable == Pageable.NONE ? 0 : pageable.getPageNumber();
|
||||
return pageable.isPaged() ? pageable.getPageNumber() : 0;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -66,7 +66,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
|
||||
* @see org.springframework.data.domain.Slice#getSize()
|
||||
*/
|
||||
public int getSize() {
|
||||
return pageable == Pageable.NONE ? 0 : pageable.getPageSize();
|
||||
return pageable.isPaged() ? pageable.getPageSize() : 0;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -106,7 +106,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
|
||||
* @see org.springframework.data.domain.Slice#nextPageable()
|
||||
*/
|
||||
public Pageable nextPageable() {
|
||||
return hasNext() ? pageable.next() : Pageable.NONE;
|
||||
return hasNext() ? pageable.next() : Pageable.unpaged();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -114,7 +114,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
|
||||
* @see org.springframework.data.domain.Slice#previousPageable()
|
||||
*/
|
||||
public Pageable previousPageable() {
|
||||
return hasPrevious() ? pageable.previousOrFirst() : Pageable.NONE;
|
||||
return hasPrevious() ? pageable.previousOrFirst() : Pageable.unpaged();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -47,7 +47,7 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
|
||||
|
||||
this.pageable = pageable;
|
||||
|
||||
Optional<Pageable> foo = Pageable.NONE == pageable ? Optional.empty() : Optional.of(pageable);
|
||||
Optional<Pageable> foo = pageable.isPaged() ? Optional.of(pageable) : Optional.empty();
|
||||
|
||||
this.total = foo.filter(it -> !content.isEmpty())//
|
||||
.filter(it -> it.getOffset() + it.getPageSize() > total)//
|
||||
@@ -62,7 +62,7 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
|
||||
* @param content must not be {@literal null}.
|
||||
*/
|
||||
public PageImpl(List<T> content) {
|
||||
this(content, Pageable.NONE, null == content ? 0 : content.size());
|
||||
this(content, Pageable.unpaged(), null == content ? 0 : content.size());
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.domain;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Abstract interface for pagination information.
|
||||
*
|
||||
@@ -22,6 +24,33 @@ package org.springframework.data.domain;
|
||||
*/
|
||||
public interface Pageable {
|
||||
|
||||
/**
|
||||
* Returns a {@link Pageable} instance representing no pagination setup.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
static Pageable unpaged() {
|
||||
return Unpaged.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the current {@link Pageable} contains pagination information.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
default boolean isPaged() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the current {@link Pageable} does not contain pagination information.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
default boolean isUnpaged() {
|
||||
return !isPaged();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the page to be returned.
|
||||
*
|
||||
@@ -50,7 +79,16 @@ public interface Pageable {
|
||||
*/
|
||||
Sort getSort();
|
||||
|
||||
/**
|
||||
* Returns the current {@link Sort} or the given one if the current one is unsorted.
|
||||
*
|
||||
* @param sort must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
default Sort getSortOr(Sort sort) {
|
||||
|
||||
Assert.notNull(sort, "Fallback Sort must not be null!");
|
||||
|
||||
return getSort().isSorted() ? getSort() : sort;
|
||||
}
|
||||
|
||||
@@ -82,47 +120,4 @@ public interface Pageable {
|
||||
* @return
|
||||
*/
|
||||
boolean hasPrevious();
|
||||
|
||||
public static Pageable NONE = new Pageable() {
|
||||
|
||||
@Override
|
||||
public Pageable previousOrFirst() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pageable next() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sort getSort() {
|
||||
return Sort.unsorted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPageSize() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPageNumber() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getOffset() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pageable first() {
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
/**
|
||||
* Creates a new {@link Sort} instance.
|
||||
*
|
||||
* @param direction defaults to {@linke Sort#DEFAULT_DIRECTION} (for {@literal null} cases, too)
|
||||
* @param direction defaults to {@link Sort#DEFAULT_DIRECTION} (for {@literal null} cases, too)
|
||||
* @param properties must not be {@literal null}, empty or contain {@literal null} or empty strings.
|
||||
*/
|
||||
public Sort(Direction direction, String... properties) {
|
||||
@@ -109,38 +109,80 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Sort} for the given properties.
|
||||
*
|
||||
* @param properties must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static Sort by(String... properties) {
|
||||
|
||||
Assert.notNull(properties, "Properties must not be null!");
|
||||
|
||||
return properties.length == 0 ? Sort.unsorted() : new Sort(properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Sort} for the given {@link Order}s.
|
||||
*
|
||||
* @param orders must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static Sort by(List<Order> orders) {
|
||||
|
||||
Assert.notNull(orders, "Orders must not be null!");
|
||||
|
||||
return orders.isEmpty() ? Sort.unsorted() : new Sort(orders);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Sort} for the given {@link Order}s.
|
||||
*
|
||||
* @param orders must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static Sort by(Order... orders) {
|
||||
|
||||
Assert.notNull(orders, "Orders must not be null!");
|
||||
|
||||
return new Sort(orders);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Sort} instances representing no sorting setup at all.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Sort unsorted() {
|
||||
return UNSORTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link Sort} with the current setup but descending order direction.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Sort descending() {
|
||||
return withDirection(Direction.DESC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link Sort} with the current setup but ascendin order direction.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Sort ascending() {
|
||||
return withDirection(Direction.ASC);
|
||||
}
|
||||
|
||||
private Sort withDirection(Direction direction) {
|
||||
return new Sort(orders.stream().map(it -> new Order(direction, it.getProperty())).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public boolean isSorted() {
|
||||
return !orders.isEmpty();
|
||||
}
|
||||
|
||||
public boolean isUnorted() {
|
||||
return !isSorted();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link Sort} consisting of the {@link Order}s of the current {@link Sort} combined with the given
|
||||
* ones.
|
||||
@@ -160,7 +202,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
these.add(order);
|
||||
}
|
||||
|
||||
return new Sort(these);
|
||||
return Sort.by(these);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -229,6 +271,17 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
|
||||
return orders.isEmpty() ? "UNSORTED" : StringUtils.collectionToCommaDelimitedString(orders);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Sort} with the current setup but the given order direction.
|
||||
*
|
||||
* @param direction
|
||||
* @return
|
||||
*/
|
||||
private Sort withDirection(Direction direction) {
|
||||
|
||||
return Sort.by(orders.stream().map(it -> new Order(direction, it.getProperty())).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumeration for sort directions.
|
||||
*
|
||||
|
||||
107
src/main/java/org/springframework/data/domain/Unpaged.java
Normal file
107
src/main/java/org/springframework/data/domain/Unpaged.java
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.domain;
|
||||
|
||||
/**
|
||||
* {@link Pageable} implementation to represent the absence of pagination information.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
enum Unpaged implements Pageable {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#isPaged()
|
||||
*/
|
||||
@Override
|
||||
public boolean isPaged() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#previousOrFirst()
|
||||
*/
|
||||
@Override
|
||||
public Pageable previousOrFirst() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#next()
|
||||
*/
|
||||
@Override
|
||||
public Pageable next() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#hasPrevious()
|
||||
*/
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getSort()
|
||||
*/
|
||||
@Override
|
||||
public Sort getSort() {
|
||||
return Sort.unsorted();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getPageSize()
|
||||
*/
|
||||
@Override
|
||||
public int getPageSize() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getPageNumber()
|
||||
*/
|
||||
@Override
|
||||
public int getPageNumber() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getOffset()
|
||||
*/
|
||||
@Override
|
||||
public long getOffset() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#first()
|
||||
*/
|
||||
@Override
|
||||
public Pageable first() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user