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
37 changes: 37 additions & 0 deletions src/main/java/utils/arrays/ArrayUtilities.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package utils.arrays;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

Expand Down Expand Up @@ -40,5 +41,41 @@ public static <ItemType> ItemType getRandomItemFrom(List<ItemType> items) {
public static <ItemType> boolean isLastMemberOf(List<ItemType> 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 <T> 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 <T> List<T> getListPartition(List<T> 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;
}
}

31 changes: 29 additions & 2 deletions src/test/java/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<Integer> 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<Integer> integers = List.of(1, 2, 3, 4, 5);
List<List<Integer>> 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<Integer> partition = getListPartition(integers, 2, i);
Assert.assertEquals(partition, partitionLists.get(i));
}
printer.success("The partitionTest() test pass!");
}

@Test
public void partitionCountTest() {
List<Integer> 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!");
}
}