You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/08/30 13:20:25 UTC

[commons-lang] branch master updated: Refactorings; reuse UncheckedFuture.

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-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 34a85e7  Refactorings; reuse UncheckedFuture.
34a85e7 is described below

commit 34a85e74360f32dcfd7938e3a3c606a4869ca7e7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Aug 30 09:13:57 2021 -0400

    Refactorings; reuse UncheckedFuture.
---
 ...kedFutureImpl.java => AbstractFutureProxy.java} | 47 ++++++++++------------
 .../lang3/concurrent/UncheckedFutureImpl.java      | 26 ++----------
 .../ReflectionToStringBuilderConcurrencyTest.java  |  5 +--
 .../builder/ToStringStyleConcurrencyTest.java      |  5 +--
 .../lang3/concurrent/UncheckedFutureTest.java      | 32 ++++-----------
 5 files changed, 38 insertions(+), 77 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/concurrent/UncheckedFutureImpl.java b/src/main/java/org/apache/commons/lang3/concurrent/AbstractFutureProxy.java
similarity index 60%
copy from src/main/java/org/apache/commons/lang3/concurrent/UncheckedFutureImpl.java
copy to src/main/java/org/apache/commons/lang3/concurrent/AbstractFutureProxy.java
index 07a7c4d..45ae4bd 100644
--- a/src/main/java/org/apache/commons/lang3/concurrent/UncheckedFutureImpl.java
+++ b/src/main/java/org/apache/commons/lang3/concurrent/AbstractFutureProxy.java
@@ -14,7 +14,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.commons.lang3.concurrent;
 
 import java.util.Objects;
@@ -23,19 +22,22 @@ import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
-import org.apache.commons.lang3.exception.UncheckedInterruptedException;
-
 /**
- * An {@link Future} implementation that throws unchecked instead of checked exceptions.
+ * Proxies to a {@link Future} for subclassing.
  *
- * @see Future
+ * @param <V> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods.
  * @since 3.13.0
  */
