Skip to content

Commit 6e05637

Browse files
committed
Refactoring Promises.dependent to instance method Promise.dependent
1 parent 3bdef34 commit 6e05637

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

src/main/java/net/tascalate/concurrent/DependentPromise.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ public DependentPromise<T> onTimeout(Supplier<T> supplier, Duration duration, bo
228228

229229
// timeout converted to supplier
230230
Promise<Supplier<T>> onTimeout = Promises
231-
.dependent(Promises.delay(duration))
231+
.delay(duration)
232+
.dependent()
232233
.thenApply(d -> supplier, true);
233234

234235
DependentPromise<T> result = this
@@ -241,6 +242,11 @@ public DependentPromise<T> onTimeout(Supplier<T> supplier, Duration duration, bo
241242
return result;
242243
}
243244

245+
@Override
246+
public DependentPromise<T> dependent() {
247+
return this;
248+
}
249+
244250
public <U> DependentPromise<U> thenApply(Function<? super T, ? extends U> fn) {
245251
return thenApply(fn, false);
246252
}

src/main/java/net/tascalate/concurrent/Promise.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ default Promise<T> orTimeout(Duration duration) {
7575
default Promise<T> orTimeout(Duration duration, boolean cancelOnTimeout) {
7676
Promise<T> onTimeout = Promises.failAfter(duration);
7777
// Use *async to execute on default "this" executor
78-
Promise<T> result = Promises.dependent(this)
78+
Promise<T> result = dependent()
7979
.applyToEitherAsync(onTimeout, Function.identity(), PromiseOrigin.PARAM_ONLY);
8080

8181
result.whenComplete(Promises.timeoutsCleanup(this, onTimeout, cancelOnTimeout));
@@ -115,10 +115,11 @@ default Promise<T> onTimeout(Supplier<T> supplier, Duration duration, boolean ca
115115

116116
// timeout converted to supplier
117117
Promise<Supplier<T>> onTimeout = Promises
118-
.dependent(Promises.delay(duration))
118+
.delay(duration)
119+
.dependent()
119120
.thenApply(d -> supplier, true);
120121

121-
Promise<T> result = Promises.dependent(this)
122+
Promise<T> result = dependent()
122123
// resolved value converted to supplier
123124
.thenApply(valueToSupplier, false)
124125
// Use *async to execute on default "this" executor
@@ -128,6 +129,15 @@ default Promise<T> onTimeout(Supplier<T> supplier, Duration duration, boolean ca
128129
return result;
129130
}
130131

132+
/**
133+
* Converts this {@link Promise} to a {@link DependentPromise}
134+
* @return
135+
* created DependentPromise
136+
*/
137+
default DependentPromise<T> dependent() {
138+
return DependentPromise.from(this);
139+
}
140+
131141
public <U> Promise<U> thenApply(Function<? super T, ? extends U> fn);
132142

133143
public <U> Promise<U> thenApplyAsync(Function<? super T, ? extends U> fn);

src/main/java/net/tascalate/concurrent/Promises.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,9 @@ static <T, R> Promise<R> from(CompletionStage<T> stage,
111111
return result;
112112
}
113113

114-
/**
115-
* Converts {@link CompletionStage} to a {@link DependentPromise}
116-
* @param stage
117-
* original CompletionStage
118-
* @param <T>
119-
* a type of the successfully resolved promise value
120-
* @return
121-
* created DependentPromise
122-
*/
123-
public static <T> DependentPromise<T> dependent(CompletionStage<T> stage) {
124-
return dependent(from(stage));
125-
}
126-
127-
/**
128-
* Converts {@link Promise} to a {@link DependentPromise}
129-
* @param stage
130-
* original Promise
131-
* @param <T>
132-
* a type of the successfully resolved promise value
133-
* @return
134-
* created DependentPromise
135-
*/
136-
public static <T> DependentPromise<T> dependent(Promise<T> stage) {
137-
return DependentPromise.from(stage);
138-
}
139-
140114
public static <T> Promise<T> task(CompletionStage<T> stage, Executor executor) {
141-
return dependent(CompletableTask.asyncOn(executor))
115+
return CompletableTask.asyncOn(executor)
116+
.dependent()
142117
.thenCombineAsync(stage, (u, v) -> v, PromiseOrigin.PARAM_ONLY);
143118
}
144119

@@ -532,15 +507,15 @@ public static Promise<Void> poll(Runnable codeBlock, Executor executor, RetryPol
532507
() -> { codeBlock.run(); return Optional.of(new Object()); },
533508
executor, retryPolicy
534509
);
535-
return dependent(wrappedResult).thenApply(v -> null, true);
510+
return wrappedResult.dependent().thenApply(v -> null, true);
536511
}
537512

538513
public static <T> Promise<T> poll(Callable<? extends T> codeBlock, Executor executor, RetryPolicy retryPolicy) {
539514
Promise<ObjectRef<T>> wrappedResult = pollOptional(
540515
() -> Optional.of(new ObjectRef<T>( codeBlock.call() )),
541516
executor, retryPolicy
542517
);
543-
return dependent(wrappedResult).thenApply(ObjectRef::dereference, true);
518+
return wrappedResult.dependent().thenApply(ObjectRef::dereference, true);
544519
}
545520

546521
public static <T> Promise<T> pollOptional(Callable<Optional<? extends T>> codeBlock, Executor executor, RetryPolicy retryPolicy) {

0 commit comments

Comments
 (0)