You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2009/11/07 17:37:06 UTC
svn commit: r833709 - in /commons/proper/lang/trunk/src:
java/org/apache/commons/lang/concurrent/ConcurrentUtils.java
test/org/apache/commons/lang/concurrent/ConcurrentUtilsTest.java
Author: oheger
Date: Sat Nov 7 16:37:05 2009
New Revision: 833709
URL: http://svn.apache.org/viewvc?rev=833709&view=rev
Log:
[LANG-545] Added ability to create a ConstantFuture object. Thanks to Stephen Colebourne for the patch.
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/ConcurrentUtils.java
commons/proper/lang/trunk/src/test/org/apache/commons/lang/concurrent/ConcurrentUtilsTest.java
Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/ConcurrentUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/ConcurrentUtils.java?rev=833709&r1=833708&r2=833709&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/ConcurrentUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/concurrent/ConcurrentUtils.java Sat Nov 7 16:37:05 2009
@@ -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 @@
* @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 @@
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;
+ }
+ }
+
}
Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/concurrent/ConcurrentUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/concurrent/ConcurrentUtilsTest.java?rev=833709&r1=833708&r2=833709&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/concurrent/ConcurrentUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/concurrent/ConcurrentUtilsTest.java Sat Nov 7 16:37:05 2009
@@ -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 @@
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));
+ }
+
}