提交 c6ecaacc 编写于 作者: R Rossen Stoyanchev

Add getResult/hasResult methods to DeferredResult

Issue: SPR-10603
上级 9bc4663e
...@@ -107,6 +107,22 @@ public class DeferredResult<T> { ...@@ -107,6 +107,22 @@ public class DeferredResult<T> {
return ((this.result != RESULT_NONE) || this.expired); return ((this.result != RESULT_NONE) || this.expired);
} }
/**
* @return {@code true} if the DeferredResult has been set.
*/
public boolean hasResult() {
return this.result != RESULT_NONE;
}
/**
* @return the result or {@code null} if the result wasn't set; since the result can
* also be {@code null}, it is recommended to use {@link #hasResult()} first
* to check if there is a result prior to calling this method.
*/
public Object getResult() {
return hasResult() ? this.result : null;
}
/** /**
* Return the configured timeout value in milliseconds. * Return the configured timeout value in milliseconds.
*/ */
......
...@@ -20,7 +20,7 @@ import org.junit.Test; ...@@ -20,7 +20,7 @@ import org.junit.Test;
import org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler; import org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*; import static org.mockito.Mockito.*;
/** /**
* DeferredResult tests. * DeferredResult tests.
...@@ -69,6 +69,21 @@ public class DeferredResultTests { ...@@ -69,6 +69,21 @@ public class DeferredResultTests {
verify(handler).handleResult("hello"); verify(handler).handleResult("hello");
} }
@Test
public void hasResult() {
DeferredResultHandler handler = mock(DeferredResultHandler.class);
DeferredResult<String> result = new DeferredResult<String>();
result.setResultHandler(handler);
assertFalse(result.hasResult());
assertNull(result.getResult());
result.setResult("hello");
assertEquals("hello", result.getResult());
}
@Test @Test
public void onCompletion() throws Exception { public void onCompletion() throws Exception {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册