BrAPIPage.java
/*
* Copyright 2020 Global Crop Diversity Trust
*
* 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.gringlobal.brapi;
import java.util.Arrays;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
/**
* Data pagination request.
*
* @author Matija Obreza
*/
public class BrAPIPage {
public static final int MAX_PAGE_SIZE = 1000;
/** The default sort properties. */
private final String[] DEFAULT_SORT_PROPERTIES = { "id" };
/** Page (0-based). */
private Integer page;
/** Page size (length). */
private Integer pageSize;
/** Sort direction. */
private Sort.Direction dir;
/** Sort properties. */
private String[] sort;
/**
* Gets the page.
*
* @return the page
*/
public int getPage() {
return page;
}
/**
* Sets the page.
*
* @param p the new page
*/
public void setPage(int p) {
this.page = p;
}
/**
* Gets the pageSize.
*
* @return the pageSize
*/
public int getPageSize() {
return pageSize;
}
/**
* Sets the pageSize.
*
* @param l the new pageSize
*/
public void setPageSize(int l) {
this.pageSize = l;
}
/**
* Gets the d.
*
* @return the d
*/
public Sort.Direction getDir() {
return dir;
}
/**
* Sets the d.
*
* @param d the new d
*/
public void setDir(Sort.Direction d) {
this.dir = d;
}
/**
* Gets the s.
*
* @return the s
*/
public String[] getSort() {
return sort;
}
/**
* Sets the s.
*
* @param s the new s
*/
public void setSort(String[] s) {
this.sort = s;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "BrAPIPage page=" + page + ", pageSize=" + pageSize + ", dir=" + dir + ", sort=" + Arrays.toString(sort);
}
/**
* Get sort direction or {@link Sort.Direction#ASC} if null.
*
* @param defaultDir the default dir
* @return sort direction or {@link Sort.Direction#ASC}
*/
private Sort.Direction getDirection(Sort.Direction defaultDir) {
return dir == null ? defaultDir : dir;
}
/**
* Gets list of sort properties or provided defaults.
*
* @param defaultSortProps the default sort props
* @return provided properties or defaultSortProps
*/
private String[] getSortProperties(String[] defaultSortProps) {
return sort == null || sort.length == 0 ? defaultSortProps : sort;
}
/**
* To page request using the {@link #DEFAULT_SORT_PROPERTIES} and ASC sort
*
* @return the pageable
*/
public Pageable toPageRequest() {
return PageRequest.of(page == null ? 0 : page, Integer.min(pageSize == null ? MAX_PAGE_SIZE : pageSize, MAX_PAGE_SIZE), getDirection(Sort.Direction.ASC), getSortProperties(DEFAULT_SORT_PROPERTIES));
}
/**
* To page request.
*
* @param maxPageSize the max page size
* @param defaultDir the default dir
* @param defaultSort the default sort
* @return the pageable
*/
public Pageable toPageRequest(int maxPageSize, Sort.Direction defaultDir, String... defaultSort) {
return PageRequest.of(page == null ? 0 : page, Integer.min(pageSize == null ? maxPageSize : pageSize, maxPageSize), getDirection(defaultDir), getSortProperties(defaultSort));
}
/**
* To page request.
*
* @param maxPageSize the max page size
* @param sort the sort
* @return the pageable
*/
public static Pageable toPageRequest(int maxPageSize, Sort sort) {
return PageRequest.of(0, maxPageSize, sort);
}
public static Pageable make(BrAPIPage page) {
return page == null ? PageRequest.of(0, MAX_PAGE_SIZE) : page.toPageRequest();
}
}