-class UncheckedFutureImpl<V> implements UncheckedFuture<V> {
+public abstract class AbstractFutureProxy<V> implements Future<V> {
 
     private final Future<V> future;
 
-    UncheckedFutureImpl(final Future<V> future) {
+    /**
+     * Constructs a new instance.
+     *
+     * @param future the delegate.
+     */
+    public AbstractFutureProxy(final Future<V> future) {
         this.future = Objects.requireNonNull(future, "future");
     }
 
@@ -45,27 +47,22 @@ class UncheckedFutureImpl<V> implements UncheckedFuture<V> {
     }
 
     @Override
-    public V get() {
-        try {
-            return future.get();
-        } catch (final InterruptedException e) {
-            throw new UncheckedInterruptedException(e);
-        } catch (final ExecutionException e) {
-            throw new UncheckedExecutionException(e);
-        }
+    public V get() throws InterruptedException, ExecutionException {
+        return future.get();
     }
 
     @Override
-    public V get(final long timeout, final TimeUnit unit) {
-        try {
-            return future.get(timeout, unit);
-        } catch (final InterruptedException e) {
-            throw new UncheckedInterruptedException(e);
-        } catch (final ExecutionException e) {
-            throw new UncheckedExecutionException(e);
-        } catch (final TimeoutException e) {
-            throw new UncheckedTimeoutException(e);
-        }
+    public V get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
+        return future.get(timeout, unit);
+    }
+
+    /**
+     * Gets the delegate.
+     *
+     * @return the delegate.
+     */
+    public Future<V> getFuture() {
+        return future;
     }
 
     @Override
diff --git a/src/main/java/org/apache/commons/lang3/concurrent/UncheckedFutureImpl.java b/src/main/java/org/apache/commons/lang3/concurrent/UncheckedFutureImpl.java
index 07a7c4d..c88340b 100644
--- a/src/main/java/org/apache/commons/lang3/concurrent/UncheckedFutureImpl.java
+++ b/src/main/java/org/apache/commons/lang3/concurrent/UncheckedFutureImpl.java
@@ -17,7 +17,6 @@
 
 package org.apache.commons.lang3.concurrent;
 
-import java.util.Objects;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
@@ -31,23 +30,16 @@ import org.apache.commons.lang3.exception.UncheckedInterruptedException;
  * @see Future
  * @since 3.13.0
  */
-class UncheckedFutureImpl<V> implements UncheckedFuture<V> {
-
-    private final Future<V> future;
+class UncheckedFutureImpl<V> extends AbstractFutureProxy<V> implements UncheckedFuture<V> {
 
     UncheckedFutureImpl(final Future<V> future) {
-        this.future = Objects.requireNonNull(future, "future");
-    }
-
-    @Override
-    public boolean cancel(final boolean mayInterruptIfRunning) {
-        return future.cancel(mayInterruptIfRunning);
+        super(future);
     }
 
     @Override
     public V get() {
         try {
-            return future.get();
+            return super.get();
         } catch (final InterruptedException e) {
             throw new UncheckedInterruptedException(e);
         } catch (final ExecutionException e) {
@@ -58,7 +50,7 @@ class UncheckedFutureImpl<V> implements UncheckedFuture<V> {
     @Override
     public V get(final long timeout, final TimeUnit unit) {
         try {
-            return future.get(timeout, unit);
+            return super.get(timeout, unit);
         } catch (final InterruptedException e) {
             throw new UncheckedInterruptedException(e);
         } catch (final ExecutionException e) {
@@ -68,14 +60,4 @@ class UncheckedFutureImpl<V> implements UncheckedFuture<V> {
         }
     }
 
-    @Override
-    public boolean isCancelled() {
-        return future.isCancelled();
-    }
-
-    @Override
-    public boolean isDone() {
-        return future.isDone();
-    }
-
 }
diff --git a/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java
index e8e11bb..668102a 100644
--- a/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java
+++ b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java
@@ -32,6 +32,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.commons.lang3.concurrent.UncheckedFuture;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
@@ -109,9 +110,7 @@ public class ReflectionToStringBuilderConcurrencyTest {
             tasks.add(consumer);
             tasks.add(producer);
             final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
-            for (final Future<Integer> future : futures) {
-                assertEquals(REPEAT, future.get().intValue());
-            }
+            UncheckedFuture.on(futures).forEach(f -> assertEquals(REPEAT, f.get().intValue()));
         } finally {
             threadPool.shutdown();
             threadPool.awaitTermination(1, TimeUnit.SECONDS);
diff --git a/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java b/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java
index 836a299..379f7f4 100644
--- a/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java
+++ b/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java
@@ -29,6 +29,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.commons.lang3.concurrent.UncheckedFuture;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -101,9 +102,7 @@ public class ToStringStyleConcurrencyTest {
             tasks.add(consumer);
             tasks.add(consumer);
             final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
-            for (final Future<Integer> future : futures) {
-                future.get();
-            }
+            UncheckedFuture.on(futures).forEach(UncheckedFuture::get);
         } finally {
             threadPool.shutdown();
             threadPool.awaitTermination(1, TimeUnit.SECONDS);
diff --git a/src/test/java/org/apache/commons/lang3/concurrent/UncheckedFutureTest.java b/src/test/java/org/apache/commons/lang3/concurrent/UncheckedFutureTest.java
index f275344..7eab6cb 100644
--- a/src/test/java/org/apache/commons/lang3/concurrent/UncheckedFutureTest.java
+++ b/src/test/java/org/apache/commons/lang3/concurrent/UncheckedFutureTest.java
@@ -36,28 +36,22 @@ import org.junit.jupiter.api.Test;
  */
 public class UncheckedFutureTest {
 
-    private static class TestFuture<V> implements Future<V> {
+    private static class TestFuture<V> extends AbstractFutureProxy<V> {
 
-        private final V value;
         private final Exception exception;
 
         TestFuture(final Exception throwable) {
-            this.value = null;
+            super(ConcurrentUtils.constantFuture(null));
             this.exception = throwable;
         }
 
         TestFuture(final V value) {
-            this.value = value;
+            super(ConcurrentUtils.constantFuture(value));
             this.exception = null;
         }
 
-        @Override
-        public boolean cancel(final boolean mayInterruptIfRunning) {
-            return false;
-        }
-
         @SuppressWarnings("unchecked") // Programming error if call site blows up at runtime.
-        private <T extends Exception> void checkExecutionException() throws T {
+        private <T extends Exception> void checkException() throws T {
             if (exception != null) {
                 throw (T) exception;
             }
@@ -65,24 +59,14 @@ public class UncheckedFutureTest {
 
         @Override
         public V get() throws InterruptedException, ExecutionException {
-            checkExecutionException();
-            return value;
+            checkException();
+            return super.get();
         }
 
         @Override
         public V get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
-            checkExecutionException();
-            return value;
-        }
-
-        @Override
-        public boolean isCancelled() {
-            return false;
-        }
-
-        @Override
-        public boolean isDone() {
-            return false;
+            checkException();
+            return super.get(timeout, unit);
         }
 
     }