Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/ca/nexapp/core/Pagination.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ca.nexapp.core;

import java.util.Objects;

public class Pagination {

private final int page;
Expand Down Expand Up @@ -37,4 +39,16 @@ public static Pagination offsetted(int offset, int itemPerPage) {
int page = offset / itemPerPage + 1;
return new Pagination(page, itemPerPage, offset);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Pagination that = (Pagination) o;
return page == that.page && itemPerPage == that.itemPerPage && offset == that.offset;
}

@Override
public int hashCode() {
return Objects.hash(page, itemPerPage, offset);
}
}
32 changes: 32 additions & 0 deletions src/test/java/ca/nexapp/core/PaginationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,36 @@ public void givenAnOffsettedPagination_CanRetrieveASubsetOfACollection() {

assertThat(results).containsExactly(1, 2, 3).inOrder();
}

@Test
public void givenTwoPaginationsWithDifferentPage_TheyAreNotEqual() {
Pagination a = Pagination.paged(1, 10);
Pagination b = Pagination.paged(2, 10);

assertThat(a).isNotEqualTo(b);
}

@Test
public void givenTwoPaginationsWithDifferentItemsPerPage_TheyAreNotEqual() {
Pagination a = Pagination.paged(1, 10);
Pagination b = Pagination.paged(1, 20);

assertThat(a).isNotEqualTo(b);
}

@Test
public void givenTwoPaginationsWithDifferentOffset_TheyAreNotEqual() {
Pagination a = Pagination.offsetted(0, 10);
Pagination b = Pagination.offsetted(5, 10);

assertThat(a).isNotEqualTo(b);
}

@Test
public void givenTwoPaginationsWithSamePageItemsPerPageAndOffset_TheyAreEqual() {
Pagination a = Pagination.offsetted(0, 10);
Pagination b = Pagination.offsetted(0, 10);

assertThat(a).isEqualTo(b);
}
}