1
14
15 package com.liferay.portal.cluster;
16
17 import java.util.concurrent.CancellationException;
18 import java.util.concurrent.CountDownLatch;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23
24
29 public class FutureResult<V> implements Future<V> {
30
31 public boolean cancel(boolean mayInterruptIfRunning) {
32 if (_cancelled || isDone()) {
33 return false;
34 }
35
36 _cancelled = true;
37
38 return true;
39 }
40
41 public V get() throws InterruptedException, ExecutionException {
42 if (_cancelled) {
43 throw new CancellationException();
44 }
45
46 _countDownLatch.await();
47
48 if (_exception != null) {
49 throw new ExecutionException(_exception);
50 }
51
52 return _result;
53 }
54
55 public V get(long timeout, TimeUnit unit)
56 throws ExecutionException, InterruptedException, TimeoutException {
57
58 if (_cancelled) {
59 throw new CancellationException();
60 }
61
62 if (_countDownLatch.await(timeout, unit)) {
63 if (_exception != null) {
64 throw new ExecutionException(_exception);
65 }
66
67 return _result;
68 }
69 else {
70 throw new TimeoutException();
71 }
72 }
73
74 public boolean hasException() {
75 return _exception != null;
76 }
77
78 public boolean isCancelled() {
79 return _cancelled;
80 }
81
82 public boolean isDone() {
83 if ((_countDownLatch.getCount() == 0) || _cancelled) {
84 return true;
85 }
86
87 return false;
88 }
89
90 public void setException(Exception exception) {
91 _exception = exception;
92 _countDownLatch.countDown();
93 }
94
95 public void setResult(V result) {
96 _result = result;
97 _countDownLatch.countDown();
98 }
99
100 private boolean _cancelled;
101 private CountDownLatch _countDownLatch = new CountDownLatch(1);
102 private Exception _exception;
103 private V _result;
104
105 }