org.springframework.data.domain.PageImplCustom Maven / Gradle / Ivy
package org.springframework.data.domain;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.util.UriComponentsBuilder;
/**
* @see org.springframework.data.domain.Page
* @see org.springframework.data.domain.SliceImpl
* @see org.springframework.hateoas.Link
*/
public class PageImplCustom extends PageImpl {
private static final long serialVersionUID = -5021629657557291345L;
private boolean direction;
private UriComponentsBuilder builder;
private Map link = new LinkedHashMap();
private static final Field PAGEABLE = ReflectionUtils.findField(PageImpl.class, "pageable");
private static final Field TOTAL = ReflectionUtils.findField(PageImpl.class, "total");
static {
ReflectionUtils.makeAccessible(PAGEABLE);
ReflectionUtils.makeAccessible(TOTAL);
}
public PageImplCustom() {
this(Collections.emptyList(), null, 0, null);
}
public PageImplCustom(List content, Pageable pageable, long total, UriComponentsBuilder builder) {
super(content, pageable, total);
this.direction = pageable instanceof OffsetRequest && ((OffsetRequest) pageable).hasDirection();
this.builder = builder;
}
@Override
public boolean hasNext() {
return this.direction ? true : super.hasNext();
}
@Override
public boolean hasPrevious() {
return this.direction ? true : super.hasPrevious();
}
@Override
public int getTotalPages() {
return this.direction ? 0 : super.getTotalPages();
}
public boolean isMore() {
return this.direction ? !(getNumberOfElements() == getTotalElements()) : (!isLast() && !isFirst());
}
public Map getLink() {
Object pageable = ReflectionUtils.getField(PAGEABLE, this);
boolean offset = pageable instanceof OffsetRequest;
String pageParameterName = offset ? ((OffsetRequest) pageable).getPageParameterName() : "page";
String sizeParameterName = offset ? ((OffsetRequest) pageable).getSizeParameterName() : "size";
String nextParameterName = offset ? ((OffsetRequest) pageable).getNextParameterName() : "next";
String prevParameterName = offset ? ((OffsetRequest) pageable).getPrevParameterName() : "prev";
if (this.builder != null) {
this.builder.scheme(null).host(null).port(null).replaceQueryParam(sizeParameterName, getSize());
if (this.direction) {
this.builder.replaceQueryParam(nextParameterName, "").replaceQueryParam(prevParameterName);
}
this.link.put(nextParameterName, this.builder.replaceQueryParam(pageParameterName, hasNext() ? nextPageable().getPageNumber() : getNumber()).build().toUriString());
if (this.direction) {
this.builder.replaceQueryParam(nextParameterName).replaceQueryParam(prevParameterName, "");
}
this.link.put(prevParameterName, this.builder.replaceQueryParam(pageParameterName, hasPrevious() ? previousPageable().getPageNumber() : getNumber()).build().toUriString());
}
return this.link;
}
@Override
public Page map(Converter converter) {
Object pageable = ReflectionUtils.getField(PAGEABLE, this);
return new PageImplCustom(getConvertedContent(converter), (pageable instanceof Pageable ? (Pageable) pageable : null), getTotalElements(), this.builder);
}
public Page page(Pageable pageable) {
return new PageImplCustom(getContent(), pageable, getTotalElements(), this.builder);
}
public void setTotalElements(long totalElements) {
ReflectionUtils.setField(TOTAL, this, totalElements);
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}