ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CompletableFuture.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/CompletableFuture.java (file contents):
Revision 1.199 by jsr166, Wed Jun 22 00:25:56 2016 UTC vs.
Revision 1.200 by jsr166, Sat Jun 25 15:11:11 2016 UTC

# Line 572 | Line 572 | public class CompletableFuture<T> implem
572          }
573          final CompletableFuture<V> tryFire(int mode) {
574              CompletableFuture<V> d; CompletableFuture<T> a;
575 <            if ((d = dep) == null ||
576 <                !d.uniApply(a = src, fn, mode > 0 ? null : this))
575 >            Object r; Throwable x; Function<? super T,? extends V> f;
576 >            if ((a = src) == null
577 >                || (r = a.result) == null
578 >                || (d = dep) == null
579 >                || (f = fn) == null)
580                  return null;
581 <            dep = null; src = null; fn = null;
582 <            return d.postFire(a, mode);
583 <        }
584 <    }
585 <
586 <    final <S> boolean uniApply(CompletableFuture<S> a,
587 <                               Function<? super S,? extends T> f,
588 <                               UniApply<S,T> c) {
589 <        Object r; Throwable x;
590 <        if (a == null || (r = a.result) == null || f == null)
591 <            return false;
592 <        tryComplete: if (result == null) {
593 <            if (r instanceof AltResult) {
594 <                if ((x = ((AltResult)r).ex) != null) {
595 <                    completeThrowable(x, r);
596 <                    break tryComplete;
581 >            tryComplete: if (d.result == null) {
582 >                if (r instanceof AltResult) {
583 >                    if ((x = ((AltResult)r).ex) != null) {
584 >                        d.completeThrowable(x, r);
585 >                        break tryComplete;
586 >                    }
587 >                    r = null;
588 >                }
589 >                try {
590 >                    if (mode <= 0 && !claim())
591 >                        return null;
592 >                    else {
593 >                        @SuppressWarnings("unchecked") T t = (T) r;
594 >                        d.completeValue(f.apply(t));
595 >                    }
596 >                } catch (Throwable ex) {
597 >                    d.completeThrowable(ex);
598                  }
595                r = null;
596            }
597            try {
598                if (c != null && !c.claim())
599                    return false;
600                @SuppressWarnings("unchecked") S s = (S) r;
601                completeValue(f.apply(s));
602            } catch (Throwable ex) {
603                completeThrowable(ex);
599              }
600 +            dep = null; src = null; fn = null;
601 +            return d.postFire(a, mode);
602          }
606        return true;
603      }
604  
605      private <V> CompletableFuture<V> uniApplyStage(
606          Executor e, Function<? super T,? extends V> f) {
607          if (f == null) throw new NullPointerException();
608 +        Object r;
609 +        if ((r = result) != null)
610 +            return uniApplyNow(r, e, f);
611          CompletableFuture<V> d = newIncompleteFuture();
612 <        if (e != null || !d.uniApply(this, f, null)) {
613 <            UniApply<T,V> c = new UniApply<T,V>(e, d, this, f);
614 <            if (e != null && result != null) {
615 <                try {
616 <                    e.execute(c);
617 <                } catch (Throwable ex) {
618 <                    d.completeThrowable(ex);
619 <                }
612 >        unipush(new UniApply<T,V>(e, d, this, f));
613 >        return d;
614 >    }
615 >
616 >    private <V> CompletableFuture<V> uniApplyNow(
617 >        Object r, Executor e, Function<? super T,? extends V> f) {
618 >        Throwable x;
619 >        CompletableFuture<V> d = newIncompleteFuture();
620 >        if (r instanceof AltResult) {
621 >            if ((x = ((AltResult)r).ex) != null) {
622 >                d.completeThrowable(x, r);
623 >                return d;
624              }
625 <            else {
626 <                unipush(c);
625 >            r = null;
626 >        }
627 >        try {
628 >            if (e != null) {
629 >                e.execute(new UniApply<T,V>(null, d, this, f));
630 >            } else {
631 >                @SuppressWarnings("unchecked") T t = (T) r;
632 >                d.completeValue(f.apply(t));
633              }
634 +        } catch (Throwable ex) {
635 +            d.completeThrowable(ex);
636          }
637          return d;
638      }
# Line 635 | Line 646 | public class CompletableFuture<T> implem
646          }
647          final CompletableFuture<Void> tryFire(int mode) {
648              CompletableFuture<Void> d; CompletableFuture<T> a;
649 <            if ((d = dep) == null ||
650 <                !d.uniAccept(a = src, fn, mode > 0 ? null : this))
649 >            Object r; Throwable x; Consumer<? super T> f;
650 >            if ((a = src) == null
651 >                || (r = a.result) == null
652 >                || (d = dep) == null
653 >                || (f = fn) == null)
654                  return null;
655 <            dep = null; src = null; fn = null;
656 <            return d.postFire(a, mode);
657 <        }
658 <    }
659 <
660 <    final <S> boolean uniAccept(CompletableFuture<S> a,
661 <                                Consumer<? super S> f, UniAccept<S> c) {
662 <        Object r; Throwable x;
663 <        if (a == null || (r = a.result) == null || f == null)
664 <            return false;
665 <        tryComplete: if (result == null) {
666 <            if (r instanceof AltResult) {
667 <                if ((x = ((AltResult)r).ex) != null) {
668 <                    completeThrowable(x, r);
669 <                    break tryComplete;
655 >            tryComplete: if (d.result == null) {
656 >                if (r instanceof AltResult) {
657 >                    if ((x = ((AltResult)r).ex) != null) {
658 >                        d.completeThrowable(x, r);
659 >                        break tryComplete;
660 >                    }
661 >                    r = null;
662 >                }
663 >                try {
664 >                    if (mode <= 0 && !claim())
665 >                        return null;
666 >                    else {
667 >                        @SuppressWarnings("unchecked") T t = (T) r;
668 >                        f.accept(t);
669 >                        d.completeNull();
670 >                    }
671 >                } catch (Throwable ex) {
672 >                    d.completeThrowable(ex);
673                  }
657                r = null;
658            }
659            try {
660                if (c != null && !c.claim())
661                    return false;
662                @SuppressWarnings("unchecked") S s = (S) r;
663                f.accept(s);
664                completeNull();
665            } catch (Throwable ex) {
666                completeThrowable(ex);
674              }
675 +            dep = null; src = null; fn = null;
676 +            return d.postFire(a, mode);
677          }
669        return true;
678      }
679  
680      private CompletableFuture<Void> uniAcceptStage(Executor e,
681                                                     Consumer<? super T> f) {
682          if (f == null) throw new NullPointerException();
683 +        Object r;
684 +        if ((r = result) != null)
685 +            return uniAcceptNow(r, e, f);
686          CompletableFuture<Void> d = newIncompleteFuture();
687 <        if (e != null || !d.uniAccept(this, f, null)) {
688 <            UniAccept<T> c = new UniAccept<T>(e, d, this, f);
689 <            if (e != null && result != null) {
690 <                try {
691 <                    e.execute(c);
692 <                } catch (Throwable ex) {
693 <                    d.completeThrowable(ex);
694 <                }
687 >        unipush(new UniAccept<T>(e, d, this, f));
688 >        return d;
689 >    }
690 >
691 >    private CompletableFuture<Void> uniAcceptNow(
692 >        Object r, Executor e, Consumer<? super T> f) {
693 >        Throwable x;
694 >        CompletableFuture<Void> d = newIncompleteFuture();
695 >        if (r instanceof AltResult) {
696 >            if ((x = ((AltResult)r).ex) != null) {
697 >                d.completeThrowable(x, r);
698 >                return d;
699              }
700 <            else {
701 <                unipush(c);
700 >            r = null;
701 >        }
702 >        try {
703 >            if (e != null) {
704 >                e.execute(new UniAccept<T>(null, d, this, f));
705 >            } else {
706 >                @SuppressWarnings("unchecked") T t = (T) r;
707 >                f.accept(t);
708 >                d.completeNull();
709              }
710 +        } catch (Throwable ex) {
711 +            d.completeThrowable(ex);
712          }
713          return d;
714      }
# Line 698 | Line 722 | public class CompletableFuture<T> implem
722          }
723          final CompletableFuture<Void> tryFire(int mode) {
724              CompletableFuture<Void> d; CompletableFuture<T> a;
725 <            if ((d = dep) == null ||
726 <                !d.uniRun(a = src, fn, mode > 0 ? null : this))
725 >            Object r; Throwable x; Runnable f;
726 >            if ((a = src) == null
727 >                || (r = a.result) == null
728 >                || (d = dep) == null
729 >                || (f = fn) == null)
730                  return null;
731 +            if (d.result == null) {
732 +                if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
733 +                    d.completeThrowable(x, r);
734 +                else
735 +                    try {
736 +                        if (mode <= 0 && !claim())
737 +                            return null;
738 +                        else {
739 +                            f.run();
740 +                            d.completeNull();
741 +                        }
742 +                    } catch (Throwable ex) {
743 +                        d.completeThrowable(ex);
744 +                    }
745 +            }
746              dep = null; src = null; fn = null;
747              return d.postFire(a, mode);
748          }
749      }
750  
709    final boolean uniRun(CompletableFuture<?> a, Runnable f, UniRun<?> c) {
710        Object r; Throwable x;
711        if (a == null || (r = a.result) == null || f == null)
712            return false;
713        if (result == null) {
714            if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
715                completeThrowable(x, r);
716            else
717                try {
718                    if (c != null && !c.claim())
719                        return false;
720                    f.run();
721                    completeNull();
722                } catch (Throwable ex) {
723                    completeThrowable(ex);
724                }
725        }
726        return true;
727    }
728
751      private CompletableFuture<Void> uniRunStage(Executor e, Runnable f) {
752          if (f == null) throw new NullPointerException();
753 +        Object r;
754 +        if ((r = result) != null)
755 +            return uniRunNow(r, e, f);
756          CompletableFuture<Void> d = newIncompleteFuture();
757 <        if (e != null || !d.uniRun(this, f, null)) {
758 <            UniRun<T> c = new UniRun<T>(e, d, this, f);
759 <            if (e != null && result != null) {
760 <                try {
761 <                    e.execute(c);
762 <                } catch (Throwable ex) {
763 <                    d.completeThrowable(ex);
757 >        unipush(new UniRun<T>(e, d, this, f));
758 >        return d;
759 >    }
760 >
761 >    private CompletableFuture<Void> uniRunNow(Object r, Executor e, Runnable f) {
762 >        Throwable x;
763 >        CompletableFuture<Void> d = newIncompleteFuture();
764 >        if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
765 >            d.completeThrowable(x, r);
766 >        else
767 >            try {
768 >                if (e != null) {
769 >                    e.execute(new UniRun<T>(null, d, this, f));
770 >                } else {
771 >                    f.run();
772 >                    d.completeNull();
773                  }
774 +            } catch (Throwable ex) {
775 +                d.completeThrowable(ex);
776              }
741            else {
742                unipush(c);
743            }
744        }
777          return d;
778      }
779  
# Line 1438 | Line 1470 | public class CompletableFuture<T> implem
1470              CompletableFuture<V> d;
1471              CompletableFuture<T> a;
1472              CompletableFuture<U> b;
1473 <            if ((d = dep) == null ||
1474 <                !d.orApply(a = src, b = snd, fn, mode > 0 ? null : this))
1473 >            Object r; Throwable x; Function<? super T,? extends V> f;
1474 >            if ((a = src) == null
1475 >                || (b = snd) == null
1476 >                || ((r = a.result) == null && (r = b.result) == null)
1477 >                || (d = dep) == null
1478 >                || (f = fn) == null)
1479                  return null;
1480 <            dep = null; src = null; snd = null; fn = null;
1481 <            return d.postFire(a, b, mode);
1482 <        }
1483 <    }
1484 <
1485 <    final <R,S extends R> boolean orApply(CompletableFuture<R> a,
1486 <                                          CompletableFuture<S> b,
1487 <                                          Function<? super R, ? extends T> f,
1488 <                                          OrApply<R,S,T> c) {
1489 <        Object r; Throwable x;
1454 <        if (a == null || b == null ||
1455 <            ((r = a.result) == null && (r = b.result) == null) || f == null)
1456 <            return false;
1457 <        tryComplete: if (result == null) {
1458 <            try {
1459 <                if (c != null && !c.claim())
1460 <                    return false;
1461 <                if (r instanceof AltResult) {
1462 <                    if ((x = ((AltResult)r).ex) != null) {
1463 <                        completeThrowable(x, r);
1464 <                        break tryComplete;
1480 >            tryComplete: if (d.result == null) {
1481 >                try {
1482 >                    if (mode <= 0 && !claim())
1483 >                        return null;
1484 >                    if (r instanceof AltResult) {
1485 >                        if ((x = ((AltResult)r).ex) != null) {
1486 >                            d.completeThrowable(x, r);
1487 >                            break tryComplete;
1488 >                        }
1489 >                        r = null;
1490                      }
1491 <                    r = null;
1491 >                    @SuppressWarnings("unchecked") T t = (T) r;
1492 >                    d.completeValue(f.apply(t));
1493 >                } catch (Throwable ex) {
1494 >                    d.completeThrowable(ex);
1495                  }
1468                @SuppressWarnings("unchecked") R rr = (R) r;
1469                completeValue(f.apply(rr));
1470            } catch (Throwable ex) {
1471                completeThrowable(ex);
1496              }
1497 +            dep = null; src = null; snd = null; fn = null;
1498 +            return d.postFire(a, b, mode);
1499          }
1474        return true;
1500      }
1501  
1502      private <U extends T,V> CompletableFuture<V> orApplyStage(
1503 <        Executor e, CompletionStage<U> o,
1479 <        Function<? super T, ? extends V> f) {
1503 >        Executor e, CompletionStage<U> o, Function<? super T, ? extends V> f) {
1504          CompletableFuture<U> b;
1505          if (f == null || (b = o.toCompletableFuture()) == null)
1506              throw new NullPointerException();
1507 +
1508 +        Object r; CompletableFuture<? extends T> z;
1509 +        if ((r = (z = this).result) != null ||
1510 +            (r = (z = b).result) != null)
1511 +            return z.uniApplyNow(r, e, f);
1512 +
1513          CompletableFuture<V> d = newIncompleteFuture();
1514 <        if (e != null || !d.orApply(this, b, f, null)) {
1485 <            OrApply<T,U,V> c = new OrApply<T,U,V>(e, d, this, b, f);
1486 <            if (e != null && (result != null || b.result != null)) {
1487 <                try {
1488 <                    e.execute(c);
1489 <                } catch (Throwable ex) {
1490 <                    d.completeThrowable(ex);
1491 <                }
1492 <            }
1493 <            else {
1494 <                orpush(b, c);
1495 <            }
1496 <        }
1514 >        orpush(b, new OrApply<T,U,V>(e, d, this, b, f));
1515          return d;
1516      }
1517  
# Line 1510 | Line 1528 | public class CompletableFuture<T> implem
1528              CompletableFuture<Void> d;
1529              CompletableFuture<T> a;
1530              CompletableFuture<U> b;
1531 <            if ((d = dep) == null ||
1532 <                !d.orAccept(a = src, b = snd, fn, mode > 0 ? null : this))
1531 >            Object r; Throwable x; Consumer<? super T> f;
1532 >            if ((a = src) == null
1533 >                || (b = snd) == null
1534 >                || ((r = a.result) == null && (r = b.result) == null)
1535 >                || (d = dep) == null
1536 >                || (f = fn) == null)
1537                  return null;
1538 <            dep = null; src = null; snd = null; fn = null;
1539 <            return d.postFire(a, b, mode);
1540 <        }
1541 <    }
1542 <
1543 <    final <R,S extends R> boolean orAccept(CompletableFuture<R> a,
1544 <                                           CompletableFuture<S> b,
1545 <                                           Consumer<? super R> f,
1546 <                                           OrAccept<R,S> c) {
1547 <        Object r; Throwable x;
1526 <        if (a == null || b == null ||
1527 <            ((r = a.result) == null && (r = b.result) == null) || f == null)
1528 <            return false;
1529 <        tryComplete: if (result == null) {
1530 <            try {
1531 <                if (c != null && !c.claim())
1532 <                    return false;
1533 <                if (r instanceof AltResult) {
1534 <                    if ((x = ((AltResult)r).ex) != null) {
1535 <                        completeThrowable(x, r);
1536 <                        break tryComplete;
1538 >            tryComplete: if (d.result == null) {
1539 >                try {
1540 >                    if (mode <= 0 && !claim())
1541 >                        return null;
1542 >                    if (r instanceof AltResult) {
1543 >                        if ((x = ((AltResult)r).ex) != null) {
1544 >                            d.completeThrowable(x, r);
1545 >                            break tryComplete;
1546 >                        }
1547 >                        r = null;
1548                      }
1549 <                    r = null;
1549 >                    @SuppressWarnings("unchecked") T t = (T) r;
1550 >                    f.accept(t);
1551 >                    d.completeNull();
1552 >                } catch (Throwable ex) {
1553 >                    d.completeThrowable(ex);
1554                  }
1540                @SuppressWarnings("unchecked") R rr = (R) r;
1541                f.accept(rr);
1542                completeNull();
1543            } catch (Throwable ex) {
1544                completeThrowable(ex);
1555              }
1556 +            dep = null; src = null; snd = null; fn = null;
1557 +            return d.postFire(a, b, mode);
1558          }
1547        return true;
1559      }
1560  
1561      private <U extends T> CompletableFuture<Void> orAcceptStage(
# Line 1552 | Line 1563 | public class CompletableFuture<T> implem
1563          CompletableFuture<U> b;
1564          if (f == null || (b = o.toCompletableFuture()) == null)
1565              throw new NullPointerException();
1566 +
1567 +        Object r; CompletableFuture<? extends T> z;
1568 +        if ((r = (z = this).result) != null ||
1569 +            (r = (z = b).result) != null)
1570 +            return z.uniAcceptNow(r, e, f);
1571 +
1572          CompletableFuture<Void> d = newIncompleteFuture();
1573 <        if (e != null || !d.orAccept(this, b, f, null)) {
1557 <            OrAccept<T,U> c = new OrAccept<T,U>(e, d, this, b, f);
1558 <            if (e != null && (result != null || b.result != null)) {
1559 <                try {
1560 <                    e.execute(c);
1561 <                } catch (Throwable ex) {
1562 <                    d.completeThrowable(ex);
1563 <                }
1564 <            }
1565 <            else {
1566 <                orpush(b, c);
1567 <            }
1568 <        }
1573 >        orpush(b, new OrAccept<T,U>(e, d, this, b, f));
1574          return d;
1575      }
1576  
# Line 1582 | Line 1587 | public class CompletableFuture<T> implem
1587              CompletableFuture<Void> d;
1588              CompletableFuture<T> a;
1589              CompletableFuture<U> b;
1590 <            if ((d = dep) == null ||
1591 <                !d.orRun(a = src, b = snd, fn, mode > 0 ? null : this))
1590 >            Object r; Throwable x; Runnable f;
1591 >            if ((a = src) == null
1592 >                || (b = snd) == null
1593 >                || ((r = a.result) == null && (r = b.result) == null)
1594 >                || (d = dep) == null
1595 >                || (f = fn) == null)
1596                  return null;
1597 <            dep = null; src = null; snd = null; fn = null;
1598 <            return d.postFire(a, b, mode);
1599 <        }
1600 <    }
1601 <
1602 <    final boolean orRun(CompletableFuture<?> a, CompletableFuture<?> b,
1603 <                        Runnable f, OrRun<?,?> c) {
1604 <        Object r; Throwable x;
1605 <        if (a == null || b == null ||
1606 <            ((r = a.result) == null && (r = b.result) == null) || f == null)
1607 <            return false;
1608 <        if (result == null) {
1609 <            try {
1601 <                if (c != null && !c.claim())
1602 <                    return false;
1603 <                if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
1604 <                    completeThrowable(x, r);
1605 <                else {
1606 <                    f.run();
1607 <                    completeNull();
1597 >            if (d.result == null) {
1598 >                try {
1599 >                    if (mode <= 0 && !claim())
1600 >                        return null;
1601 >                    else if (r instanceof AltResult
1602 >                        && (x = ((AltResult)r).ex) != null)
1603 >                        d.completeThrowable(x, r);
1604 >                    else {
1605 >                        f.run();
1606 >                        d.completeNull();
1607 >                    }
1608 >                } catch (Throwable ex) {
1609 >                    d.completeThrowable(ex);
1610                  }
1609            } catch (Throwable ex) {
1610                completeThrowable(ex);
1611              }
1612 +            dep = null; src = null; snd = null; fn = null;
1613 +            return d.postFire(a, b, mode);
1614          }
1613        return true;
1615      }
1616  
1617      private CompletableFuture<Void> orRunStage(Executor e, CompletionStage<?> o,
# Line 1618 | Line 1619 | public class CompletableFuture<T> implem
1619          CompletableFuture<?> b;
1620          if (f == null || (b = o.toCompletableFuture()) == null)
1621              throw new NullPointerException();
1622 +
1623 +        Object r; CompletableFuture<?> z;
1624 +        if ((r = (z = this).result) != null ||
1625 +            (r = (z = b).result) != null)
1626 +            return z.uniRunNow(r, e, f);
1627 +
1628          CompletableFuture<Void> d = newIncompleteFuture();
1629 <        if (e != null || !d.orRun(this, b, f, null)) {
1623 <            OrRun<T,?> c = new OrRun<>(e, d, this, b, f);
1624 <            if (e != null && (result != null || b.result != null)) {
1625 <                try {
1626 <                    e.execute(c);
1627 <                } catch (Throwable ex) {
1628 <                    d.completeThrowable(ex);
1629 <                }
1630 <            }
1631 <            else {
1632 <                orpush(b, c);
1633 <            }
1634 <        }
1629 >        orpush(b, new OrRun<>(e, d, this, b, f));
1630          return d;
1631      }
1632  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines