View Javadoc

1   package org.dfdaemon.il2.spi.command;
2   
3   import org.dfdaemon.il2.api.command.Command;
4   
5   import java.util.concurrent.TimeUnit;
6   import java.util.concurrent.locks.Condition;
7   import java.util.concurrent.locks.Lock;
8   import java.util.concurrent.locks.ReentrantLock;
9   
10  /**
11   * @author aka50
12   */
13  public class CommandFuture {
14  
15      /**
16       * Statuses
17       */
18      public enum Status {
19          UNDEFINED,
20          OK,
21          FAILED
22      }
23  
24  
25      private final Command<?> _command;
26      private volatile Status _status;
27      private String _reason;
28      private Object _result;
29  
30      private final Lock _lock = new ReentrantLock();
31      private final Condition _completeCond = _lock.newCondition();
32  
33      public <C extends Command<?>> CommandFuture(C cmd) {
34          this._command = cmd;
35          this._status = null;
36          this._reason = "";
37  
38      }
39  
40      public void setResult(Object o) {
41          _result = o;
42      }
43  
44  
45      public Object getResult() {
46          return _result;
47      }
48  
49      public Command<?> getCommand() {
50          return _command;
51      }
52  
53      public Status getStatus() {
54          return _status;
55      }
56  
57      public void setStatus(Status status) {
58          _status = status;
59      }
60  
61      public String getReason() {
62          return _reason;
63      }
64  
65      public void awaitComplete() throws InterruptedException {
66          _lock.lock();
67          try {
68              while (_status == null)
69                  _completeCond.await(100, TimeUnit.MILLISECONDS);
70          } finally {
71              _lock.unlock();
72          }
73      }
74  
75      public void complete(Status status, String reason) {
76          _status = status;
77          _reason = reason;
78          complete();
79      }
80  
81      public void complete() {
82          _lock.lock();
83          try {
84              _completeCond.signal();
85          } finally {
86              _lock.unlock();
87          }
88      }
89  }
90