393 |
|
|
394 |
|
/** A Completion with a source, dependent, and executor. */ |
395 |
|
@SuppressWarnings("serial") |
396 |
< |
abstract static class UniCompletion<T> extends Completion { |
396 |
> |
abstract static class UniCompletion<T,V> extends Completion { |
397 |
|
Executor executor; // executor to use (null if none) |
398 |
< |
CompletableFuture<T> dep; // the dependent to complete |
399 |
< |
CompletableFuture<?> src; // source for action |
398 |
> |
CompletableFuture<V> dep; // the dependent to complete |
399 |
> |
CompletableFuture<T> src; // source for action |
400 |
|
|
401 |
< |
UniCompletion(Executor executor, CompletableFuture<T> dep, |
402 |
< |
CompletableFuture<?> src) { |
401 |
> |
UniCompletion(Executor executor, CompletableFuture<V> dep, |
402 |
> |
CompletableFuture<T> src) { |
403 |
|
this.executor = executor; this.dep = dep; this.src = src; |
404 |
|
} |
405 |
|
|
424 |
|
} |
425 |
|
|
426 |
|
/** Pushes the given completion (if it exists) unless done. */ |
427 |
< |
final void push(UniCompletion<?> c) { |
427 |
> |
final void push(UniCompletion<?,?> c) { |
428 |
|
if (c != null) { |
429 |
|
while (result == null && !casStack(c.next = stack, c)) |
430 |
|
c.next = null; // clear on failure |
453 |
|
} |
454 |
|
|
455 |
|
@SuppressWarnings("serial") |
456 |
< |
static final class UniApply<T,U> extends UniCompletion<U> { |
457 |
< |
Function<? super T,? extends U> fn; |
458 |
< |
UniApply(Executor executor, CompletableFuture<U> dep, |
459 |
< |
CompletableFuture<?> src, Function<? super T,? extends U> fn) { |
456 |
> |
static final class UniApply<T,V> extends UniCompletion<T,V> { |
457 |
> |
Function<? super T,? extends V> fn; |
458 |
> |
UniApply(Executor executor, CompletableFuture<V> dep, |
459 |
> |
CompletableFuture<T> src, |
460 |
> |
Function<? super T,? extends V> fn) { |
461 |
|
super(executor, dep, src); this.fn = fn; |
462 |
|
} |
463 |
< |
final CompletableFuture<?> tryFire(int mode) { |
464 |
< |
CompletableFuture<U> d; CompletableFuture<?> a; |
463 |
> |
final CompletableFuture<V> tryFire(int mode) { |
464 |
> |
CompletableFuture<V> d; CompletableFuture<T> a; |
465 |
|
if ((d = dep) == null || |
466 |
|
!d.uniApply(a = src, fn, mode > 0 ? null : this)) |
467 |
|
return null; |
470 |
|
} |
471 |
|
} |
472 |
|
|
473 |
< |
final <S> boolean uniApply(CompletableFuture<?> a, |
473 |
> |
final <S> boolean uniApply(CompletableFuture<S> a, |
474 |
|
Function<? super S,? extends T> f, |
475 |
|
UniApply<S,T> c) { |
476 |
|
Object r; T u; Throwable x; |
501 |
|
return true; |
502 |
|
} |
503 |
|
|
504 |
< |
private <U> CompletableFuture<U> uniApplyStage( |
505 |
< |
Executor e, Function<? super T,? extends U> f) { |
504 |
> |
private <V> CompletableFuture<V> uniApplyStage( |
505 |
> |
Executor e, Function<? super T,? extends V> f) { |
506 |
|
if (f == null) throw new NullPointerException(); |
507 |
< |
CompletableFuture<U> d = new CompletableFuture<U>(); |
507 |
> |
CompletableFuture<V> d = new CompletableFuture<V>(); |
508 |
|
if (e != null || !d.uniApply(this, f, null)) { |
509 |
< |
UniApply<T,U> c = new UniApply<T,U>(e, d, this, f); |
509 |
> |
UniApply<T,V> c = new UniApply<T,V>(e, d, this, f); |
510 |
|
push(c); |
511 |
|
c.tryFire(SYNC); |
512 |
|
} |
514 |
|
} |
515 |
|
|
516 |
|
@SuppressWarnings("serial") |
517 |
< |
static final class UniAccept<T> extends UniCompletion<Void> { |
517 |
> |
static final class UniAccept<T> extends UniCompletion<T,Void> { |
518 |
|
Consumer<? super T> fn; |
519 |
|
UniAccept(Executor executor, CompletableFuture<Void> dep, |
520 |
< |
CompletableFuture<?> src, Consumer<? super T> fn) { |
520 |
> |
CompletableFuture<T> src, Consumer<? super T> fn) { |
521 |
|
super(executor, dep, src); this.fn = fn; |
522 |
|
} |
523 |
< |
final CompletableFuture<?> tryFire(int mode) { |
524 |
< |
CompletableFuture<Void> d; CompletableFuture<?> a; |
523 |
> |
final CompletableFuture<Void> tryFire(int mode) { |
524 |
> |
CompletableFuture<Void> d; CompletableFuture<T> a; |
525 |
|
if ((d = dep) == null || |
526 |
|
!d.uniAccept(a = src, fn, mode > 0 ? null : this)) |
527 |
|
return null; |
530 |
|
} |
531 |
|
} |
532 |
|
|
533 |
< |
final <S> boolean uniAccept(CompletableFuture<?> a, |
533 |
> |
final <S> boolean uniAccept(CompletableFuture<S> a, |
534 |
|
Consumer<? super S> f, UniAccept<S> c) { |
535 |
|
Object r; Throwable x; |
536 |
|
if (a == null || (r = a.result) == null || f == null) |
572 |
|
} |
573 |
|
|
574 |
|
@SuppressWarnings("serial") |
575 |
< |
static final class UniRun extends UniCompletion<Void> { |
575 |
> |
static final class UniRun<T> extends UniCompletion<T,Void> { |
576 |
|
Runnable fn; |
577 |
|
UniRun(Executor executor, CompletableFuture<Void> dep, |
578 |
< |
CompletableFuture<?> src, Runnable fn) { |
578 |
> |
CompletableFuture<T> src, Runnable fn) { |
579 |
|
super(executor, dep, src); this.fn = fn; |
580 |
|
} |
581 |
< |
final CompletableFuture<?> tryFire(int mode) { |
582 |
< |
CompletableFuture<Void> d; CompletableFuture<?> a; |
581 |
> |
final CompletableFuture<Void> tryFire(int mode) { |
582 |
> |
CompletableFuture<Void> d; CompletableFuture<T> a; |
583 |
|
if ((d = dep) == null || |
584 |
|
!d.uniRun(a = src, fn, mode > 0 ? null : this)) |
585 |
|
return null; |
588 |
|
} |
589 |
|
} |
590 |
|
|
591 |
< |
final boolean uniRun(CompletableFuture<?> a, Runnable f, UniRun c) { |
591 |
> |
final boolean uniRun(CompletableFuture<?> a, Runnable f, UniRun<?> c) { |
592 |
|
Object r; Throwable x; |
593 |
|
if (a == null || (r = a.result) == null || f == null) |
594 |
|
return false; |
616 |
|
if (f == null) throw new NullPointerException(); |
617 |
|
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
618 |
|
if (e != null || !d.uniRun(this, f, null)) { |
619 |
< |
UniRun c = new UniRun(e, d, this, f); |
619 |
> |
UniRun<T> c = new UniRun<T>(e, d, this, f); |
620 |
|
push(c); |
621 |
|
c.tryFire(SYNC); |
622 |
|
} |
624 |
|
} |
625 |
|
|
626 |
|
@SuppressWarnings("serial") |
627 |
< |
static final class UniWhenComplete<T> extends UniCompletion<T> { |
627 |
> |
static final class UniWhenComplete<T> extends UniCompletion<T,T> { |
628 |
|
BiConsumer<? super T, ? super Throwable> fn; |
629 |
|
UniWhenComplete(Executor executor, CompletableFuture<T> dep, |
630 |
< |
CompletableFuture<?> src, |
630 |
> |
CompletableFuture<T> src, |
631 |
|
BiConsumer<? super T, ? super Throwable> fn) { |
632 |
|
super(executor, dep, src); this.fn = fn; |
633 |
|
} |
634 |
< |
final CompletableFuture<?> tryFire(int mode) { |
635 |
< |
CompletableFuture<T> d; CompletableFuture<?> a; |
634 |
> |
final CompletableFuture<T> tryFire(int mode) { |
635 |
> |
CompletableFuture<T> d; CompletableFuture<T> a; |
636 |
|
if ((d = dep) == null || |
637 |
|
!d.uniWhenComplete(a = src, fn, mode > 0 ? null : this)) |
638 |
|
return null; |
641 |
|
} |
642 |
|
} |
643 |
|
|
644 |
< |
final boolean uniWhenComplete(CompletableFuture<?> a, |
644 |
> |
final boolean uniWhenComplete(CompletableFuture<T> a, |
645 |
|
BiConsumer<? super T,? super Throwable> f, |
646 |
|
UniWhenComplete<T> c) { |
647 |
|
Object r; |
681 |
|
} |
682 |
|
|
683 |
|
@SuppressWarnings("serial") |
684 |
< |
static final class UniHandle<T,U> extends UniCompletion<U> { |
685 |
< |
BiFunction<? super T, Throwable, ? extends U> fn; |
686 |
< |
UniHandle(Executor executor, CompletableFuture<U> dep, |
687 |
< |
CompletableFuture<?> src, |
688 |
< |
BiFunction<? super T, Throwable, ? extends U> fn) { |
684 |
> |
static final class UniHandle<T,V> extends UniCompletion<T,V> { |
685 |
> |
BiFunction<? super T, Throwable, ? extends V> fn; |
686 |
> |
UniHandle(Executor executor, CompletableFuture<V> dep, |
687 |
> |
CompletableFuture<T> src, |
688 |
> |
BiFunction<? super T, Throwable, ? extends V> fn) { |
689 |
|
super(executor, dep, src); this.fn = fn; |
690 |
|
} |
691 |
< |
final CompletableFuture<?> tryFire(int mode) { |
692 |
< |
CompletableFuture<U> d; CompletableFuture<?> a; |
691 |
> |
final CompletableFuture<V> tryFire(int mode) { |
692 |
> |
CompletableFuture<V> d; CompletableFuture<T> a; |
693 |
|
if ((d = dep) == null || |
694 |
|
!d.uniHandle(a = src, fn, mode > 0 ? null : this)) |
695 |
|
return null; |
698 |
|
} |
699 |
|
} |
700 |
|
|
701 |
< |
final <S> boolean uniHandle(CompletableFuture<?> a, |
701 |
> |
final <S> boolean uniHandle(CompletableFuture<S> a, |
702 |
|
BiFunction<? super S, Throwable, ? extends T> f, |
703 |
|
UniHandle<S,T> c) { |
704 |
|
Object r; T u; Throwable x, y; |
728 |
|
return true; |
729 |
|
} |
730 |
|
|
731 |
< |
private <U> CompletableFuture<U> uniHandleStage( |
732 |
< |
Executor e, BiFunction<? super T, Throwable, ? extends U> f) { |
731 |
> |
private <V> CompletableFuture<V> uniHandleStage( |
732 |
> |
Executor e, BiFunction<? super T, Throwable, ? extends V> f) { |
733 |
|
if (f == null) throw new NullPointerException(); |
734 |
< |
CompletableFuture<U> d = new CompletableFuture<U>(); |
734 |
> |
CompletableFuture<V> d = new CompletableFuture<V>(); |
735 |
|
if (e != null || !d.uniHandle(this, f, null)) { |
736 |
< |
UniHandle<T,U> c = new UniHandle<T,U>(e, d, this, f); |
736 |
> |
UniHandle<T,V> c = new UniHandle<T,V>(e, d, this, f); |
737 |
|
push(c); |
738 |
|
c.tryFire(SYNC); |
739 |
|
} |
741 |
|
} |
742 |
|
|
743 |
|
@SuppressWarnings("serial") |
744 |
< |
static final class UniExceptionally<T> extends UniCompletion<T> { |
744 |
> |
static final class UniExceptionally<T> extends UniCompletion<T,T> { |
745 |
|
Function<? super Throwable, ? extends T> fn; |
746 |
< |
UniExceptionally(CompletableFuture<T> dep, CompletableFuture<?> src, |
746 |
> |
UniExceptionally(CompletableFuture<T> dep, CompletableFuture<T> src, |
747 |
|
Function<? super Throwable, ? extends T> fn) { |
748 |
|
super(null, dep, src); this.fn = fn; |
749 |
|
} |
750 |
< |
final CompletableFuture<?> tryFire(int mode) { // never ASYNC |
751 |
< |
CompletableFuture<T> d; CompletableFuture<?> a; |
750 |
> |
final CompletableFuture<T> tryFire(int mode) { // never ASYNC |
751 |
> |
CompletableFuture<T> d; CompletableFuture<T> a; |
752 |
|
if ((d = dep) == null || !d.uniExceptionally(a = src, fn, this)) |
753 |
|
return null; |
754 |
|
dep = null; src = null; fn = null; |
756 |
|
} |
757 |
|
} |
758 |
|
|
759 |
< |
final boolean uniExceptionally(CompletableFuture<?> a, |
759 |
> |
final boolean uniExceptionally(CompletableFuture<T> a, |
760 |
|
Function<? super Throwable, ? extends T> f, |
761 |
|
UniExceptionally<T> c) { |
762 |
|
Object r; T t; Throwable x, y; |
801 |
|
} |
802 |
|
|
803 |
|
@SuppressWarnings("serial") |
804 |
< |
static final class UniRelay<T> extends UniCompletion<T> { // for Compose |
805 |
< |
UniRelay(CompletableFuture<T> dep, CompletableFuture<?> src) { |
804 |
> |
static final class UniRelay<T> extends UniCompletion<T,T> { // for Compose |
805 |
> |
UniRelay(CompletableFuture<T> dep, CompletableFuture<T> src) { |
806 |
|
super(null, dep, src); |
807 |
|
} |
808 |
< |
final CompletableFuture<?> tryFire(int mode) { |
809 |
< |
CompletableFuture<T> d; CompletableFuture<?> a; |
808 |
> |
final CompletableFuture<T> tryFire(int mode) { |
809 |
> |
CompletableFuture<T> d; CompletableFuture<T> a; |
810 |
|
if ((d = dep) == null || !d.uniRelay(a = src)) |
811 |
|
return null; |
812 |
|
src = null; dep = null; |
814 |
|
} |
815 |
|
} |
816 |
|
|
817 |
< |
final boolean uniRelay(CompletableFuture<?> a) { |
817 |
> |
final boolean uniRelay(CompletableFuture<T> a) { |
818 |
|
Object r; |
819 |
|
if (a == null || (r = a.result) == null) |
820 |
|
return false; |
825 |
|
} |
826 |
|
|
827 |
|
@SuppressWarnings("serial") |
828 |
< |
static final class UniCompose<T,U> extends UniCompletion<U> { |
829 |
< |
Function<? super T, ? extends CompletionStage<U>> fn; |
830 |
< |
UniCompose(Executor executor, CompletableFuture<U> dep, |
831 |
< |
CompletableFuture<?> src, |
832 |
< |
Function<? super T, ? extends CompletionStage<U>> fn) { |
828 |
> |
static final class UniCompose<T,V> extends UniCompletion<T,V> { |
829 |
> |
Function<? super T, ? extends CompletionStage<V>> fn; |
830 |
> |
UniCompose(Executor executor, CompletableFuture<V> dep, |
831 |
> |
CompletableFuture<T> src, |
832 |
> |
Function<? super T, ? extends CompletionStage<V>> fn) { |
833 |
|
super(executor, dep, src); this.fn = fn; |
834 |
|
} |
835 |
< |
final CompletableFuture<?> tryFire(int mode) { |
836 |
< |
CompletableFuture<U> d; CompletableFuture<?> a; |
835 |
> |
final CompletableFuture<V> tryFire(int mode) { |
836 |
> |
CompletableFuture<V> d; CompletableFuture<T> a; |
837 |
|
if ((d = dep) == null || |
838 |
|
!d.uniCompose(a = src, fn, mode > 0 ? null : this)) |
839 |
|
return null; |
843 |
|
} |
844 |
|
|
845 |
|
final <S> boolean uniCompose( |
846 |
< |
CompletableFuture<?> a, |
846 |
> |
CompletableFuture<S> a, |
847 |
|
Function<? super S, ? extends CompletionStage<T>> f, |
848 |
|
UniCompose<S,T> c) { |
849 |
|
Object r; Throwable x; |
882 |
|
return true; |
883 |
|
} |
884 |
|
|
885 |
< |
private <U> CompletableFuture<U> uniComposeStage( |
886 |
< |
Executor e, Function<? super T, ? extends CompletionStage<U>> f) { |
885 |
> |
private <V> CompletableFuture<V> uniComposeStage( |
886 |
> |
Executor e, Function<? super T, ? extends CompletionStage<V>> f) { |
887 |
|
if (f == null) throw new NullPointerException(); |
888 |
|
Object r; Throwable x; |
889 |
|
if (e == null && (r = result) != null) { |
904 |
|
} |
905 |
|
else |
906 |
|
x = null; |
907 |
< |
CompletableFuture<U> d = new CompletableFuture<U>(); |
907 |
> |
CompletableFuture<V> d = new CompletableFuture<V>(); |
908 |
|
if (x == null) { |
909 |
< |
UniCompose<T,U> c = new UniCompose<T,U>(e, d, this, f); |
909 |
> |
UniCompose<T,V> c = new UniCompose<T,V>(e, d, this, f); |
910 |
|
push(c); |
911 |
|
c.tryFire(SYNC); |
912 |
|
} |
919 |
|
|
920 |
|
/** A Completion for an action with two sources */ |
921 |
|
@SuppressWarnings("serial") |
922 |
< |
abstract static class BiCompletion<T> extends UniCompletion<T> { |
923 |
< |
CompletableFuture<?> snd; // second source for action |
924 |
< |
BiCompletion(Executor executor, CompletableFuture<T> dep, |
925 |
< |
CompletableFuture<?> src, CompletableFuture<?> snd) { |
922 |
> |
abstract static class BiCompletion<T,U,V> extends UniCompletion<T,V> { |
923 |
> |
CompletableFuture<U> snd; // second source for action |
924 |
> |
BiCompletion(Executor executor, CompletableFuture<V> dep, |
925 |
> |
CompletableFuture<T> src, CompletableFuture<U> snd) { |
926 |
|
super(executor, dep, src); this.snd = snd; |
927 |
|
} |
928 |
|
} |
930 |
|
/** A Completion delegating to a BiCompletion */ |
931 |
|
@SuppressWarnings("serial") |
932 |
|
static final class CoCompletion extends Completion { |
933 |
< |
BiCompletion<?> base; |
934 |
< |
CoCompletion(BiCompletion<?> base) { this.base = base; } |
933 |
> |
BiCompletion<?,?,?> base; |
934 |
> |
CoCompletion(BiCompletion<?,?,?> base) { this.base = base; } |
935 |
|
final CompletableFuture<?> tryFire(int mode) { |
936 |
< |
BiCompletion<?> c; CompletableFuture<?> d; |
936 |
> |
BiCompletion<?,?,?> c; CompletableFuture<?> d; |
937 |
|
if ((c = base) == null || (d = c.tryFire(mode)) == null) |
938 |
|
return null; |
939 |
|
base = null; // detach |
940 |
|
return d; |
941 |
|
} |
942 |
|
final boolean isLive() { |
943 |
< |
BiCompletion<?> c; |
943 |
> |
BiCompletion<?,?,?> c; |
944 |
|
return (c = base) != null && c.dep != null; |
945 |
|
} |
946 |
|
} |
947 |
|
|
948 |
|
/** Pushes completion to this and b unless both done. */ |
949 |
< |
final void bipush(CompletableFuture<?> b, BiCompletion<?> c) { |
949 |
> |
final void bipush(CompletableFuture<?> b, BiCompletion<?,?,?> c) { |
950 |
|
if (c != null) { |
951 |
|
Object r; |
952 |
|
while ((r = result) == null && !casStack(c.next = stack, c)) |
972 |
|
} |
973 |
|
|
974 |
|
@SuppressWarnings("serial") |
975 |
< |
static final class BiApply<T,U,V> extends BiCompletion<V> { |
975 |
> |
static final class BiApply<T,U,V> extends BiCompletion<T,U,V> { |
976 |
|
BiFunction<? super T,? super U,? extends V> fn; |
977 |
|
BiApply(Executor executor, CompletableFuture<V> dep, |
978 |
< |
CompletableFuture<?> src, CompletableFuture<?> snd, |
978 |
> |
CompletableFuture<T> src, CompletableFuture<U> snd, |
979 |
|
BiFunction<? super T,? super U,? extends V> fn) { |
980 |
|
super(executor, dep, src, snd); this.fn = fn; |
981 |
|
} |
982 |
< |
final CompletableFuture<?> tryFire(int mode) { |
983 |
< |
CompletableFuture<V> d; CompletableFuture<?> a, b; |
982 |
> |
final CompletableFuture<V> tryFire(int mode) { |
983 |
> |
CompletableFuture<V> d; |
984 |
> |
CompletableFuture<T> a; |
985 |
> |
CompletableFuture<U> b; |
986 |
|
if ((d = dep) == null || |
987 |
|
!d.biApply(a = src, b = snd, fn, mode > 0 ? null : this)) |
988 |
|
return null; |
989 |
< |
dep = null; snd = src = null; fn = null; |
989 |
> |
dep = null; src = null; snd = null; fn = null; |
990 |
|
return d.postFire(a, b, mode); |
991 |
|
} |
992 |
|
} |
993 |
|
|
994 |
< |
final <R,S> boolean biApply(CompletableFuture<?> a, CompletableFuture<?> b, |
994 |
> |
final <R,S> boolean biApply(CompletableFuture<R> a, |
995 |
> |
CompletableFuture<S> b, |
996 |
|
BiFunction<? super R,? super S,? extends T> f, |
997 |
|
BiApply<R,S,T> c) { |
998 |
|
Object r, s; T v; Throwable x; |
1030 |
|
} |
1031 |
|
|
1032 |
|
private <U,V> CompletableFuture<V> biApplyStage( |
1033 |
< |
Executor e, CompletionStage<? extends U> o, |
1033 |
> |
Executor e, CompletionStage<U> o, |
1034 |
|
BiFunction<? super T,? super U,? extends V> f) { |
1035 |
< |
CompletableFuture<?> b; |
1035 |
> |
CompletableFuture<U> b; |
1036 |
|
if (f == null || (b = o.toCompletableFuture()) == null) |
1037 |
|
throw new NullPointerException(); |
1038 |
|
CompletableFuture<V> d = new CompletableFuture<V>(); |
1045 |
|
} |
1046 |
|
|
1047 |
|
@SuppressWarnings("serial") |
1048 |
< |
static final class BiAccept<T,U> extends BiCompletion<Void> { |
1048 |
> |
static final class BiAccept<T,U> extends BiCompletion<T,U,Void> { |
1049 |
|
BiConsumer<? super T,? super U> fn; |
1050 |
|
BiAccept(Executor executor, CompletableFuture<Void> dep, |
1051 |
< |
CompletableFuture<?> src, CompletableFuture<?> snd, |
1051 |
> |
CompletableFuture<T> src, CompletableFuture<U> snd, |
1052 |
|
BiConsumer<? super T,? super U> fn) { |
1053 |
|
super(executor, dep, src, snd); this.fn = fn; |
1054 |
|
} |
1055 |
< |
final CompletableFuture<?> tryFire(int mode) { |
1056 |
< |
CompletableFuture<Void> d; CompletableFuture<?> a, b; |
1055 |
> |
final CompletableFuture<Void> tryFire(int mode) { |
1056 |
> |
CompletableFuture<Void> d; |
1057 |
> |
CompletableFuture<T> a; |
1058 |
> |
CompletableFuture<U> b; |
1059 |
|
if ((d = dep) == null || |
1060 |
|
!d.biAccept(a = src, b = snd, fn, mode > 0 ? null : this)) |
1061 |
|
return null; |
1062 |
< |
dep = null; snd = src = null; fn = null; |
1062 |
> |
dep = null; src = null; snd = null; fn = null; |
1063 |
|
return d.postFire(a, b, mode); |
1064 |
|
} |
1065 |
|
} |
1066 |
|
|
1067 |
< |
final <R,S> boolean biAccept(CompletableFuture<?> a, CompletableFuture<?> b, |
1067 |
> |
final <R,S> boolean biAccept(CompletableFuture<R> a, |
1068 |
> |
CompletableFuture<S> b, |
1069 |
|
BiConsumer<? super R,? super S> f, |
1070 |
|
BiAccept<R,S> c) { |
1071 |
|
Object r, s; Throwable x; |
1102 |
|
} |
1103 |
|
|
1104 |
|
private <U> CompletableFuture<Void> biAcceptStage( |
1105 |
< |
Executor e, CompletionStage<? extends U> o, |
1105 |
> |
Executor e, CompletionStage<U> o, |
1106 |
|
BiConsumer<? super T,? super U> f) { |
1107 |
< |
CompletableFuture<?> b; |
1107 |
> |
CompletableFuture<U> b; |
1108 |
|
if (f == null || (b = o.toCompletableFuture()) == null) |
1109 |
|
throw new NullPointerException(); |
1110 |
|
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
1117 |
|
} |
1118 |
|
|
1119 |
|
@SuppressWarnings("serial") |
1120 |
< |
static final class BiRun extends BiCompletion<Void> { |
1120 |
> |
static final class BiRun<T,U> extends BiCompletion<T,U,Void> { |
1121 |
|
Runnable fn; |
1122 |
|
BiRun(Executor executor, CompletableFuture<Void> dep, |
1123 |
< |
CompletableFuture<?> src, CompletableFuture<?> snd, |
1123 |
> |
CompletableFuture<T> src, |
1124 |
> |
CompletableFuture<U> snd, |
1125 |
|
Runnable fn) { |
1126 |
|
super(executor, dep, src, snd); this.fn = fn; |
1127 |
|
} |
1128 |
< |
final CompletableFuture<?> tryFire(int mode) { |
1129 |
< |
CompletableFuture<Void> d; CompletableFuture<?> a, b; |
1128 |
> |
final CompletableFuture<Void> tryFire(int mode) { |
1129 |
> |
CompletableFuture<Void> d; |
1130 |
> |
CompletableFuture<T> a; |
1131 |
> |
CompletableFuture<U> b; |
1132 |
|
if ((d = dep) == null || |
1133 |
|
!d.biRun(a = src, b = snd, fn, mode > 0 ? null : this)) |
1134 |
|
return null; |
1135 |
< |
dep = null; snd = src = null; fn = null; |
1135 |
> |
dep = null; src = null; snd = null; fn = null; |
1136 |
|
return d.postFire(a, b, mode); |
1137 |
|
} |
1138 |
|
} |
1139 |
|
|
1140 |
|
final boolean biRun(CompletableFuture<?> a, CompletableFuture<?> b, |
1141 |
< |
Runnable f, BiRun c) { |
1141 |
> |
Runnable f, BiRun<?,?> c) { |
1142 |
|
Object r, s; Throwable x; |
1143 |
|
if (a == null || (r = a.result) == null || |
1144 |
|
b == null || (s = b.result) == null || f == null) |
1172 |
|
throw new NullPointerException(); |
1173 |
|
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
1174 |
|
if (e != null || !d.biRun(this, b, f, null)) { |
1175 |
< |
BiRun c = new BiRun(e, d, this, b, f); |
1175 |
> |
BiRun<T,?> c = new BiRun<>(e, d, this, b, f); |
1176 |
|
bipush(b, c); |
1177 |
|
c.tryFire(SYNC); |
1178 |
|
} |
1180 |
|
} |
1181 |
|
|
1182 |
|
@SuppressWarnings("serial") |
1183 |
< |
static final class BiRelay extends BiCompletion<Void> { // for And |
1184 |
< |
BiRelay(CompletableFuture<Void> dep, CompletableFuture<?> src, |
1185 |
< |
CompletableFuture<?> snd) { |
1183 |
> |
static final class BiRelay<T,U> extends BiCompletion<T,U,Void> { // for And |
1184 |
> |
BiRelay(CompletableFuture<Void> dep, |
1185 |
> |
CompletableFuture<T> src, |
1186 |
> |
CompletableFuture<U> snd) { |
1187 |
|
super(null, dep, src, snd); |
1188 |
|
} |
1189 |
< |
final CompletableFuture<?> tryFire(int mode) { |
1190 |
< |
CompletableFuture<Void> d; CompletableFuture<?> a, b; |
1189 |
> |
final CompletableFuture<Void> tryFire(int mode) { |
1190 |
> |
CompletableFuture<Void> d; |
1191 |
> |
CompletableFuture<T> a; |
1192 |
> |
CompletableFuture<U> b; |
1193 |
|
if ((d = dep) == null || !d.biRelay(a = src, b = snd)) |
1194 |
|
return null; |
1195 |
< |
snd = src = null; dep = null; |
1195 |
> |
src = null; snd = null; dep = null; |
1196 |
|
return d.postFire(a, b, mode); |
1197 |
|
} |
1198 |
|
} |
1229 |
|
andTree(cfs, mid+1, hi))) == null) |
1230 |
|
throw new NullPointerException(); |
1231 |
|
if (!d.biRelay(a, b)) { |
1232 |
< |
BiRelay c = new BiRelay(d, a, b); |
1232 |
> |
BiRelay<?,?> c = new BiRelay<>(d, a, b); |
1233 |
|
a.bipush(b, c); |
1234 |
|
c.tryFire(SYNC); |
1235 |
|
} |
1240 |
|
/* ------------- Projected (Ored) BiCompletions -------------- */ |
1241 |
|
|
1242 |
|
/** Pushes completion to this and b unless either done. */ |
1243 |
< |
final void orpush(CompletableFuture<?> b, BiCompletion<?> c) { |
1243 |
> |
final void orpush(CompletableFuture<?> b, BiCompletion<?,?,?> c) { |
1244 |
|
if (c != null) { |
1245 |
|
while ((b == null || b.result == null) && result == null) { |
1246 |
|
if (casStack(c.next = stack, c)) { |
1258 |
|
} |
1259 |
|
|
1260 |
|
@SuppressWarnings("serial") |
1261 |
< |
static final class OrApply<T,U> extends BiCompletion<U> { |
1262 |
< |
Function<? super T,? extends U> fn; |
1263 |
< |
OrApply(Executor executor, CompletableFuture<U> dep, |
1264 |
< |
CompletableFuture<?> src, CompletableFuture<?> snd, |
1265 |
< |
Function<? super T,? extends U> fn) { |
1261 |
> |
static final class OrApply<T,U extends T,V> extends BiCompletion<T,U,V> { |
1262 |
> |
Function<? super T,? extends V> fn; |
1263 |
> |
OrApply(Executor executor, CompletableFuture<V> dep, |
1264 |
> |
CompletableFuture<T> src, |
1265 |
> |
CompletableFuture<U> snd, |
1266 |
> |
Function<? super T,? extends V> fn) { |
1267 |
|
super(executor, dep, src, snd); this.fn = fn; |
1268 |
|
} |
1269 |
< |
final CompletableFuture<?> tryFire(int mode) { |
1270 |
< |
CompletableFuture<U> d; CompletableFuture<?> a, b; |
1269 |
> |
final CompletableFuture<V> tryFire(int mode) { |
1270 |
> |
CompletableFuture<V> d; |
1271 |
> |
CompletableFuture<T> a; |
1272 |
> |
CompletableFuture<U> b; |
1273 |
|
if ((d = dep) == null || |
1274 |
|
!d.orApply(a = src, b = snd, fn, mode > 0 ? null : this)) |
1275 |
|
return null; |
1276 |
< |
dep = null; snd = src = null; fn = null; |
1276 |
> |
dep = null; src = null; snd = null; fn = null; |
1277 |
|
return d.postFire(a, b, mode); |
1278 |
|
} |
1279 |
|
} |
1280 |
|
|
1281 |
< |
final <S> boolean orApply(CompletableFuture<?> a, CompletableFuture<?> b, |
1282 |
< |
Function<? super S, ? extends T> f, |
1283 |
< |
OrApply<S,T> c) { |
1281 |
> |
final <R,S extends R> boolean orApply(CompletableFuture<R> a, |
1282 |
> |
CompletableFuture<S> b, |
1283 |
> |
Function<? super R, ? extends T> f, |
1284 |
> |
OrApply<R,S,T> c) { |
1285 |
|
Object r; T u; Throwable x; |
1286 |
|
if (a == null || b == null || |
1287 |
|
((r = a.result) == null && (r = b.result) == null) || f == null) |
1299 |
|
else if (c != null && !c.claim()) |
1300 |
|
return false; |
1301 |
|
else { |
1302 |
< |
@SuppressWarnings("unchecked") S t = (S) r; |
1302 |
> |
@SuppressWarnings("unchecked") R t = (R) r; |
1303 |
|
u = f.apply(t); |
1304 |
|
} |
1305 |
|
} catch (Throwable ex) { |
1311 |
|
return true; |
1312 |
|
} |
1313 |
|
|
1314 |
< |
private <T,U> CompletableFuture<U> orApplyStage( |
1315 |
< |
Executor e, CompletionStage<? extends T> o, Function<? super T, U> f) { |
1316 |
< |
CompletableFuture<?> b; |
1314 |
> |
private <U extends T,V> CompletableFuture<V> orApplyStage( |
1315 |
> |
Executor e, CompletionStage<U> o, |
1316 |
> |
Function<? super T, ? extends V> f) { |
1317 |
> |
CompletableFuture<U> b; |
1318 |
|
if (f == null || (b = o.toCompletableFuture()) == null) |
1319 |
|
throw new NullPointerException(); |
1320 |
< |
CompletableFuture<U> d = new CompletableFuture<U>(); |
1320 |
> |
CompletableFuture<V> d = new CompletableFuture<V>(); |
1321 |
|
if (e != null || !d.orApply(this, b, f, null)) { |
1322 |
< |
OrApply<T,U> c = new OrApply<T,U>(e, d, this, b, f); |
1322 |
> |
OrApply<T,U,V> c = new OrApply<T,U,V>(e, d, this, b, f); |
1323 |
|
orpush(b, c); |
1324 |
|
c.tryFire(SYNC); |
1325 |
|
} |
1327 |
|
} |
1328 |
|
|
1329 |
|
@SuppressWarnings("serial") |
1330 |
< |
static final class OrAccept<T> extends BiCompletion<Void> { |
1330 |
> |
static final class OrAccept<T,U extends T> extends BiCompletion<T,U,Void> { |
1331 |
|
Consumer<? super T> fn; |
1332 |
|
OrAccept(Executor executor, CompletableFuture<Void> dep, |
1333 |
< |
CompletableFuture<?> src, CompletableFuture<?> snd, |
1333 |
> |
CompletableFuture<T> src, |
1334 |
> |
CompletableFuture<U> snd, |
1335 |
|
Consumer<? super T> fn) { |
1336 |
|
super(executor, dep, src, snd); this.fn = fn; |
1337 |
|
} |
1338 |
< |
final CompletableFuture<?> tryFire(int mode) { |
1339 |
< |
CompletableFuture<Void> d; CompletableFuture<?> a, b; |
1338 |
> |
final CompletableFuture<Void> tryFire(int mode) { |
1339 |
> |
CompletableFuture<Void> d; |
1340 |
> |
CompletableFuture<T> a; |
1341 |
> |
CompletableFuture<U> b; |
1342 |
|
if ((d = dep) == null || |
1343 |
|
!d.orAccept(a = src, b = snd, fn, mode > 0 ? null : this)) |
1344 |
|
return null; |
1345 |
< |
dep = null; snd = src = null; fn = null; |
1345 |
> |
dep = null; src = null; snd = null; fn = null; |
1346 |
|
return d.postFire(a, b, mode); |
1347 |
|
} |
1348 |
|
} |
1349 |
|
|
1350 |
< |
final <S> boolean orAccept(CompletableFuture<?> a, |
1351 |
< |
CompletableFuture<?> b, |
1352 |
< |
Consumer<? super S> f, OrAccept<S> c) { |
1350 |
> |
final <R,S extends R> boolean orAccept(CompletableFuture<R> a, |
1351 |
> |
CompletableFuture<S> b, |
1352 |
> |
Consumer<? super R> f, |
1353 |
> |
OrAccept<R,S> c) { |
1354 |
|
Object r; Throwable x; |
1355 |
|
if (a == null || b == null || |
1356 |
|
((r = a.result) == null && (r = b.result) == null) || f == null) |
1367 |
|
if (c != null && !c.claim()) |
1368 |
|
return false; |
1369 |
|
else { |
1370 |
< |
@SuppressWarnings("unchecked") S t = (S) r; |
1370 |
> |
@SuppressWarnings("unchecked") R t = (R) r; |
1371 |
|
f.accept(t); |
1372 |
|
} |
1373 |
|
} |
1379 |
|
return true; |
1380 |
|
} |
1381 |
|
|
1382 |
< |
private CompletableFuture<Void> orAcceptStage( |
1383 |
< |
Executor e, CompletionStage<? extends T> o, Consumer<? super T> f) { |
1384 |
< |
CompletableFuture<?> b; |
1382 |
> |
private <U extends T> CompletableFuture<Void> orAcceptStage( |
1383 |
> |
Executor e, CompletionStage<U> o, Consumer<? super T> f) { |
1384 |
> |
CompletableFuture<U> b; |
1385 |
|
if (f == null || (b = o.toCompletableFuture()) == null) |
1386 |
|
throw new NullPointerException(); |
1387 |
|
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
1388 |
|
if (e != null || !d.orAccept(this, b, f, null)) { |
1389 |
< |
OrAccept<T> c = new OrAccept<T>(e, d, this, b, f); |
1389 |
> |
OrAccept<T,U> c = new OrAccept<T,U>(e, d, this, b, f); |
1390 |
|
orpush(b, c); |
1391 |
|
c.tryFire(SYNC); |
1392 |
|
} |
1394 |
|
} |
1395 |
|
|
1396 |
|
@SuppressWarnings("serial") |
1397 |
< |
static final class OrRun extends BiCompletion<Void> { |
1397 |
> |
static final class OrRun<T,U> extends BiCompletion<T,U,Void> { |
1398 |
|
Runnable fn; |
1399 |
|
OrRun(Executor executor, CompletableFuture<Void> dep, |
1400 |
< |
CompletableFuture<?> src, CompletableFuture<?> snd, Runnable fn) { |
1400 |
> |
CompletableFuture<T> src, |
1401 |
> |
CompletableFuture<U> snd, |
1402 |
> |
Runnable fn) { |
1403 |
|
super(executor, dep, src, snd); this.fn = fn; |
1404 |
|
} |
1405 |
< |
final CompletableFuture<?> tryFire(int mode) { |
1406 |
< |
CompletableFuture<Void> d; CompletableFuture<?> a, b; |
1405 |
> |
final CompletableFuture<Void> tryFire(int mode) { |
1406 |
> |
CompletableFuture<Void> d; |
1407 |
> |
CompletableFuture<T> a; |
1408 |
> |
CompletableFuture<U> b; |
1409 |
|
if ((d = dep) == null || |
1410 |
|
!d.orRun(a = src, b = snd, fn, mode > 0 ? null : this)) |
1411 |
|
return null; |
1412 |
< |
dep = null; snd = src = null; fn = null; |
1412 |
> |
dep = null; src = null; snd = null; fn = null; |
1413 |
|
return d.postFire(a, b, mode); |
1414 |
|
} |
1415 |
|
} |
1416 |
|
|
1417 |
|
final boolean orRun(CompletableFuture<?> a, CompletableFuture<?> b, |
1418 |
< |
Runnable f, OrRun c) { |
1418 |
> |
Runnable f, OrRun<?,?> c) { |
1419 |
|
Object r; Throwable x; |
1420 |
|
if (a == null || b == null || |
1421 |
|
((r = a.result) == null && (r = b.result) == null) || f == null) |
1447 |
|
throw new NullPointerException(); |
1448 |
|
CompletableFuture<Void> d = new CompletableFuture<Void>(); |
1449 |
|
if (e != null || !d.orRun(this, b, f, null)) { |
1450 |
< |
OrRun c = new OrRun(e, d, this, b, f); |
1450 |
> |
OrRun<T,?> c = new OrRun<>(e, d, this, b, f); |
1451 |
|
orpush(b, c); |
1452 |
|
c.tryFire(SYNC); |
1453 |
|
} |
1455 |
|
} |
1456 |
|
|
1457 |
|
@SuppressWarnings("serial") |
1458 |
< |
static final class OrRelay extends BiCompletion<Object> { // for Or |
1459 |
< |
OrRelay(CompletableFuture<Object> dep, CompletableFuture<?> src, |
1460 |
< |
CompletableFuture<?> snd) { |
1458 |
> |
static final class OrRelay<T,U> extends BiCompletion<T,U,Object> { // for Or |
1459 |
> |
OrRelay(CompletableFuture<Object> dep, CompletableFuture<T> src, |
1460 |
> |
CompletableFuture<U> snd) { |
1461 |
|
super(null, dep, src, snd); |
1462 |
|
} |
1463 |
< |
final CompletableFuture<?> tryFire(int mode) { |
1464 |
< |
CompletableFuture<Object> d; CompletableFuture<?> a, b; |
1463 |
> |
final CompletableFuture<Object> tryFire(int mode) { |
1464 |
> |
CompletableFuture<Object> d; |
1465 |
> |
CompletableFuture<T> a; |
1466 |
> |
CompletableFuture<U> b; |
1467 |
|
if ((d = dep) == null || !d.orRelay(a = src, b = snd)) |
1468 |
|
return null; |
1469 |
< |
snd = src = null; dep = null; |
1469 |
> |
src = null; snd = null; dep = null; |
1470 |
|
return d.postFire(a, b, mode); |
1471 |
|
} |
1472 |
|
} |
1495 |
|
orTree(cfs, mid+1, hi))) == null) |
1496 |
|
throw new NullPointerException(); |
1497 |
|
if (!d.orRelay(a, b)) { |
1498 |
< |
OrRelay c = new OrRelay(d, a, b); |
1498 |
> |
OrRelay<?,?> c = new OrRelay<>(d, a, b); |
1499 |
|
a.orpush(b, c); |
1500 |
|
c.tryFire(SYNC); |
1501 |
|
} |
1512 |
|
this.dep = dep; this.fn = fn; |
1513 |
|
} |
1514 |
|
|
1515 |
< |
final CompletableFuture<?> tryFire(int alwaysAsync) { |
1515 |
> |
final CompletableFuture<T> tryFire(int alwaysAsync) { |
1516 |
|
CompletableFuture<T> d; Supplier<T> f; |
1517 |
|
if ((d = dep) != null && (f = fn) != null) { |
1518 |
|
dep = null; fn = null; |
1549 |
|
this.dep = dep; this.fn = fn; |
1550 |
|
} |
1551 |
|
|
1552 |
< |
final CompletableFuture<?> tryFire(int alwaysAsync) { |
1552 |
> |
final CompletableFuture<Void> tryFire(int alwaysAsync) { |
1553 |
|
CompletableFuture<Void> d; Runnable f; |
1554 |
|
if ((d = dep) != null && (f = fn) != null) { |
1555 |
|
dep = null; fn = null; |