mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-09 19:45:01 +00:00
Refactorings; reuse UncheckedFuture.
This commit is contained in:
parent
c28b2e4e71
commit
34a85e7436
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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<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 @@ 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<List<Integer>> holder) throw
|
||||
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);
|
||||
|
@ -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<List<Integer>> holder) throw
|
||||
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);
|
||||
|
@ -36,28 +36,22 @@
|
||||
*/
|
||||
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 @@ private <T extends Exception> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user