1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.ldap.client.api.future;
21
22
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.Future;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
27
28
29
30
31
32
33
34 public class HandshakeFuture implements Future<Boolean>
35 {
36
37 private volatile boolean done = false;
38
39
40 protected boolean cancelled = false;
41
42
43
44
45 public HandshakeFuture()
46 {
47
48 }
49
50
51
52
53
54
55 public synchronized void cancel()
56 {
57
58 cancelled = true;
59
60
61 notifyAll();
62 }
63
64
65
66
67
68 public synchronized void secured()
69 {
70 done = true;
71
72 notifyAll();
73 }
74
75
76
77
78
79 @Override
80 public synchronized boolean cancel( boolean mayInterruptIfRunning )
81 {
82 if ( cancelled )
83 {
84 return cancelled;
85 }
86
87
88 cancelled = true;
89
90
91 notifyAll();
92
93 return cancelled;
94 }
95
96
97
98
99
100 @Override
101 public synchronized Boolean get() throws InterruptedException, ExecutionException
102 {
103 while ( !done && !cancelled )
104 {
105 wait();
106 }
107
108 return done;
109 }
110
111
112
113
114
115 @Override
116 public synchronized Boolean get( long timeout, TimeUnit unit )
117 throws InterruptedException, ExecutionException, TimeoutException
118 {
119
120 if ( !done && !cancelled )
121 {
122 wait( unit.toMillis( timeout ) );
123 }
124
125 return done;
126 }
127
128
129
130
131
132 @Override
133 public boolean isCancelled()
134 {
135 return cancelled;
136 }
137
138
139
140
141
142 @Override
143 public boolean isDone()
144 {
145 return done;
146 }
147
148
149
150
151
152 @Override
153 public String toString()
154 {
155 StringBuilder sb = new StringBuilder();
156
157 sb.append( "HandshakeFuture, completed: " ).append( done ).append( ", cancelled: " ).append( cancelled );
158
159 return sb.toString();
160 }
161 }