Created
February 26, 2020 16:48
-
-
Save gcdd1993/dc2a6afb400d401f7a4a2a048f6b74e4 to your computer and use it in GitHub Desktop.
Spring将已存在的List手动分页
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.maxtropy.cloud.util; | |
import lombok.experimental.UtilityClass; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.PageImpl; | |
import org.springframework.data.domain.Pageable; | |
import javax.annotation.Nullable; | |
import java.util.ArrayList; | |
import java.util.List; | |
@UtilityClass | |
public class Pagination { | |
private static final int DEFAULT_PAGE_NUMBER = 0; | |
private static final int NO_PAGINATION = Integer.MAX_VALUE; | |
/** | |
* Convenience method for getPage(List originalList, Pageable pageable). | |
*/ | |
public static <T> Page<T> getPage(Iterable<T> data, Pageable pageable) { | |
List<T> resultList = new ArrayList<>(); | |
data.forEach(resultList::add); | |
return getPage(resultList, pageable); | |
} | |
/** | |
* Return Page of the list using Page defaults. | |
* See {@link #getPage(List, Pageable)} | |
*/ | |
public static <T> Page<T> getPage(List<T> originalList) { | |
return getPage(originalList, null); | |
} | |
/** | |
* Returns the Page for a subset of the specified list, determined by the pageable passed in. | |
* | |
* @param originalList A list of values, some or all of which should be included in a page. | |
* @param pageable An object used to encapsulate the pagination related values: page and size. | |
*/ | |
public static <T> Page<T> getPage(List<T> originalList, @Nullable Pageable pageable) { | |
int pageSize = getPageSize(pageable); | |
int pageNumber = getPageNumber(pageable); | |
int fromIndex = pageNumber * pageSize; | |
int toIndex = fromIndex + pageSize; | |
//Validate toIndex. | |
//Note that toIndex is exclusive, which is why we don't want originalList.size() - 1. | |
int maxPossibleToIndex = originalList.size(); | |
if (toIndex > maxPossibleToIndex) { | |
toIndex = maxPossibleToIndex; | |
} | |
//Validate fromIndex | |
int maxPossibleFromIndex = originalList.size() - 1; | |
if (fromIndex > maxPossibleFromIndex) { | |
// If the fronIndex is out of bounds, set it and toIndex to the same value. | |
// This will cause us to return an empty list. | |
fromIndex = toIndex = 0; | |
} | |
List<T> subList = originalList.subList(fromIndex, toIndex); | |
return new PageImpl<>(subList, pageable, originalList.size()); | |
} | |
/** | |
* Returns the Page for the entirety of the specified list. Intended for use when the | |
* supplied list already contains exactly the set of elements requested and there's | |
* no need to return a subset of it. | |
*/ | |
public static <T> Page<T> getPage(List<T> subList, Pageable pageable, long fullListSize) { | |
return new PageImpl<>(subList, pageable, fullListSize); | |
} | |
private static int getPageNumber(Pageable pageable) { | |
if (pageable == null) { | |
return DEFAULT_PAGE_NUMBER; | |
} | |
return pageable.getPageNumber(); | |
} | |
private static int getPageSize(Pageable pageable) { | |
if (pageable == null) { | |
return NO_PAGINATION; | |
} | |
return pageable.getPageSize(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment