[LANG-545] Added ability to create a ConstantFuture object. Thanks to Stephen Colebourne for the patch.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@833709 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oliver Heger 2009-11-07 16:37:05 +00:00
parent 5bfa8aaf14
commit caf3f0c3c3
2 changed files with 86 additions and 0 deletions

View File

@ -17,6 +17,8 @@
package org.apache.commons.lang.concurrent;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
* <p>
@ -28,6 +30,7 @@ import java.util.concurrent.ExecutionException;
* @version $Id$
*/
public class ConcurrentUtils {
/**
* Private constructor so that no instances can be created. This class
* contains only static utility methods.
@ -117,4 +120,53 @@ public class ConcurrentUtils {
throw (Error) ex.getCause();
}
}
//-----------------------------------------------------------------------
/**
* <p>
* Gets an implementation of <code>Future</code> that is immediately done
* and returns the specified constant value.
* </p>
* <p>
* This can be useful to return a simple constant immediately from the
* concurrent processing, perhaps as part of avoiding nulls.
* A constant future can also be useful in testing.
* </p>
*
* @param value the constant value to return, may be null
* @return an instance of Future that will return the value, never null
*/
public static <T> Future<T> constantFuture(T value) {
return new ConstantFuture<T>(value);
}
static final class ConstantFuture<T> implements Future<T> {
/** The constant value. */
private final T value;
ConstantFuture(T value) {
this.value = value;
}
public boolean isDone() {
return true;
}
public T get() {
return value;
}
public T get(long timeout, TimeUnit unit) {
return value;
}
public boolean isCancelled() {
return false;
}
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}
}
}

View File

@ -17,6 +17,8 @@
package org.apache.commons.lang.concurrent;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
@ -161,4 +163,36 @@ public class ConcurrentUtilsTest extends TestCase {
ConcurrentUtils.handleCause(null);
ConcurrentUtils.handleCause(new ExecutionException("Test", null));
}
//-----------------------------------------------------------------------
/**
* Tests constant future.
*/
public void testConstantFuture_Integer() throws Exception {
Integer value = new Integer(5);
Future<Integer> test = ConcurrentUtils.constantFuture(value);
assertEquals(true, test.isDone());
assertSame(value, test.get());
assertSame(value, test.get(1000, TimeUnit.SECONDS));
assertSame(value, test.get(1000, null));
assertEquals(false, test.isCancelled());
assertEquals(false, test.cancel(true));
assertEquals(false, test.cancel(false));
}
/**
* Tests constant future.
*/
public void testConstantFuture_null() throws Exception {
Integer value = null;
Future<Integer> test = ConcurrentUtils.constantFuture(value);
assertEquals(true, test.isDone());
assertSame(value, test.get());
assertSame(value, test.get(1000, TimeUnit.SECONDS));
assertSame(value, test.get(1000, null));
assertEquals(false, test.isCancelled());
assertEquals(false, test.cancel(true));
assertEquals(false, test.cancel(false));
}
}