Refactorings; reuse UncheckedFuture.

This commit is contained in:
Gary Gregory 2021-08-30 09:13:57 -04:00
parent c28b2e4e71
commit 34a85e7436
5 changed files with 94 additions and 52 deletions

View File

@ -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 <V> The result type returned by this Future's {@link #get()} and {@link #get(long, TimeUnit)} methods.
* @since 3.13.0
*/
public abstract class AbstractFutureProxy<V> implements Future<V> {
private final Future<V> future;
/**
* Constructs a new instance.
*
* @param future the delegate.
*/
public AbstractFutureProxy(final Future<V> 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<V> getFuture() {
return future;
}
@Override
public boolean isCancelled() {
return future.isCancelled();
}
@Override
public boolean isDone() {
return future.isDone();
}
}

View File

@ -17,7 +17,6 @@
package org.apache.commons.lang3.concurrent; package org.apache.commons.lang3.concurrent;
import java.util.Objects;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -31,23 +30,16 @@
* @see Future * @see Future
* @since 3.13.0 * @since 3.13.0
*/ */
class UncheckedFutureImpl<V> implements UncheckedFuture<V> { class UncheckedFutureImpl<V> extends AbstractFutureProxy<V> implements UncheckedFuture<V> {
private final Future<V> future;
UncheckedFutureImpl(final Future<V> future) { UncheckedFutureImpl(final Future<V> future) {
this.future = Objects.requireNonNull(future, "future"); super(future);
}
@Override
public boolean cancel(final boolean mayInterruptIfRunning) {
return future.cancel(mayInterruptIfRunning);
} }
@Override @Override
public V get() { public V get() {
try { try {
return future.get(); return super.get();
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
throw new UncheckedInterruptedException(e); throw new UncheckedInterruptedException(e);
} catch (final ExecutionException e) { } catch (final ExecutionException e) {
@ -58,7 +50,7 @@ public V get() {
@Override @Override
public V get(final long timeout, final TimeUnit unit) { public V get(final long timeout, final TimeUnit unit) {
try { try {
return future.get(timeout, unit); return super.get(timeout, unit);
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
throw new UncheckedInterruptedException(e); throw new UncheckedInterruptedException(e);
} catch (final ExecutionException 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();
}
} }

View File

@ -32,6 +32,7 @@
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.concurrent.UncheckedFuture;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -109,9 +110,7 @@ private void testConcurrency(final CollectionHolder<List<Integer>> holder) throw
tasks.add(consumer); tasks.add(consumer);
tasks.add(producer); tasks.add(producer);
final List<Future<Integer>> futures = threadPool.invokeAll(tasks); final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
for (final Future<Integer> future : futures) { UncheckedFuture.on(futures).forEach(f -> assertEquals(REPEAT, f.get().intValue()));
assertEquals(REPEAT, future.get().intValue());
}
} finally { } finally {
threadPool.shutdown(); threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.SECONDS); threadPool.awaitTermination(1, TimeUnit.SECONDS);

View File

@ -29,6 +29,7 @@
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.concurrent.UncheckedFuture;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@ -101,9 +102,7 @@ private void testConcurrency(final CollectionHolder<List<Integer>> holder) throw
tasks.add(consumer); tasks.add(consumer);
tasks.add(consumer); tasks.add(consumer);
final List<Future<Integer>> futures = threadPool.invokeAll(tasks); final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
for (final Future<Integer> future : futures) { UncheckedFuture.on(futures).forEach(UncheckedFuture::get);
future.get();
}
} finally { } finally {
threadPool.shutdown(); threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.SECONDS); threadPool.awaitTermination(1, TimeUnit.SECONDS);

View File

@ -36,28 +36,22 @@
*/ */
public class UncheckedFutureTest { 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; private final Exception exception;
TestFuture(final Exception throwable) { TestFuture(final Exception throwable) {
this.value = null; super(ConcurrentUtils.constantFuture(null));
this.exception = throwable; this.exception = throwable;
} }
TestFuture(final V value) { TestFuture(final V value) {
this.value = value; super(ConcurrentUtils.constantFuture(value));
this.exception = null; this.exception = null;
} }
@Override
public boolean cancel(final boolean mayInterruptIfRunning) {
return false;
}
@SuppressWarnings("unchecked") // Programming error if call site blows up at runtime. @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) { if (exception != null) {
throw (T) exception; throw (T) exception;
} }
@ -65,24 +59,14 @@ private <T extends Exception> void checkExecutionException() throws T {
@Override @Override
public V get() throws InterruptedException, ExecutionException { public V get() throws InterruptedException, ExecutionException {
checkExecutionException(); checkException();
return value; return super.get();
} }
@Override @Override
public V get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { public V get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
checkExecutionException(); checkException();
return value; return super.get(timeout, unit);
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return false;
} }
} }