From 34a85e74360f32dcfd7938e3a3c606a4869ca7e7 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 30 Aug 2021 09:13:57 -0400 Subject: [PATCH] Refactorings; reuse UncheckedFuture. --- .../lang3/concurrent/AbstractFutureProxy.java | 78 +++++++++++++++++++ .../lang3/concurrent/UncheckedFutureImpl.java | 26 +------ ...lectionToStringBuilderConcurrencyTest.java | 5 +- .../builder/ToStringStyleConcurrencyTest.java | 5 +- .../lang3/concurrent/UncheckedFutureTest.java | 32 ++------ 5 files changed, 94 insertions(+), 52 deletions(-) create mode 100644 src/main/java/org/apache/commons/lang3/concurrent/AbstractFutureProxy.java diff --git a/src/main/java/org/apache/commons/lang3/concurrent/AbstractFutureProxy.java b/src/main/java/org/apache/commons/lang3/concurrent/AbstractFutureProxy.java new file mode 100644 index 000000000..45ae4bd74 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/concurrent/AbstractFutureProxy.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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; +import java.util.concurrent.TimeoutException; + +/** + * Proxies to a {@link Future} for subclassing. + * + * @param The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods. + * @since 3.13.0 + */ +public abstract class AbstractFutureProxy implements Future { + + private final Future future; + + /** + * Constructs a new instance. + * + * @param future the delegate. + */ + public AbstractFutureProxy(final Future future) { + this.future = Objects.requireNonNull(future, "future"); + } + + @Override + public boolean cancel(final boolean mayInterruptIfRunning) { + return future.cancel(mayInterruptIfRunning); + } + + @Override + public V get() throws InterruptedException, ExecutionException { + return future.get(); + } + + @Override + 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 getFuture() { + return future; + } + + @Override + public boolean isCancelled() { + return future.isCancelled(); + } + + @Override + public boolean isDone() { + return future.isDone(); + } + +} 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 07a7c4d76..c88340b82 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 @@ * @see Future * @since 3.13.0 */ -class UncheckedFutureImpl implements UncheckedFuture { - - private final Future future; +class UncheckedFutureImpl extends AbstractFutureProxy implements UncheckedFuture { UncheckedFutureImpl(final Future 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 @@ public V get() { @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 @@ public V get(final long timeout, final TimeUnit unit) { } } - @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 e8e11bba2..668102a9d 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.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 @@ private void testConcurrency(final CollectionHolder> holder) throw tasks.add(consumer); tasks.add(producer); final List> futures = threadPool.invokeAll(tasks); - for (final Future 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 836a2993d..379f7f47b 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.Future; import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.concurrent.UncheckedFuture; import org.junit.jupiter.api.Test; /** @@ -101,9 +102,7 @@ private void testConcurrency(final CollectionHolder> holder) throw tasks.add(consumer); tasks.add(consumer); final List> futures = threadPool.invokeAll(tasks); - for (final Future 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 f2753441f..7eab6cb04 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 @@ */ public class UncheckedFutureTest { - private static class TestFuture implements Future { + private static class TestFuture extends AbstractFutureProxy { - 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 void checkExecutionException() throws T { + private void checkException() throws T { if (exception != null) { throw (T) exception; } @@ -65,24 +59,14 @@ private void checkExecutionException() throws T { @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); } }