From caf3f0c3c3d2308f6a61554cfabae429b6efc760 Mon Sep 17 00:00:00 2001 From: Oliver Heger Date: Sat, 7 Nov 2009 16:37:05 +0000 Subject: [PATCH] [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 --- .../lang/concurrent/ConcurrentUtils.java | 52 +++++++++++++++++++ .../lang/concurrent/ConcurrentUtilsTest.java | 34 ++++++++++++ 2 files changed, 86 insertions(+) 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)); + } + }