From ff46785b6783de2816e8d435b2d246a4a0cc4ff9 Mon Sep 17 00:00:00 2001 From: Wu Zengfu Date: Mon, 11 Nov 2024 18:09:11 +0800 Subject: [PATCH 1/4] Refactor Role.java --- src/main/java/seedu/address/model/contact/Role.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/main/java/seedu/address/model/contact/Role.java b/src/main/java/seedu/address/model/contact/Role.java index c38449c1a08..4ec5709261d 100644 --- a/src/main/java/seedu/address/model/contact/Role.java +++ b/src/main/java/seedu/address/model/contact/Role.java @@ -66,7 +66,7 @@ public Role(String roleName) { public static boolean isValidRoleName(String test) { requireNonNull(test); return Arrays.stream(AVAILABLE_ROLES) - .map(role -> role.toLowerCase()) + .map(String::toLowerCase) .anyMatch(role -> role.equals(test.trim().toLowerCase())); } @@ -82,13 +82,7 @@ private String toOfficialCase(String input) { } private int assignRoleIndex(String roleName) { - assert roleName.equals(PRESIDENT) // must be case sensitive - || roleName.equals(VICE_PRESIDENT) - || roleName.equals(ADMIN) - || roleName.equals(MARKETING) - || roleName.equals(EVENTS_INTERNAL) - || roleName.equals(EVENTS_EXTERNAL) - || roleName.equals(EXTERNAL_RELATIONS); + assert Arrays.asList(AVAILABLE_ROLES).contains(roleName); int errorStatus = -1; From e4b3871f41ed530194584a76f631e545771ab8a9 Mon Sep 17 00:00:00 2001 From: Wu Zengfu Date: Mon, 11 Nov 2024 18:41:50 +0800 Subject: [PATCH 2/4] Add assertions and logging in PaginationPanel.java --- .../java/seedu/address/ui/PaginationPanel.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/address/ui/PaginationPanel.java b/src/main/java/seedu/address/ui/PaginationPanel.java index 92a421a0923..f8420144853 100644 --- a/src/main/java/seedu/address/ui/PaginationPanel.java +++ b/src/main/java/seedu/address/ui/PaginationPanel.java @@ -1,12 +1,16 @@ package seedu.address.ui; +import static java.util.Objects.requireNonNull; + import java.util.List; +import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; import javafx.scene.Node; import javafx.scene.control.Pagination; +import seedu.address.commons.core.LogsCenter; import seedu.address.model.contact.Contact; /** @@ -15,6 +19,7 @@ public class PaginationPanel extends Pagination { public static final int ROWS_PER_PAGE = 10; private static int currentPageIndex = 0; + private static final Logger logger = LogsCenter.getLogger(PaginationPanel.class); private final ObservableList contactList; /** @@ -23,8 +28,9 @@ public class PaginationPanel extends Pagination { */ public PaginationPanel(ObservableList contactList) { super(); + requireNonNull(contactList); this.contactList = contactList; - this.contactList.addListener(this::onListItemsChanged); // enables UI update to ContactPagination + this.contactList.addListener(this::onListItemsChanged); this.initPagination(); } @@ -34,18 +40,21 @@ public PaginationPanel(ObservableList contactList) { * @return The index of the item in {@code contactList}. */ public static int convertItemIndexToDisplayIndex(int itemIndex) { - return (currentPageIndex) * ROWS_PER_PAGE + itemIndex + 1; + assert itemIndex >= 0; + return currentPageIndex * ROWS_PER_PAGE + itemIndex + 1; } private void initPagination() { final int minPageCount = 1; - int pageCount = Math.max((int) Math.ceil((double) contactList.size() / ROWS_PER_PAGE), minPageCount); + int expectedPageCount = (int) Math.ceil((double) contactList.size() / ROWS_PER_PAGE); + int pageCount = Math.max(expectedPageCount, minPageCount); this.setPageCount(pageCount); - this.setStyle("-fx-page-information-alignment: bottom;"); this.setPageFactory(this::createPage); + logger.info("A pagination which contains " + pageCount + " page(s) has been created."); } private Node createPage(int pageIndex) { + assert pageIndex >= 0; PaginationPanel.currentPageIndex = pageIndex; int fromIndex = pageIndex * PaginationPanel.ROWS_PER_PAGE; int endIndex = Math.min(fromIndex + PaginationPanel.ROWS_PER_PAGE, contactList.size()); @@ -59,5 +68,6 @@ private Node createPage(int pageIndex) { private void onListItemsChanged(ListChangeListener.Change unused) { this.initPagination(); + logger.info("Re-render pagination due to the change of list items."); } } From 2166447f4d3a2c59c2ba48ee8945b98a4a0a0a80 Mon Sep 17 00:00:00 2001 From: Wu Zengfu Date: Mon, 11 Nov 2024 19:22:58 +0800 Subject: [PATCH 3/4] Add test cases for PaginationPanel --- .../seedu/address/ui/PaginationPanelTest.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/test/java/seedu/address/ui/PaginationPanelTest.java diff --git a/src/test/java/seedu/address/ui/PaginationPanelTest.java b/src/test/java/seedu/address/ui/PaginationPanelTest.java new file mode 100644 index 00000000000..2b8887457fe --- /dev/null +++ b/src/test/java/seedu/address/ui/PaginationPanelTest.java @@ -0,0 +1,44 @@ +package seedu.address.ui; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.testutil.Assert.assertThrows; + +import java.util.ArrayList; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.embed.swing.JFXPanel; +import seedu.address.model.contact.Contact; + +public class PaginationPanelTest { + @BeforeAll + public static void initJavaFxPlatform() { + new JFXPanel(); + } + + @Test + public void constructor_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new PaginationPanel(null)); + } + + @Test + public void constructor_validObservationList_success() { + ObservableList observableList = FXCollections.observableList(new ArrayList<>()); + assertDoesNotThrow(() -> new PaginationPanel(observableList)); + } + + @Test + public void convertItemIndexToDisplayIndex_invalidIndex_throwsAssertionError() { + assertThrows(AssertionError.class, () -> PaginationPanel.convertItemIndexToDisplayIndex(-1)); + } + + @Test + public void convertItemIndexToDisplayIndex_validIndex_success() { + assertEquals(1, PaginationPanel.convertItemIndexToDisplayIndex(0)); + assertEquals(10, PaginationPanel.convertItemIndexToDisplayIndex(9)); + } +} From e4b5cf4a289c53866eae48b001e9e118a31758a9 Mon Sep 17 00:00:00 2001 From: Wu Zengfu Date: Mon, 11 Nov 2024 20:02:35 +0800 Subject: [PATCH 4/4] Delete test cases for PaginationPanelTest.java --- .../seedu/address/ui/PaginationPanelTest.java | 44 ------------------- 1 file changed, 44 deletions(-) delete mode 100644 src/test/java/seedu/address/ui/PaginationPanelTest.java diff --git a/src/test/java/seedu/address/ui/PaginationPanelTest.java b/src/test/java/seedu/address/ui/PaginationPanelTest.java deleted file mode 100644 index 2b8887457fe..00000000000 --- a/src/test/java/seedu/address/ui/PaginationPanelTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package seedu.address.ui; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static seedu.address.testutil.Assert.assertThrows; - -import java.util.ArrayList; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; -import javafx.embed.swing.JFXPanel; -import seedu.address.model.contact.Contact; - -public class PaginationPanelTest { - @BeforeAll - public static void initJavaFxPlatform() { - new JFXPanel(); - } - - @Test - public void constructor_null_throwsNullPointerException() { - assertThrows(NullPointerException.class, () -> new PaginationPanel(null)); - } - - @Test - public void constructor_validObservationList_success() { - ObservableList observableList = FXCollections.observableList(new ArrayList<>()); - assertDoesNotThrow(() -> new PaginationPanel(observableList)); - } - - @Test - public void convertItemIndexToDisplayIndex_invalidIndex_throwsAssertionError() { - assertThrows(AssertionError.class, () -> PaginationPanel.convertItemIndexToDisplayIndex(-1)); - } - - @Test - public void convertItemIndexToDisplayIndex_validIndex_success() { - assertEquals(1, PaginationPanel.convertItemIndexToDisplayIndex(0)); - assertEquals(10, PaginationPanel.convertItemIndexToDisplayIndex(9)); - } -}