@@ -136,71 +136,106 @@ private static <U> DependentPromise<U> doWrap(Promise<U> original, CompletionSta
136136
137137
138138 public DependentPromise <T > orTimeout (long timeout , TimeUnit unit ) {
139- return orTimeout (timeout , unit , false );
139+ return orTimeout (timeout , unit , true );
140+ }
141+
142+ public DependentPromise <T > orTimeout (long timeout , TimeUnit unit , boolean cancelOnTimeout ) {
143+ return orTimeout (timeout , unit , cancelOnTimeout , false );
140144 }
141145
142- public DependentPromise <T > orTimeout (long timeout , TimeUnit unit , boolean enlistOrigin ) {
143- return orTimeout (Promises .toDuration (timeout , unit ), enlistOrigin );
146+ public DependentPromise <T > orTimeout (long timeout , TimeUnit unit , boolean cancelOnTimeout , boolean enlistOrigin ) {
147+ return orTimeout (Promises .toDuration (timeout , unit ), cancelOnTimeout , enlistOrigin );
144148 }
145149
146150 public DependentPromise <T > orTimeout (Duration duration ) {
147- return orTimeout (duration , false );
151+ return orTimeout (duration , true );
152+ }
153+
154+ public DependentPromise <T > orTimeout (Duration duration , boolean cancelOnTimeout ) {
155+ return orTimeout (duration , cancelOnTimeout , false );
148156 }
149157
150- public DependentPromise <T > orTimeout (Duration duration , boolean enlistOrigin ) {
158+ public DependentPromise <T > orTimeout (Duration duration , boolean cancelOnTimeout , boolean enlistOrigin ) {
159+ Promise <T > onTimeout = Promises .failAfter (duration );
151160 // Use *async to execute on default "this" executor
152- return applyToEitherAsync (
153- Promises .failAfter (duration ),
154- Function .identity (),
161+ return Promises .dependent (this ).applyToEitherAsync (
162+ onTimeout ,
163+ v -> {
164+ if (cancelOnTimeout ) {
165+ cancel (true );
166+ }
167+ onTimeout .cancel (true );
168+ return v ;
169+ },
155170 enlistOrigin ? PromiseOrigin .ALL : PromiseOrigin .PARAM_ONLY
156- );
171+ );
172+ }
173+
174+ public DependentPromise <T > onTimeout (T value , long timeout , TimeUnit unit ) {
175+ return onTimeout (value , timeout , unit , true );
176+ }
177+
178+ public DependentPromise <T > onTimeout (T value , long timeout , TimeUnit unit , boolean cancelOnTimeout ) {
179+ return onTimeout (value , timeout , unit , cancelOnTimeout , false );
157180 }
158181
159- public DependentPromise <T > completeOnTimeout (T value , long timeout , TimeUnit unit ) {
160- return completeOnTimeout (value , timeout , unit , false );
182+ public DependentPromise <T > onTimeout (T value , long timeout , TimeUnit unit , boolean cancelOnTimeout , boolean enlistOrigin ) {
183+ return onTimeout (value , Promises . toDuration ( timeout , unit ), cancelOnTimeout , enlistOrigin );
161184 }
162185
163- public DependentPromise <T > completeOnTimeout (T value , long timeout , TimeUnit unit , boolean enlistOrigin ) {
164- return completeOnTimeout (value , Promises . toDuration ( timeout , unit ), enlistOrigin );
186+ public DependentPromise <T > onTimeout (T value , Duration duration ) {
187+ return onTimeout (value , duration , true );
165188 }
166189
167- public DependentPromise <T > completeOnTimeout (T value , Duration duration ) {
168- return completeOnTimeout (value , duration , false );
190+ public DependentPromise <T > onTimeout (T value , Duration duration , boolean cancelOnTimeout ) {
191+ return onTimeout (value , duration , cancelOnTimeout , false );
169192 }
170193
171- public DependentPromise <T > completeOnTimeout (T value , Duration duration , boolean enlistOrigin ) {
172- // Use *async to execute on default "this" executor
173- return applyToEitherAsync (
174- // timeout converted to onTimeout value
175- DependentPromise .from (Promises .delay (duration )).thenApply (d -> value , true ),
176- Function .identity (),
177- enlistOrigin ? PromiseOrigin .ALL : PromiseOrigin .PARAM_ONLY
178- );
194+ public DependentPromise <T > onTimeout (T value , Duration duration , boolean cancelOnTimeout , boolean enlistOrigin ) {
195+ return onTimeout (() -> value , duration , enlistOrigin );
196+ }
197+
198+ public DependentPromise <T > onTimeout (Supplier <T > supplier , long timeout , TimeUnit unit ) {
199+ return onTimeout (supplier , timeout , unit , true );
179200 }
180201
181- public DependentPromise <T > completeOnTimeout (Supplier <T > supplier , long timeout , TimeUnit unit ) {
182- return completeOnTimeout (supplier , timeout , unit , false );
202+ public DependentPromise <T > onTimeout (Supplier <T > supplier , long timeout , TimeUnit unit , boolean cancelOnTimeout ) {
203+ return onTimeout (supplier , Promises . toDuration ( timeout , unit ) , false );
183204 }
184205
185- public DependentPromise <T > completeOnTimeout (Supplier <T > supplier , long timeout , TimeUnit unit , boolean enlistOrigin ) {
186- return completeOnTimeout (supplier , Promises .toDuration (timeout , unit ), enlistOrigin );
206+ public DependentPromise <T > onTimeout (Supplier <T > supplier , long timeout , TimeUnit unit , boolean cancelOnTimeout , boolean enlistOrigin ) {
207+ return onTimeout (supplier , Promises .toDuration (timeout , unit ), enlistOrigin );
187208 }
188209
189- public DependentPromise <T > completeOnTimeout (Supplier <T > supplier , Duration duration ) {
190- return completeOnTimeout (supplier , duration , false );
210+ public DependentPromise <T > onTimeout (Supplier <T > supplier , Duration duration ) {
211+ return onTimeout (supplier , duration , true );
212+ }
213+
214+ public DependentPromise <T > onTimeout (Supplier <T > supplier , Duration duration , boolean cancelOnTimeout ) {
215+ return onTimeout (supplier , duration , cancelOnTimeout , false );
191216 }
192217
193- public DependentPromise <T > completeOnTimeout (Supplier <T > supplier , Duration duration , boolean enlistOrigin ) {
218+ public DependentPromise <T > onTimeout (Supplier <T > supplier , Duration duration , boolean cancelOnTimeout , boolean enlistOrigin ) {
194219 Function <T , Supplier <T >> valueToSupplier = v -> () -> v ;
195- return this
196- .thenApply (valueToSupplier , enlistOrigin ) // ready this value converted to supplier
197- .applyToEitherAsync (// Use *async to execute on default "this" executor
198- // timeout converted to supplier of onTimeout value
199- DependentPromise .from (Promises .delay (duration )).thenApply (d -> supplier , true ),
200- Function .identity (),
220+
221+ // timeout converted to supplier
222+ Promise <Supplier <T >> onTimeout = Promises .dependent (Promises .delay (duration )).thenApply (d -> supplier , true );
223+
224+ return Promises .dependent (this )
225+ // resolved value converted to supplier
226+ .thenApply (valueToSupplier , enlistOrigin )
227+ // Use *async to execute on default "this" executor
228+ .applyToEitherAsync (
229+ onTimeout ,
230+ s -> {
231+ if (cancelOnTimeout ) {
232+ cancel (true );
233+ }
234+ onTimeout .cancel (true );
235+ return s .get ();
236+ },
201237 PromiseOrigin .ALL
202- )
203- .thenApply (s -> s .get (), true );
238+ );
204239 }
205240
206241 public <U > DependentPromise <U > thenApply (Function <? super T , ? extends U > fn ) {
0 commit comments