Created
August 18, 2022 03:05
-
-
Save ywjno/0c9851cfc7a721d34a07545541eccc89 to your computer and use it in GitHub Desktop.
pagination util
This file contains 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
import java.util.Comparator; | |
import java.util.stream.IntStream; | |
public class PaginationUtil { | |
private final int pageNumber; | |
private final int blockSize; | |
private final int pageCount; | |
/** | |
* constructor | |
* | |
* @param totalCount total count | |
* @param pageNumber page number | |
* @param pageSize page size | |
* @param blockSize block size | |
*/ | |
public PaginationUtil(int totalCount, int pageNumber, int pageSize, int blockSize) { | |
// page number can not be minus | |
pageNumber = Math.max(pageNumber, 1); | |
this.pageCount = (int) Math.ceil(totalCount * 1.0 / pageSize); | |
this.pageNumber = Math.min(this.pageCount, pageNumber); | |
this.blockSize = Math.min(this.pageCount, blockSize); | |
} | |
public boolean isFirst() { | |
return this.pageNumber == 1; | |
} | |
public boolean isLast() { | |
return this.pageNumber == this.pageCount; | |
} | |
public boolean hasPrevious() { | |
return !isFirst(); | |
} | |
public boolean hasNext() { | |
return !isLast(); | |
} | |
public int previousNumber() { | |
return hasPrevious() ? this.pageNumber - 1 : this.pageNumber; | |
} | |
public int nextNumber() { | |
return hasNext() ? this.pageNumber + 1 : this.pageNumber; | |
} | |
public int[] getBlock() { | |
int currentIndex = isFirst() ? 0 : (isLast() ? this.blockSize - 1 : (this.blockSize - 1) / 2); | |
int[] block = new int[this.blockSize]; | |
block[currentIndex] = this.pageNumber; | |
IntStream.range(0, currentIndex) | |
.boxed() | |
.sorted(Comparator.reverseOrder()) | |
.forEach(i -> block[i] = block[i + 1] - 1); | |
IntStream.range(currentIndex + 1, this.blockSize).forEach(i -> block[i] = block[i - 1] + 1); | |
return block; | |
} | |
public int getPageNumber() { | |
return pageNumber; | |
} | |
public int getPageCount() { | |
return pageCount; | |
} | |
} |
This file contains 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
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import static org.junit.jupiter.api.Assertions.assertFalse; | |
import static org.junit.jupiter.api.Assertions.assertTrue; | |
import org.junit.jupiter.api.Test; | |
public class PaginationUtilTest { | |
@Test | |
public void testPagination() { | |
PaginationUtil pager = new PaginationUtil(100, 4, 2, 5); | |
assertArrayEquals(new int[]{2, 3, 4, 5, 6}, pager.getBlock()); | |
assertEquals(4, pager.getPageNumber()); | |
assertEquals(50, pager.getPageCount()); | |
assertEquals(3, pager.previousNumber()); | |
assertEquals(5, pager.nextNumber()); | |
pager = new PaginationUtil(8, 2, 2, 3); | |
assertArrayEquals(new int[]{1, 2, 3}, pager.getBlock()); | |
pager = new PaginationUtil(8, 3, 2, 3); | |
assertArrayEquals(new int[]{2, 3, 4}, pager.getBlock()); | |
pager = new PaginationUtil(8, 4, 2, 3); | |
assertArrayEquals(new int[]{2, 3, 4}, pager.getBlock()); | |
pager = new PaginationUtil(8, 4, 200, 3); | |
assertArrayEquals(new int[]{1}, pager.getBlock()); | |
} | |
@Test | |
public void testFirstBlock() { | |
PaginationUtil pager = new PaginationUtil(8, 1, 2, 1); | |
assertArrayEquals(new int[]{1}, pager.getBlock()); | |
assertTrue(pager.isFirst()); | |
assertFalse(pager.isLast()); | |
assertFalse(pager.hasPrevious()); | |
assertTrue(pager.hasNext()); | |
assertEquals(1, pager.getPageNumber()); | |
pager = new PaginationUtil(8, 1, 2, 2); | |
assertArrayEquals(new int[]{1, 2}, pager.getBlock()); | |
pager = new PaginationUtil(8, 1, 2, 3); | |
assertArrayEquals(new int[]{1, 2, 3}, pager.getBlock()); | |
} | |
@Test | |
public void testLastBlock() { | |
PaginationUtil pager = new PaginationUtil(10, 5, 2, 1); | |
assertArrayEquals(new int[]{5}, pager.getBlock()); | |
pager = new PaginationUtil(10, 5, 2, 2); | |
assertArrayEquals(new int[]{4, 5}, pager.getBlock()); | |
pager = new PaginationUtil(10, 5, 2, 3); | |
assertArrayEquals(new int[]{3, 4, 5}, pager.getBlock()); | |
pager = new PaginationUtil(10, 5, 2, 4); | |
assertArrayEquals(new int[]{2, 3, 4, 5}, pager.getBlock()); | |
pager = new PaginationUtil(10, 5, 2, 5); | |
assertArrayEquals(new int[]{1, 2, 3, 4, 5}, pager.getBlock()); | |
assertFalse(pager.isFirst()); | |
assertTrue(pager.isLast()); | |
assertTrue(pager.hasPrevious()); | |
assertFalse(pager.hasNext()); | |
assertEquals(5, pager.getPageNumber()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment