package org.ccframe.commons.base; import java.io.Serializable; import org.apache.commons.lang3.builder.EqualsBuilder; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; public class OffsetBasedPageRequest implements Pageable, Serializable { /** * */ private static final long serialVersionUID = 810579142489422257L; private int limit; private long offset; private final Sort sort; public OffsetBasedPageRequest(long offset, int limit, Order... orders) { this(offset, limit, Sort.by(orders)); } /** * Creates a new {@link OffsetBasedPageRequest} with sort parameters applied. * * @param offset zero-based offset. * @param limit the size of the elements to be returned. * @param sort can be {@literal null}. */ public OffsetBasedPageRequest(long offset, int limit, Sort sort) { if (offset < 0) { throw new IllegalArgumentException("Offset index must not be less than zero!"); } if (limit < 1) { throw new IllegalArgumentException("Limit must not be less than one!"); } this.limit = limit; this.offset = offset; this.sort = sort; } /** * Creates a new {@link OffsetBasedPageRequest} with sort parameters applied. * * @param offset zero-based offset. * @param limit the size of the elements to be returned. * @param direction the direction of the {@link Sort} to be specified, can be {@literal null}. * @param properties the properties to sort by, must not be {@literal null} or empty. */ public OffsetBasedPageRequest(int offset, int limit, Sort.Direction direction, String... properties) { this(offset, limit, Sort.by(direction, properties)); } @Override public int getPageNumber() { return (int)(offset / limit); } @Override public int getPageSize() { return limit; } @Override public long getOffset() { return offset; } @Override public Sort getSort() { return sort; } @Override public Pageable next() { return new OffsetBasedPageRequest(getOffset() + getPageSize(), getPageSize(), getSort()); } public OffsetBasedPageRequest previous() { return hasPrevious() ? new OffsetBasedPageRequest(getOffset() - getPageSize(), getPageSize(), getSort()) : this; } @Override public Pageable previousOrFirst() { return hasPrevious() ? previous() : first(); } @Override public Pageable first() { return new OffsetBasedPageRequest(0, getPageSize(), getSort()); } @Override public boolean hasPrevious() { return offset > limit; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof OffsetBasedPageRequest)) return false; OffsetBasedPageRequest that = (OffsetBasedPageRequest) o; return new EqualsBuilder() .append(limit, that.limit) .append(offset, that.offset) .append(sort, that.sort) .isEquals(); } }