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
46
47
48 public HandshakeFuture()
49 {
50
51 }
52
53
54
55
56
57
58 public synchronized void cancel()
59 {
60
61 cancelled = true;
62
63
64 notifyAll();
65 }
66
67
68
69
70
71
72
73 public synchronized void secured()
74 {
75 done = true;
76
77 notifyAll();
78 }
79
80
81
82
83
84 @Override
85 public synchronized boolean cancel( boolean mayInterruptIfRunning )
86 {
87 if ( cancelled )
88 {
89 return cancelled;
90 }
91
92
93 cancelled = true;
94
95
96 notifyAll();
97
98 return cancelled;
99 }
100
101
102
103
104
105 @Override
106 public synchronized Boolean get() throws InterruptedException, ExecutionException
107 {
108 while ( !done && !cancelled )
109 {
110 wait();
111 }
112
113 return done;
114 }
115
116
117
118
119
120 @Override
121 public synchronized Boolean get( long timeout, TimeUnit unit ) throws InterruptedException, ExecutionException, TimeoutException
122 {
123 wait( unit.toMillis( timeout ) );
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 }
162