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

Comparing jsr166/src/jsr166e/CompletableFuture.java (file contents):
Revision 1.5 by jsr166, Thu Jan 3 19:34:31 2013 UTC vs.
Revision 1.13 by jsr166, Sat Feb 16 16:53:34 2013 UTC

# Line 388 | Line 388 | public class CompletableFuture<T> implem
388      }
389  
390      /** Base class can act as either FJ or plain Runnable */
391 <    static abstract class Async extends ForkJoinTask<Void>
391 >    abstract static class Async extends ForkJoinTask<Void>
392          implements Runnable, AsynchronousCompletionTask {
393          public final Void getRawResult() { return null; }
394          public final void setRawResult(Void v) { }
# Line 557 | Line 557 | public class CompletableFuture<T> implem
557      }
558  
559      // Opportunistically subclass AtomicInteger to use compareAndSet to claim.
560 <    static abstract class Completion extends AtomicInteger implements Runnable {
560 >    abstract static class Completion extends AtomicInteger implements Runnable {
561      }
562  
563      static final class ApplyCompletion<T,U> extends Completion {
# Line 1319 | Line 1319 | public class CompletableFuture<T> implem
1319          Object r; Throwable ex, cause;
1320          if ((r = result) == null && (r = waitingGet(true)) == null)
1321              throw new InterruptedException();
1322 <        if (r instanceof AltResult) {
1323 <            if ((ex = ((AltResult)r).ex) != null) {
1324 <                if (ex instanceof CancellationException)
1325 <                    throw (CancellationException)ex;
1326 <                if ((ex instanceof CompletionException) &&
1327 <                    (cause = ex.getCause()) != null)
1328 <                    ex = cause;
1329 <                throw new ExecutionException(ex);
1330 <            }
1331 <            return null;
1322 >        if (!(r instanceof AltResult)) {
1323 >            @SuppressWarnings("unchecked") T tr = (T) r;
1324 >            return tr;
1325          }
1326 <        @SuppressWarnings("unchecked") T tr = (T) r;
1327 <        return tr;
1326 >        if ((ex = ((AltResult)r).ex) == null)
1327 >            return null;
1328 >        if (ex instanceof CancellationException)
1329 >            throw (CancellationException)ex;
1330 >        if ((ex instanceof CompletionException) &&
1331 >            (cause = ex.getCause()) != null)
1332 >            ex = cause;
1333 >        throw new ExecutionException(ex);
1334      }
1335  
1336      /**
# Line 1356 | Line 1355 | public class CompletableFuture<T> implem
1355              throw new InterruptedException();
1356          if ((r = result) == null)
1357              r = timedAwaitDone(nanos);
1358 <        if (r instanceof AltResult) {
1359 <            if ((ex = ((AltResult)r).ex) != null) {
1360 <                if (ex instanceof CancellationException)
1362 <                    throw (CancellationException)ex;
1363 <                if ((ex instanceof CompletionException) &&
1364 <                    (cause = ex.getCause()) != null)
1365 <                    ex = cause;
1366 <                throw new ExecutionException(ex);
1367 <            }
1368 <            return null;
1358 >        if (!(r instanceof AltResult)) {
1359 >            @SuppressWarnings("unchecked") T tr = (T) r;
1360 >            return tr;
1361          }
1362 <        @SuppressWarnings("unchecked") T tr = (T) r;
1363 <        return tr;
1362 >        if ((ex = ((AltResult)r).ex) == null)
1363 >            return null;
1364 >        if (ex instanceof CancellationException)
1365 >            throw (CancellationException)ex;
1366 >        if ((ex instanceof CompletionException) &&
1367 >            (cause = ex.getCause()) != null)
1368 >            ex = cause;
1369 >        throw new ExecutionException(ex);
1370      }
1371  
1372      /**
# Line 1389 | Line 1387 | public class CompletableFuture<T> implem
1387          Object r; Throwable ex;
1388          if ((r = result) == null)
1389              r = waitingGet(false);
1390 <        if (r instanceof AltResult) {
1391 <            if ((ex = ((AltResult)r).ex) != null) {
1392 <                if (ex instanceof CancellationException)
1395 <                    throw (CancellationException)ex;
1396 <                if (ex instanceof CompletionException)
1397 <                    throw (CompletionException)ex;
1398 <                throw new CompletionException(ex);
1399 <            }
1400 <            return null;
1390 >        if (!(r instanceof AltResult)) {
1391 >            @SuppressWarnings("unchecked") T tr = (T) r;
1392 >            return tr;
1393          }
1394 <        @SuppressWarnings("unchecked") T tr = (T) r;
1395 <        return tr;
1394 >        if ((ex = ((AltResult)r).ex) == null)
1395 >            return null;
1396 >        if (ex instanceof CancellationException)
1397 >            throw (CancellationException)ex;
1398 >        if (ex instanceof CompletionException)
1399 >            throw (CompletionException)ex;
1400 >        throw new CompletionException(ex);
1401      }
1402  
1403      /**
# Line 1417 | Line 1414 | public class CompletableFuture<T> implem
1414          Object r; Throwable ex;
1415          if ((r = result) == null)
1416              return valueIfAbsent;
1417 <        if (r instanceof AltResult) {
1418 <            if ((ex = ((AltResult)r).ex) != null) {
1419 <                if (ex instanceof CancellationException)
1423 <                    throw (CancellationException)ex;
1424 <                if (ex instanceof CompletionException)
1425 <                    throw (CompletionException)ex;
1426 <                throw new CompletionException(ex);
1427 <            }
1428 <            return null;
1417 >        if (!(r instanceof AltResult)) {
1418 >            @SuppressWarnings("unchecked") T tr = (T) r;
1419 >            return tr;
1420          }
1421 <        @SuppressWarnings("unchecked") T tr = (T) r;
1422 <        return tr;
1421 >        if ((ex = ((AltResult)r).ex) == null)
1422 >            return null;
1423 >        if (ex instanceof CancellationException)
1424 >            throw (CancellationException)ex;
1425 >        if (ex instanceof CompletionException)
1426 >            throw (CompletionException)ex;
1427 >        throw new CompletionException(ex);
1428      }
1429  
1430      /**
# Line 2449 | Line 2445 | public class CompletableFuture<T> implem
2445       * then the returned CompletableFuture also does so, with a
2446       * CompletionException holding this exception as its cause.
2447       *
2448 <     * @param fn the function returning a new CompletableFuture.
2448 >     * @param fn the function returning a new CompletableFuture
2449       * @return the CompletableFuture, that {@code isDone()} upon
2450       * return if completed by the given function, or an exception
2451 <     * occurs.
2451 >     * occurs
2452       */
2453      public <U> CompletableFuture<U> thenCompose(Fun<? super T,
2454                                                  CompletableFuture<U>> fn) {
# Line 2559 | Line 2555 | public class CompletableFuture<T> implem
2555       *
2556       * @param fn the function to use to compute the value of the
2557       * returned CompletableFuture
2562
2558       * @return the new CompletableFuture
2559       */
2560      public <U> CompletableFuture<U> handle(BiFun<? super T, Throwable, ? extends U> fn) {
# Line 2633 | Line 2628 | public class CompletableFuture<T> implem
2628       */
2629      public boolean isCancelled() {
2630          Object r;
2631 <        return ((r = result) != null &&
2632 <                (r instanceof AltResult) &&
2638 <                (((AltResult)r).ex instanceof CancellationException));
2631 >        return ((r = result) instanceof AltResult) &&
2632 >            (((AltResult)r).ex instanceof CancellationException);
2633      }
2634  
2635      /**
2636       * Forcibly sets or resets the value subsequently returned by
2637 <     * method get() and related methods, whether or not already
2638 <     * completed. This method is designed for use only in error
2639 <     * recovery actions, and even in such situations may result in
2640 <     * ongoing dependent completions using established versus
2637 >     * method {@link #get()} and related methods, whether or not
2638 >     * already completed. This method is designed for use only in
2639 >     * error recovery actions, and even in such situations may result
2640 >     * in ongoing dependent completions using established versus
2641       * overwritten outcomes.
2642       *
2643       * @param value the completion value
# Line 2654 | Line 2648 | public class CompletableFuture<T> implem
2648      }
2649  
2650      /**
2651 <     * Forcibly causes subsequent invocations of method get() and
2652 <     * related methods to throw the given exception, whether or not
2653 <     * already completed. This method is designed for use only in
2651 >     * Forcibly causes subsequent invocations of method {@link #get()}
2652 >     * and related methods to throw the given exception, whether or
2653 >     * not already completed. This method is designed for use only in
2654       * recovery actions, and even in such situations may result in
2655       * ongoing dependent completions using established versus
2656       * overwritten outcomes.
# Line 2700 | Line 2694 | public class CompletableFuture<T> implem
2694      private static sun.misc.Unsafe getUnsafe() {
2695          try {
2696              return sun.misc.Unsafe.getUnsafe();
2697 <        } catch (SecurityException se) {
2698 <            try {
2699 <                return java.security.AccessController.doPrivileged
2700 <                    (new java.security
2701 <                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
2702 <                        public sun.misc.Unsafe run() throws Exception {
2703 <                            java.lang.reflect.Field f = sun.misc
2704 <                                .Unsafe.class.getDeclaredField("theUnsafe");
2705 <                            f.setAccessible(true);
2706 <                            return (sun.misc.Unsafe) f.get(null);
2707 <                        }});
2708 <            } catch (java.security.PrivilegedActionException e) {
2709 <                throw new RuntimeException("Could not initialize intrinsics",
2710 <                                           e.getCause());
2711 <            }
2697 >        } catch (SecurityException tryReflectionInstead) {}
2698 >        try {
2699 >            return java.security.AccessController.doPrivileged
2700 >            (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
2701 >                public sun.misc.Unsafe run() throws Exception {
2702 >                    Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
2703 >                    for (java.lang.reflect.Field f : k.getDeclaredFields()) {
2704 >                        f.setAccessible(true);
2705 >                        Object x = f.get(null);
2706 >                        if (k.isInstance(x))
2707 >                            return k.cast(x);
2708 >                    }
2709 >                    throw new NoSuchFieldError("the Unsafe");
2710 >                }});
2711 >        } catch (java.security.PrivilegedActionException e) {
2712 >            throw new RuntimeException("Could not initialize intrinsics",
2713 >                                       e.getCause());
2714          }
2715      }
2720
2716   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines