diff --git a/src/java/org/apache/commons/lang/concurrent/ConcurrentUtils.java b/src/java/org/apache/commons/lang/concurrent/ConcurrentUtils.java
index 203ba4ac4..032bfdaca 100644
--- a/src/java/org/apache/commons/lang/concurrent/ConcurrentUtils.java
+++ b/src/java/org/apache/commons/lang/concurrent/ConcurrentUtils.java
@@ -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;
/**
*
@@ -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();
}
}
+
+ //-----------------------------------------------------------------------
+ /**
+ *
+ * Gets an implementation of Future
that is immediately done
+ * and returns the specified constant value.
+ *
+ *
+ * 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.
+ *
+ *
+ * @param value the constant value to return, may be null
+ * @return an instance of Future that will return the value, never null
+ */
+ public static Future constantFuture(T value) {
+ return new ConstantFuture(value);
+ }
+
+ static final class ConstantFuture implements Future {
+ /** 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;
+ }
+ }
+
}
diff --git a/src/test/org/apache/commons/lang/concurrent/ConcurrentUtilsTest.java b/src/test/org/apache/commons/lang/concurrent/ConcurrentUtilsTest.java
index 04cb86c51..879b0cb71 100644
--- a/src/test/org/apache/commons/lang/concurrent/ConcurrentUtilsTest.java
+++ b/src/test/org/apache/commons/lang/concurrent/ConcurrentUtilsTest.java
@@ -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 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 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));
+ }
+
}