Skip to content

Commit 841e02d

Browse files
committed
Simplify AbstractCompletableTask / CompletableSubTask dependencies
1 parent 3930ead commit 841e02d

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,22 @@ abstract class AbstractCompletableTask<T> extends PromiseAdapterExtended<T>
5757

5858
private final CallbackRegistry<T> callbackRegistry = new CallbackRegistry<>();
5959
protected final RunnableFuture<T> task;
60-
protected final Callable<T> action;
6160

6261
protected AbstractCompletableTask(Executor defaultExecutor, Callable<T> action) {
6362
super(defaultExecutor);
64-
this.action = action;
65-
this.task = new StageTransition(action);
63+
this.task = new FutureTask<T>(action) {
64+
@Override
65+
protected void set(T v) {
66+
super.set(v);
67+
success(v);
68+
};
69+
70+
@Override
71+
protected void setException(Throwable t) {
72+
super.setException(t);
73+
failure(t);
74+
};
75+
};
6676
}
6777

6878
private volatile CompletionStage<?> intermediateStage;
@@ -129,25 +139,6 @@ public String toString() {
129139
return String.format("%s@%d[%s]", getClass().getSimpleName(), System.identityHashCode(this), task);
130140
}
131141

132-
class StageTransition extends FutureTask<T>
133-
implements CompletableFuture.AsynchronousCompletionTask {
134-
StageTransition(Callable<T> callable) {
135-
super(callable);
136-
}
137-
138-
@Override
139-
protected void set(T v) {
140-
super.set(v);
141-
success(v);
142-
};
143-
144-
@Override
145-
protected void setException(Throwable t) {
146-
super.setException(t);
147-
failure(t);
148-
};
149-
}
150-
151142
// Override thenApplyAsync and exceptionallyAsync just to minimize amount of wrappers
152143
// Otherwise delegation to handleAsync (in superclass) works perfect
153144
@Override

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
class CompletableSubTask<T> extends AbstractCompletableTask<T> {
2929

30-
static class DelegatingCallable<T> implements Callable<T> {
30+
private static class DelegatingCallable<T> implements Callable<T> {
3131
private final AtomicReference<Callable<T>> delegateRef = new AtomicReference<>();
3232

3333
void setup(Callable<T> delegate) {
@@ -48,15 +48,21 @@ public T call() throws Exception {
4848
}
4949

5050
}
51+
52+
private final DelegatingCallable<T> action;
5153

5254
CompletableSubTask(Executor executor) {
53-
super(executor, new DelegatingCallable<>());
55+
this(executor, new DelegatingCallable<>());
56+
}
57+
58+
private CompletableSubTask(Executor executor, DelegatingCallable<T> action) {
59+
super(executor, action);
60+
this.action = action;
5461
}
5562

5663
@Override
57-
void fireTransition(Callable<T> code) {
58-
DelegatingCallable<T> transitionCall = (DelegatingCallable<T>) action;
59-
transitionCall.setup(code);
64+
final void fireTransition(Callable<T> code) {
65+
action.setup(code);
6066
task.run();
6167
}
6268

0 commit comments

Comments
 (0)