diff --git a/java/inner-class/custom-iterable/src/main/java/it/unibo/inner/TestIterableWithPolicy.java b/java/inner-class/custom-iterable/src/main/java/it/unibo/inner/TestIterableWithPolicy.java index 3f65bd35..8312aeab 100644 --- a/java/inner-class/custom-iterable/src/main/java/it/unibo/inner/TestIterableWithPolicy.java +++ b/java/inner-class/custom-iterable/src/main/java/it/unibo/inner/TestIterableWithPolicy.java @@ -15,6 +15,14 @@ public class TestIterableWithPolicy { private TestIterableWithPolicy() {} + private static IterableWithPolicy getIterableWithPolicy(T[] elements, Predicate filter) { + return new IterableWithPolicyImpl<>(elements, filter); + } + + private static IterableWithPolicy getIterableWithPolicy(T[] elements) { + return new IterableWithPolicyImpl<>(elements); + } + public static void main(String[] args) { Integer[] test1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Predicate filterEven = new Predicate() { @@ -28,8 +36,8 @@ public boolean test(Integer elem) { } }; - IterableWithPolicy evenIterable = new IterableWithPolicyImpl<>(test1, filterEven); - IterableWithPolicy oddIterable = new IterableWithPolicyImpl<>(test1, filterOdd); + IterableWithPolicy evenIterable = getIterableWithPolicy(test1, filterEven); + IterableWithPolicy oddIterable = getIterableWithPolicy(test1, filterOdd); assertContentEqualsInOrder(evenIterable, Arrays.asList(2, 4, 6, 8)); assertContentEqualsInOrder(oddIterable, Arrays.asList(1, 3, 5, 7, 9)); @@ -45,13 +53,13 @@ public boolean test(Integer elem) { } }; - IterableWithPolicy emptyIterable = new IterableWithPolicyImpl<>(test1, filterOutAll); - IterableWithPolicy allIterable = new IterableWithPolicyImpl<>(test1, takeAll); + IterableWithPolicy emptyIterable = getIterableWithPolicy(test1, filterOutAll); + IterableWithPolicy allIterable = getIterableWithPolicy(test1, takeAll); assertContentEqualsInOrder(emptyIterable, new ArrayList<>()); assertContentEqualsInOrder(allIterable, Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)); - IterableWithPolicy switchPolicy = new IterableWithPolicyImpl<>(test1); + IterableWithPolicy switchPolicy = getIterableWithPolicy(test1); // By default, if no Predicate is given, the iterator should return all the elements assertContentEqualsInOrder(switchPolicy, Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)); @@ -87,13 +95,13 @@ public boolean test(Product elem) { } }; - IterableWithPolicy availableProducts = new IterableWithPolicyImpl<>(productsTest, filterAllAvailable); + IterableWithPolicy availableProducts = getIterableWithPolicy(productsTest, filterAllAvailable); assertContentEqualsInOrder(availableProducts, Arrays.asList(prod2, prod3, prod4, prod5)); - IterableWithPolicy expensiveProducts = new IterableWithPolicyImpl<>(productsTest, filterGraterThanFifty); + IterableWithPolicy expensiveProducts = getIterableWithPolicy(productsTest, filterGraterThanFifty); assertContentEqualsInOrder(expensiveProducts, Arrays.asList(prod2, prod5)); - IterableWithPolicy onlyProductOne = new IterableWithPolicyImpl<>(productsTest, takeOnlyProductOne); + IterableWithPolicy onlyProductOne = getIterableWithPolicy(productsTest, takeOnlyProductOne); assertContentEqualsInOrder(onlyProductOne, Arrays.asList(prod1)); } } diff --git a/java/inner-class/custom-iterable/src/main/java/it/unibo/inner/test/Assertions.java b/java/inner-class/custom-iterable/src/main/java/it/unibo/inner/test/Assertions.java index 6128c1a6..80cbc5af 100644 --- a/java/inner-class/custom-iterable/src/main/java/it/unibo/inner/test/Assertions.java +++ b/java/inner-class/custom-iterable/src/main/java/it/unibo/inner/test/Assertions.java @@ -28,34 +28,6 @@ public static void assertTrue(final boolean condition) { } } - /** - * Exits with an error if the two collections do not contain the same elements (except for the order). - * - * @param expected the expected collection - * @param actual the actual collection - */ - public static void assertContentEqualsInAnyOrder(final Collection expected, final Collection actual) { - if (checkContentEqualsInAnyOrder(expected, actual)) { - confirmOK(expected, actual); - } else { - onNotEquals(expected, actual); - } - } - - private static boolean checkContentEqualsInAnyOrder(final Collection expected, final Collection actual) { - Objects.requireNonNull(expected); - if (actual == null || expected.size() != actual.size()) { - return false; - } - final Collection expectedCopy = new ArrayList<>(expected); - for (final var elem : actual) { - if (!expectedCopy.remove(elem)) { - return false; - } - } - return expectedCopy.isEmpty(); - } - /** * Exits with an error if the two collections do not contain the same elements (except for the order). *