Skip to content

Commit

Permalink
Merge pull request #208 from bobaikato/Enhance-Partition-list
Browse files Browse the repository at this point in the history
Enhance partition list
  • Loading branch information
bobaikato authored Oct 8, 2023
2 parents 9a9bedc + 44ed634 commit c091e30
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/main/java/art/cutils/collection/ListPartition.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.Validate;
Expand Down Expand Up @@ -78,6 +79,20 @@ private ListPartition(final List<? extends T> list) {
return new ListPartition<>(list);
}

/**
* Method to receive the array to be partitioned.
*
* @param array Array to be partitioned.
* @param <T> Array type
* @return instance of {@link ListPartition}
*/
@SafeVarargs
@Contract("_ -> new")
public static <T> @NotNull ListPartition<T> of(final T... array) {
Objects.requireNonNull(array, "List cannot be null");
return new ListPartition<>(Arrays.asList(array));
}

/**
* Method used to get partition size.
*
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/collection/ListPartitionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import art.cutils.collection.ListPartition;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import art.cutils.collection.ListPartition;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -135,6 +135,19 @@ void equalsAndHashCodeContractToBeValid() {
assertEquals(p1.hashCode(), p2.hashCode());
}

@Test
void test_Overridden_Partition_Of_Arrays() {
final ListPartition<String> p1 = of(new String[] {"A", "B", "C", "D", "E", "F"}).into(2);
final ListPartition<String> p2 = of("A", "B", "C", "D", "E", "F").into(2);

assertEquals(p1, p2);
assertEquals(p1, p1);
assertEquals(p2, p2);

// Hashcode
assertEquals(p1.hashCode(), p2.hashCode());
}

@Test
void equalsAndHashCodeContractToBeInvalid() {
final ListPartition<?> p1 = of(asList("A", "B", "C", "D", "E")).into(3);
Expand All @@ -144,4 +157,4 @@ void equalsAndHashCodeContractToBeInvalid() {
assertNotEquals(p1, p2.get(0));
assertNotEquals(p1.hashCode(), p2.hashCode());
}
}
}

0 comments on commit c091e30

Please sign in to comment.