diff --git a/src/main/java/utils/arrays/ArrayUtilities.java b/src/main/java/utils/arrays/ArrayUtilities.java index 9e4f061..18b1c18 100644 --- a/src/main/java/utils/arrays/ArrayUtilities.java +++ b/src/main/java/utils/arrays/ArrayUtilities.java @@ -1,5 +1,6 @@ package utils.arrays; +import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -40,5 +41,41 @@ public static ItemType getRandomItemFrom(List items) { public static boolean isLastMemberOf(List items, ItemType member){ return items.indexOf(member) == (items.size() - 1); } + + /** + * Splits a given list into partitions and returns a specific partition based on the partition index. + * + * This method uses stream filtering to find and return the elements that belong to the requested partition. + * The partition index is 0-based. + * + * @param The type of elements in the list. + * @param originalList The original list to be partitioned. + * @param partitionSize The size of each partition. + * @param partitionIndex The index of the partition to retrieve (0-based index). + * @return A list containing the elements from the specified partition. + * @throws IndexOutOfBoundsException If the partitionIndex is out of bounds (negative or too large). + */ + public static List getListPartition(List originalList, int partitionSize, int partitionIndex){ + int partitionBottomLimit = partitionSize * partitionIndex; + return originalList.stream().filter(item -> { + int itemIndex = originalList.indexOf(item); + return partitionBottomLimit <= itemIndex && itemIndex < partitionSize + partitionBottomLimit; + }).toList(); + } + + /** + * Calculates the total number of partitions needed to divide a list of a given size into partitions + * of the specified size. + * + * If there are leftover elements after dividing the list, an extra partition is counted. + * + * @param listSize The total number of elements in the list. + * @param partitionSize The size of each partition. + * @return The number of partitions required to divide the list. + */ + public static int getPartitionCount(int listSize, int partitionSize){ + int partitionCount = listSize / partitionSize; + return listSize % partitionCount == 0 ? partitionCount : partitionCount + 1; + } } diff --git a/src/test/java/AppTest.java b/src/test/java/AppTest.java index e57f124..68ef097 100644 --- a/src/test/java/AppTest.java +++ b/src/test/java/AppTest.java @@ -19,7 +19,7 @@ import java.util.List; import java.util.regex.Pattern; -import static utils.arrays.ArrayUtilities.isLastMemberOf; +import static utils.arrays.ArrayUtilities.*; import static utils.email.EmailUtilities.Inbox.EmailField.CONTENT; import static utils.email.EmailUtilities.Inbox.EmailField.SUBJECT; import static utils.MappingUtilities.Json.*; @@ -241,12 +241,39 @@ public void setFieldTest() { expectedFieldValue, pet.getName() ); - printer.success("The setFieldTest() test passed!"); + printer.success("The setFieldTest() test pass!"); } @Test public void lastItemOfTest() { List integers = List.of(1, 2, 3, 4, 5); Assert.assertTrue("Integer was not the last member!", isLastMemberOf(integers, 5)); + printer.success("The lastItemOfTest() test pass!"); + } + + @Test + public void partitionTest() { + List integers = List.of(1, 2, 3, 4, 5); + List> partitionLists = List.of( + List.of(1,2), + List.of(3,4), + List.of(5) + ); + for (int i = 0; i < getPartitionCount(integers.size(), 2); i++) { + List partition = getListPartition(integers, 2, i); + Assert.assertEquals(partition, partitionLists.get(i)); + } + printer.success("The partitionTest() test pass!"); + } + + @Test + public void partitionCountTest() { + List integers = List.of(1, 2, 3, 4, 5); + Assert.assertEquals( + "The getPartitionCount() method returns an incorrect value!", + 3, + getPartitionCount(integers.size(), 2) + ); + printer.success("The partitionCountTest() test pass!"); } }