1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
25   * <a href="FutureResult.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Tina Tian
28   */
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 }