This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push: new 35a6778 COLLECTIONS-788 - java8 improvements: (#228) 35a6778 is described below commit 35a6778db8b8b2abab7e41a5b11098df1405de15 Author: Arturo Bernal <arturobern...@gmail.com> AuthorDate: Fri Mar 4 15:07:07 2022 +0100 COLLECTIONS-788 - java8 improvements: (#228) * Replace Collections.sort() with List.sort() * Replace lambda with method reference * Replace Loop with Collection.removeIf() * Use lambda Co-authored-by: Gary Gregory <garydgreg...@users.noreply.github.com> --- .../commons/collections4/FluentIterableTest.java | 2 +- .../apache/commons/collections4/MapUtilsTest.java | 20 +++++--------------- .../commons/collections4/SplitMapUtilsTest.java | 12 ++++++------ .../collection/AbstractCollectionTest.java | 2 +- .../apache/commons/collections4/map/LRUMapTest.java | 13 ++----------- .../commons/collections4/map/ReferenceMapTest.java | 2 +- 6 files changed, 16 insertions(+), 35 deletions(-) diff --git a/src/test/java/org/apache/commons/collections4/FluentIterableTest.java b/src/test/java/org/apache/commons/collections4/FluentIterableTest.java index 492044a..b2aada8 100644 --- a/src/test/java/org/apache/commons/collections4/FluentIterableTest.java +++ b/src/test/java/org/apache/commons/collections4/FluentIterableTest.java @@ -213,7 +213,7 @@ public class FluentIterableTest { @Test public void forEach() { final AtomicInteger sum = new AtomicInteger(0); - final Closure<Integer> closure = input -> sum.addAndGet(input); + final Closure<Integer> closure = sum::addAndGet; FluentIterable.of(iterableA).forEach(closure); int expectedSum = 0; diff --git a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java index 6696d98..ba3f5ad 100644 --- a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java @@ -167,9 +167,7 @@ public class MapUtilsTest { @Test public void testInvertMapNull() { final Map<String, String> nullMap = null; - final Exception exception = assertThrows(NullPointerException.class, () -> { - MapUtils.invertMap(nullMap); - }); + final Exception exception = assertThrows(NullPointerException.class, () -> MapUtils.invertMap(nullMap)); final String actualMessage = exception.getMessage(); assertTrue(actualMessage.contains("map")); } @@ -1015,30 +1013,22 @@ public class MapUtilsTest { @Test public void testUnmodifiableMap() { - final Exception exception = assertThrows(UnsupportedOperationException.class, () -> { - MapUtils.unmodifiableMap(new HashMap<>()).clear(); - }); + final Exception exception = assertThrows(UnsupportedOperationException.class, () -> MapUtils.unmodifiableMap(new HashMap<>()).clear()); } @Test public void testUnmodifiableSortedMap() { - final Exception exception = assertThrows(UnsupportedOperationException.class, () -> { - MapUtils.unmodifiableSortedMap(new TreeMap<>()).clear(); - }); + final Exception exception = assertThrows(UnsupportedOperationException.class, () -> MapUtils.unmodifiableSortedMap(new TreeMap<>()).clear()); } @Test public void testFixedSizeMap() { - final Exception exception = assertThrows(IllegalArgumentException.class, () -> { - MapUtils.fixedSizeMap(new HashMap<>()).put(new Object(), new Object()); - }); + final Exception exception = assertThrows(IllegalArgumentException.class, () -> MapUtils.fixedSizeMap(new HashMap<>()).put(new Object(), new Object())); } @Test public void testFixedSizeSortedMap() { - final Exception exception = assertThrows(IllegalArgumentException.class, () -> { - MapUtils.fixedSizeSortedMap(new TreeMap<Long, Long>()).put(1L, 1L); - }); + final Exception exception = assertThrows(IllegalArgumentException.class, () -> MapUtils.fixedSizeSortedMap(new TreeMap<Long, Long>()).put(1L, 1L)); } @Test diff --git a/src/test/java/org/apache/commons/collections4/SplitMapUtilsTest.java b/src/test/java/org/apache/commons/collections4/SplitMapUtilsTest.java index 0ace69d..b5d3719 100644 --- a/src/test/java/org/apache/commons/collections4/SplitMapUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/SplitMapUtilsTest.java @@ -79,7 +79,7 @@ public class SplitMapUtilsTest { // check individual operations int sz = map.size(); - attemptPutOperation(() -> map.clear()); + attemptPutOperation(map::clear); assertEquals(sz, map.size()); @@ -116,11 +116,11 @@ public class SplitMapUtilsTest { public void testWritableMap() { final Map<String, String> map = SplitMapUtils.writableMap(transformedMap); attemptGetOperation(() -> map.get(null)); - attemptGetOperation(() -> map.entrySet()); - attemptGetOperation(() -> map.keySet()); - attemptGetOperation(() -> map.values()); - attemptGetOperation(() -> map.size()); - attemptGetOperation(() -> map.isEmpty()); + attemptGetOperation(map::entrySet); + attemptGetOperation(map::keySet); + attemptGetOperation(map::values); + attemptGetOperation(map::size); + attemptGetOperation(map::isEmpty); attemptGetOperation(() -> map.containsKey(null)); attemptGetOperation(() -> map.containsValue(null)); attemptGetOperation(() -> map.remove(null)); diff --git a/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java b/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java index 60d6370..62c036e 100644 --- a/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java +++ b/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java @@ -963,7 +963,7 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest { final int size = getCollection().size(); final int targetCount = Collections.frequency(elements, target); - final Predicate<E> filter = e -> target.equals(e); + final Predicate<E> filter = target::equals; assertTrue("Full collection removeIf should work", getCollection().removeIf(filter)); getConfirmed().removeIf(filter); diff --git a/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java b/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java index bb918f0..374b039 100644 --- a/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java @@ -700,12 +700,7 @@ public class LRUMapTest<K, V> extends AbstractOrderedMapTest<K, V> { } } synchronized (map) { - for (final Iterator<Map.Entry<Object, Thread>> iter = map.entrySet().iterator(); iter.hasNext();) { - final Map.Entry<Object, Thread> entry = iter.next(); - if (entry.getValue() == this) { - iter.remove(); - } - } + map.entrySet().removeIf(entry -> entry.getValue() == this); } } catch (final InterruptedException e) { fail("Unexpected InterruptedException"); @@ -866,11 +861,7 @@ public class LRUMapTest<K, V> extends AbstractOrderedMapTest<K, V> { } } synchronized (map) { - for (final Iterator<Thread> iter = map.values().iterator(); iter.hasNext();) { - if (iter.next() == this) { - iter.remove(); - } - } + map.values().removeIf(thread1 -> thread1 == this); } } catch (final InterruptedException e) { fail("Unexpected InterruptedException"); diff --git a/src/test/java/org/apache/commons/collections4/map/ReferenceMapTest.java b/src/test/java/org/apache/commons/collections4/map/ReferenceMapTest.java index 064516e..3412102 100644 --- a/src/test/java/org/apache/commons/collections4/map/ReferenceMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/ReferenceMapTest.java @@ -261,7 +261,7 @@ public class ReferenceMapTest<K, V> extends AbstractIterableMapTest<K, V> { public void testCustomPurge() { final List<Integer> expiredValues = new ArrayList<>(); @SuppressWarnings("unchecked") - final Consumer<Integer> consumer = (Consumer<Integer> & Serializable) v -> expiredValues.add(v); + final Consumer<Integer> consumer = (Consumer<Integer> & Serializable) expiredValues::add; final Map<Integer, Integer> map = new ReferenceMap<Integer, Integer>(ReferenceStrength.WEAK, ReferenceStrength.HARD, false) { private static final long serialVersionUID = 1L;