OffsetBasedPageRequest.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package org.ccframe.commons.base;
  2. import java.io.Serializable;
  3. import org.apache.commons.lang3.builder.EqualsBuilder;
  4. import org.springframework.data.domain.Pageable;
  5. import org.springframework.data.domain.Sort;
  6. import org.springframework.data.domain.Sort.Order;
  7. public class OffsetBasedPageRequest implements Pageable, Serializable {
  8. /**
  9. *
  10. */
  11. private static final long serialVersionUID = 810579142489422257L;
  12. private int limit;
  13. private long offset;
  14. private final Sort sort;
  15. public OffsetBasedPageRequest(long offset, int limit, Order... orders) {
  16. this(offset, limit, Sort.by(orders));
  17. }
  18. /**
  19. * Creates a new {@link OffsetBasedPageRequest} with sort parameters applied.
  20. *
  21. * @param offset zero-based offset.
  22. * @param limit the size of the elements to be returned.
  23. * @param sort can be {@literal null}.
  24. */
  25. public OffsetBasedPageRequest(long offset, int limit, Sort sort) {
  26. if (offset < 0) {
  27. throw new IllegalArgumentException("Offset index must not be less than zero!");
  28. }
  29. if (limit < 1) {
  30. throw new IllegalArgumentException("Limit must not be less than one!");
  31. }
  32. this.limit = limit;
  33. this.offset = offset;
  34. this.sort = sort;
  35. }
  36. /**
  37. * Creates a new {@link OffsetBasedPageRequest} with sort parameters applied.
  38. *
  39. * @param offset zero-based offset.
  40. * @param limit the size of the elements to be returned.
  41. * @param direction the direction of the {@link Sort} to be specified, can be {@literal null}.
  42. * @param properties the properties to sort by, must not be {@literal null} or empty.
  43. */
  44. public OffsetBasedPageRequest(int offset, int limit, Sort.Direction direction, String... properties) {
  45. this(offset, limit, Sort.by(direction, properties));
  46. }
  47. @Override
  48. public int getPageNumber() {
  49. return (int)(offset / limit);
  50. }
  51. @Override
  52. public int getPageSize() {
  53. return limit;
  54. }
  55. @Override
  56. public long getOffset() {
  57. return offset;
  58. }
  59. @Override
  60. public Sort getSort() {
  61. return sort;
  62. }
  63. @Override
  64. public Pageable next() {
  65. return new OffsetBasedPageRequest(getOffset() + getPageSize(), getPageSize(), getSort());
  66. }
  67. public OffsetBasedPageRequest previous() {
  68. return hasPrevious() ? new OffsetBasedPageRequest(getOffset() - getPageSize(), getPageSize(), getSort()) : this;
  69. }
  70. @Override
  71. public Pageable previousOrFirst() {
  72. return hasPrevious() ? previous() : first();
  73. }
  74. @Override
  75. public Pageable first() {
  76. return new OffsetBasedPageRequest(0, getPageSize(), getSort());
  77. }
  78. @Override
  79. public boolean hasPrevious() {
  80. return offset > limit;
  81. }
  82. @Override
  83. public boolean equals(Object o) {
  84. if (this == o) return true;
  85. if (!(o instanceof OffsetBasedPageRequest)) return false;
  86. OffsetBasedPageRequest that = (OffsetBasedPageRequest) o;
  87. return new EqualsBuilder()
  88. .append(limit, that.limit)
  89. .append(offset, that.offset)
  90. .append(sort, that.sort)
  91. .isEquals();
  92. }
  93. }