From 8f7eb6ce207f354c83157b95bdf9848d59944650 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 11 Jun 2020 09:55:02 -0400 Subject: [PATCH] Simplify lambads. Fix Javadoc. --- .../org/apache/commons/lang3/Functions.java | 9 +++----- .../apache/commons/lang3/FunctionsTest.java | 4 ++-- .../org/apache/commons/lang3/LocksTest.java | 6 ++--- .../org/apache/commons/lang3/StreamsTest.java | 22 +++++++++---------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/Functions.java b/src/main/java/org/apache/commons/lang3/Functions.java index 896c93a29..9bbffa7da 100644 --- a/src/main/java/org/apache/commons/lang3/Functions.java +++ b/src/main/java/org/apache/commons/lang3/Functions.java @@ -40,8 +40,7 @@ import org.apache.commons.lang3.Streams.FailableStream; * constructs like: * *
- * {
- *     @code
+ * {@code
  *     Consumer consumer = (m) -> {
  *         try {
  *             m.invoke(o, args);
@@ -504,8 +503,7 @@ public class Functions {
      * {@link Throwable} is rethrown. Example use:
      *
      * 
-     * {
-     *     @code
+     * {@code
      *     final FileInputStream fis = new FileInputStream("my.file");
      *     Functions.tryWithResources(useInputStream(fis), null, () -> fis.close());
      * }
@@ -566,8 +564,7 @@ public class Functions { * {@link Throwable} is rethrown. Example use: * *
-     * {
-     *     @code
+     * {@code
      *     final FileInputStream fis = new FileInputStream("my.file");
      *     Functions.tryWithResources(useInputStream(fis), () -> fis.close());
      * }
diff --git a/src/test/java/org/apache/commons/lang3/FunctionsTest.java b/src/test/java/org/apache/commons/lang3/FunctionsTest.java index 50c3e1847..4180829f4 100644 --- a/src/test/java/org/apache/commons/lang3/FunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/FunctionsTest.java @@ -178,7 +178,7 @@ class FunctionsTest { @Test void testAsCallable() { FailureOnOddInvocations.invocation = 0; - final FailableCallable failableCallable = () -> new FailureOnOddInvocations(); + final FailableCallable failableCallable = FailureOnOddInvocations::new; final Callable callable = Functions.asCallable(failableCallable); final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> callable.call()); final Throwable cause = e.getCause(); @@ -221,7 +221,7 @@ class FunctionsTest { void testAsConsumer() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable(ise); - final Consumer consumer = Functions.asConsumer(t -> t.test()); + final Consumer consumer = Functions.asConsumer(Testable::test); Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable)); assertSame(ise, e); diff --git a/src/test/java/org/apache/commons/lang3/LocksTest.java b/src/test/java/org/apache/commons/lang3/LocksTest.java index 5b0801395..18a81d9db 100644 --- a/src/test/java/org/apache/commons/lang3/LocksTest.java +++ b/src/test/java/org/apache/commons/lang3/LocksTest.java @@ -33,7 +33,7 @@ class LocksTest { /** If our threads are running concurrently, then we expect to be faster * than running one after the other. */ - runTest(DELAY, false, (l) -> assertTrue(l < NUMBER_OF_THREADS*DELAY)); + runTest(DELAY, false, l -> assertTrue(l < NUMBER_OF_THREADS*DELAY)); } void testWriteLock() throws Exception { @@ -41,7 +41,7 @@ class LocksTest { /** If our threads are running concurrently, then we expect to be no faster * than running one after the other. */ - runTest(DELAY, true, (l) -> assertTrue(l >= NUMBER_OF_THREADS*DELAY)); + runTest(DELAY, true, l -> assertTrue(l >= NUMBER_OF_THREADS*DELAY)); } private void runTest(long delay, boolean exclusiveLock, LongConsumer runTimeCheck) throws InterruptedException { @@ -52,7 +52,7 @@ class LocksTest { final long startTime = System.currentTimeMillis(); for (int i = 0; i < booleanValues.length; i++) { final int index = i; - final FailableConsumer consumer = (b) -> { + final FailableConsumer consumer = b -> { b[index] = false; Thread.sleep(delay); b[index] = true; diff --git a/src/test/java/org/apache/commons/lang3/StreamsTest.java b/src/test/java/org/apache/commons/lang3/StreamsTest.java index a5985aad3..3e17ed1bd 100644 --- a/src/test/java/org/apache/commons/lang3/StreamsTest.java +++ b/src/test/java/org/apache/commons/lang3/StreamsTest.java @@ -46,7 +46,7 @@ class StreamsTest { @Test void testSimpleStreamMap() { final List input = Arrays.asList("1", "2", "3", "4", "5", "6"); - final List output = Functions.stream(input).map((s) -> Integer.valueOf(s)).collect(Collectors.toList()); + final List output = Functions.stream(input).map(s -> Integer.valueOf(s)).collect(Collectors.toList()); assertEquals(6, output.size()); for (int i = 0; i < 6; i++) { assertEquals(i+1, output.get(i).intValue()); @@ -56,7 +56,7 @@ class StreamsTest { @Test void testSimpleStreamMapFailing() { final List input = Arrays.asList("1", "2", "3", "4 ", "5", "6"); - final Executable testMethod = () -> Functions.stream(input).map((s) -> Integer.valueOf(s)).collect(Collectors.toList()); + final Executable testMethod = () -> Functions.stream(input).map(s -> Integer.valueOf(s)).collect(Collectors.toList()); final NumberFormatException thrown = assertThrows(NumberFormatException.class, testMethod); assertEquals("For input string: \"4 \"", thrown.getMessage()); } @@ -65,7 +65,7 @@ class StreamsTest { void testSimpleStreamForEach() { final List input = Arrays.asList("1", "2", "3", "4", "5", "6"); final List output = new ArrayList<>(); - Functions.stream(input).forEach((s) -> output.add(Integer.valueOf(s))); + Functions.stream(input).forEach(s -> output.add(Integer.valueOf(s))); assertEquals(6, output.size()); for (int i = 0; i < 6; i++) { assertEquals(i+1, output.get(i).intValue()); @@ -83,7 +83,7 @@ class StreamsTest { } protected FailableConsumer asIntConsumer(final T pThrowable) { - return (s) -> { + return s -> { final Integer i = Integer.valueOf(s); if (i.intValue() == 4) { throw pThrowable; @@ -130,8 +130,8 @@ class StreamsTest { void testSimpleStreamFilter() { final List input = Arrays.asList("1", "2", "3", "4", "5", "6"); final List output = Functions.stream(input) - .map((s) -> Integer.valueOf(s)) - .filter((i) -> { + .map(s -> Integer.valueOf(s)) + .filter(i -> { return i.intValue() %2 == 0; }) .collect(Collectors.toList()); @@ -146,7 +146,7 @@ class StreamsTest { } protected FailablePredicate asIntPredicate(final T pThrowable) { - return (i) -> { + return i -> { if (i.intValue() == 5) { if (pThrowable != null) { throw pThrowable; @@ -160,7 +160,7 @@ class StreamsTest { Stream simpleStreamFilterFailing() { final List input = Arrays.asList("1", "2", "3", "4", "5", "6"); final List output = Functions.stream(input) - .map((s) -> Integer.valueOf(s)) + .map(s -> Integer.valueOf(s)) .filter(asIntPredicate(null)) .collect(Collectors.toList()); assertEvenNumbers(output); @@ -170,7 +170,7 @@ class StreamsTest { dynamicTest("IllegalArgumentException", () -> { final IllegalArgumentException iae = new IllegalArgumentException("Invalid argument: " + 5); final Executable testMethod = () -> Functions.stream(input) - .map((s) -> Integer.valueOf(s)) + .map(s -> Integer.valueOf(s)) .filter(asIntPredicate(iae)) .collect(Collectors.toList()); final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod); @@ -180,7 +180,7 @@ class StreamsTest { dynamicTest("OutOfMemoryError", () -> { final OutOfMemoryError oome = new OutOfMemoryError(); final Executable testMethod = () -> Functions.stream(input) - .map((s) -> Integer.valueOf(s)) + .map(s -> Integer.valueOf(s)) .filter(asIntPredicate(oome)) .collect(Collectors.toList()); final OutOfMemoryError thrown = assertThrows(OutOfMemoryError.class, testMethod); @@ -190,7 +190,7 @@ class StreamsTest { dynamicTest("SAXException", () -> { final SAXException se = new SAXException(); final Executable testMethod = () -> Functions.stream(input) - .map((s) -> Integer.valueOf(s)) + .map(s -> Integer.valueOf(s)) .filter(asIntPredicate(se)) .collect(Collectors.toList()); final UndeclaredThrowableException thrown = assertThrows(UndeclaredThrowableException.class, testMethod);