From 290f5f9a896a6aa128c49aab2e5fd75224113d9f Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Tue, 3 Sep 2024 13:45:00 -0400 Subject: [PATCH] Make sure IterableUtils.duplicateList() has predicyable order in its result. - Add testDuplicateListMultipleDuplicatesInDequeReverse() - Add testDuplicateListMultipleDuplicatesInListReverse() - If you do not care about order, use IterableUtils.duplicateSet --- .../commons/collections4/IterableUtils.java | 2 +- .../collections4/IterableUtilsTest.java | 71 +++++++++---------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/IterableUtils.java b/src/main/java/org/apache/commons/collections4/IterableUtils.java index 0dc50453ae..b314474534 100644 --- a/src/main/java/org/apache/commons/collections4/IterableUtils.java +++ b/src/main/java/org/apache/commons/collections4/IterableUtils.java @@ -356,7 +356,7 @@ public static long countMatches(final Iterable input, final Predicate List duplicateList(final Iterable iterable) { - return new ArrayList<>(duplicateSet(iterable)); + return new ArrayList<>(duplicateSequencedSet(iterable)); } /** diff --git a/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java b/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java index b1de8d9185..b2b394bbeb 100644 --- a/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java @@ -140,8 +140,7 @@ public int hash(final String o) { assertFalse(IterableUtils.contains(base, "CX", secondLetterEquator)); assertFalse(IterableUtils.contains(null, null, secondLetterEquator)); - assertThrows(NullPointerException.class, () -> IterableUtils.contains(base, "AC", null), - "expecting NullPointerException"); + assertThrows(NullPointerException.class, () -> IterableUtils.contains(base, "AC", null), "expecting NullPointerException"); } @Test @@ -151,9 +150,7 @@ public void testCountMatches() { assertAll( () -> assertThrows(NullPointerException.class, () -> assertEquals(0, IterableUtils.countMatches(iterableA, null)), "predicate must not be null"), - () -> assertThrows(NullPointerException.class, () -> assertEquals(0, IterableUtils.countMatches(null, null)), - "predicate must not be null") - ); + () -> assertThrows(NullPointerException.class, () -> assertEquals(0, IterableUtils.countMatches(null, null)), "predicate must not be null")); } @Test @@ -185,6 +182,14 @@ public void testDuplicateListMultipleDuplicatesInDeque() { assertEquals(expected, IterableUtils.duplicateList(input)); } + @Test + public void testDuplicateListMultipleDuplicatesInDequeReverse() { + // We want to make sure that the actual list is in the expected order + final Deque input = new ArrayDeque<>(Arrays.asList(4, 4, 3, 3, 2, 2, 1, 1)); + final List expected = Arrays.asList(4, 3, 2, 1); + assertEquals(expected, IterableUtils.duplicateList(input)); + } + @Test public void testDuplicateListMultipleDuplicatesInList() { final List input = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4); @@ -192,6 +197,14 @@ public void testDuplicateListMultipleDuplicatesInList() { assertEquals(expected, IterableUtils.duplicateList(input)); } + @Test + public void testDuplicateListMultipleDuplicatesInListReverse() { + // We want to make sure that the actual list is in the expected order + final List input = Arrays.asList(4, 4, 3, 3, 2, 2, 1, 1); + final List expected = Arrays.asList(4, 3, 2, 1); + assertEquals(expected, IterableUtils.duplicateList(input)); + } + @Test public void testDuplicateListNoDuplicates() { final List input = Arrays.asList(1, 2, 3, 4, 5); @@ -297,8 +310,7 @@ public void testFind() { assertNull(test); assertNull(IterableUtils.find(null, testPredicate)); - assertThrows(NullPointerException.class, () -> IterableUtils.find(iterableA, null), - "expecting NullPointerException"); + assertThrows(NullPointerException.class, () -> IterableUtils.find(iterableA, null), "expecting NullPointerException"); } @Test @@ -324,8 +336,7 @@ public void testForEach() { IterableUtils.forEach(col, testClosure); assertTrue(listA.isEmpty() && listB.isEmpty()); - assertThrows(NullPointerException.class, () -> IterableUtils.forEach(col, null), - "expecting NullPointerException"); + assertThrows(NullPointerException.class, () -> IterableUtils.forEach(col, null), "expecting NullPointerException"); IterableUtils.forEach(null, testClosure); @@ -350,8 +361,7 @@ public void testForEachButLast() { assertTrue(listA.isEmpty() && !listB.isEmpty()); assertSame(listB, last); - assertThrows(NullPointerException.class, () -> IterableUtils.forEachButLast(col, null), - "expecting NullPointerException"); + assertThrows(NullPointerException.class, () -> IterableUtils.forEachButLast(col, null), "expecting NullPointerException"); IterableUtils.forEachButLast(null, testClosure); @@ -455,17 +465,14 @@ public void testIndexOf() { assertEquals(-1, index); assertEquals(-1, IterableUtils.indexOf(null, testPredicate)); - assertThrows(NullPointerException.class, () -> IterableUtils.indexOf(iterableA, null), - "expecting NullPointerException"); + assertThrows(NullPointerException.class, () -> IterableUtils.indexOf(iterableA, null), "expecting NullPointerException"); } @Test public void testMatchesAll() { - assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAll(null, null)), - "predicate must not be null"); + assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAll(null, null)), "predicate must not be null"); - assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAll(iterableA, null)), - "predicate must not be null"); + assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAll(iterableA, null)), "predicate must not be null"); final Predicate lessThanFive = object -> object < 5; assertTrue(IterableUtils.matchesAll(iterableA, lessThanFive)); @@ -481,11 +488,9 @@ public void testMatchesAll() { public void testMatchesAny() { final List list = new ArrayList<>(); - assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAny(null, null)), - "predicate must not be null"); + assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAny(null, null)), "predicate must not be null"); - assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAny(list, null)), - "predicate must not be null"); + assertThrows(NullPointerException.class, () -> assertFalse(IterableUtils.matchesAny(list, null)), "predicate must not be null"); assertFalse(IterableUtils.matchesAny(null, EQUALS_TWO)); assertFalse(IterableUtils.matchesAny(list, EQUALS_TWO)); @@ -515,7 +520,7 @@ public void testPartition() { assertEquals(2, CollectionUtils.extractSingleton(partition).intValue()); // second partition contains 1, 3, and 4 - final Integer[] expected = {1, 3, 4}; + final Integer[] expected = { 1, 3, 4 }; partition = partitions.get(1); assertArrayEquals(expected, partition.toArray()); @@ -528,8 +533,7 @@ public void testPartition() { assertEquals(1, partitions.size()); assertEquals(input, partitions.get(0)); - assertThrows(NullPointerException.class, () -> IterableUtils.partition(input, (Predicate) null), - "expecting NullPointerException"); + assertThrows(NullPointerException.class, () -> IterableUtils.partition(input, (Predicate) null), "expecting NullPointerException"); } @SuppressWarnings("unchecked") @@ -553,7 +557,7 @@ public void testPartitionMultiplePredicates() { assertEquals(4, partition.iterator().next().intValue()); // third partition contains 1 and 3 - final Integer[] expected = {1, 3}; + final Integer[] expected = { 1, 3 }; partition = partitions.get(2); assertArrayEquals(expected, partition.toArray()); @@ -635,23 +639,18 @@ public void testToStringWithNullArguments() { assertAll( () -> assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), null, StringUtils.EMPTY, "(", ")"), "expecting NullPointerException"), - () -> assertThrows(NullPointerException.class, () -> - IterableUtils.toString(new ArrayList<>(), input -> { - fail("not supposed to reach here"); - return StringUtils.EMPTY; - }, null, "(", ")"), - "expecting NullPointerException"), () -> assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), input -> { fail("not supposed to reach here"); return StringUtils.EMPTY; - }, StringUtils.EMPTY, null, ")"), - "expecting NullPointerException"), + }, null, "(", ")"), "expecting NullPointerException"), + () -> assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), input -> { + fail("not supposed to reach here"); + return StringUtils.EMPTY; + }, StringUtils.EMPTY, null, ")"), "expecting NullPointerException"), () -> assertThrows(NullPointerException.class, () -> IterableUtils.toString(new ArrayList<>(), input -> { fail("not supposed to reach here"); return StringUtils.EMPTY; - }, StringUtils.EMPTY, "(", null), - "expecting NullPointerException") - ); + }, StringUtils.EMPTY, "(", null), "expecting NullPointerException")); } }