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.1 by dl, Thu Dec 27 18:12:22 2012 UTC vs.
Revision 1.2 by jsr166, Thu Dec 27 20:29:07 2012 UTC

# Line 71 | Line 71 | public class CompletableFuture<T> implem
71  
72      static final class AltResult {
73          final Throwable ex; // null only for NIL
74 <        AltResult(Throwable ex) { this.ex = ex; }
74 >        AltResult(Throwable ex) { this.ex = ex; }
75      }
76  
77      static final AltResult NIL = new AltResult(null);
# Line 91 | Line 91 | public class CompletableFuture<T> implem
91       * basically the same way as WaitNodes
92       */
93      static final class CompletionNode {
94 <        final Completion completion;
94 >        final Completion completion;
95          volatile CompletionNode next;
96 <        CompletionNode(Completion completion) { this.completion = completion; }
96 >        CompletionNode(Completion completion) { this.completion = completion; }
97      }
98  
99  
# Line 202 | Line 202 | public class CompletableFuture<T> implem
202       */
203      public T get() {
204          Object r; Throwable ex;
205 <        if ((r = result) == null)
206 <            return waitingGet();
207 <        if (r instanceof AltResult) {
208 <            if ((ex = ((AltResult)r).ex) != null)
209 <                rethrow(ex);
210 <            return null;
211 <        }
212 <        return (T)r;
205 >        if ((r = result) == null)
206 >            return waitingGet();
207 >        if (r instanceof AltResult) {
208 >            if ((ex = ((AltResult)r).ex) != null)
209 >                rethrow(ex);
210 >            return null;
211 >        }
212 >        return (T)r;
213      }
214  
215      /**
# Line 221 | Line 221 | public class CompletableFuture<T> implem
221       */
222      public T getNow(T valueIfAbsent) {
223          Object r; Throwable ex;
224 <        if ((r = result) == null)
225 <            return valueIfAbsent;
226 <        if (r instanceof AltResult) {
227 <            if ((ex = ((AltResult)r).ex) != null)
228 <                rethrow(ex);
229 <            return null;
230 <        }
231 <        return (T)r;
224 >        if ((r = result) == null)
225 >            return valueIfAbsent;
226 >        if (r instanceof AltResult) {
227 >            if ((ex = ((AltResult)r).ex) != null)
228 >                rethrow(ex);
229 >            return null;
230 >        }
231 >        return (T)r;
232      }
233  
234      /**
# Line 251 | Line 251 | public class CompletableFuture<T> implem
251          long nanos = unit.toNanos(timeout);
252          if (Thread.interrupted())
253              throw new InterruptedException();
254 <        if ((r = result) == null)
255 <            r = timedAwaitDone(nanos);
256 <        if (r instanceof AltResult) {
257 <            if ((ex = ((AltResult)r).ex) != null)
254 >        if ((r = result) == null)
255 >            r = timedAwaitDone(nanos);
256 >        if (r instanceof AltResult) {
257 >            if ((ex = ((AltResult)r).ex) != null)
258                  throw new ExecutionException(ex);
259 <            return null;
260 <        }
261 <        return (T)r;
259 >            return null;
260 >        }
261 >        return (T)r;
262      }
263  
264      /**
# Line 272 | Line 272 | public class CompletableFuture<T> implem
272      public boolean complete(T value) {
273          if (result == null &&
274              UNSAFE.compareAndSwapObject(this, RESULT, null,
275 <                                        (value == null)? NIL : value)) {
275 >                                        (value == null) ? NIL : value)) {
276              postComplete();
277              return true;
278          }
# Line 659 | Line 659 | public class CompletableFuture<T> implem
659          if (action == null) throw new NullPointerException();
660          CompletableFuture<Void> dst = new CompletableFuture<Void>();
661          ExceptionAction<T> d = null;
662 <        Object r; Throwable ex;
663 <        if ((r = result) == null) {
662 >        Object r; Throwable ex;
663 >        if ((r = result) == null) {
664              CompletionNode p =
665                  new CompletionNode(d = new ExceptionAction<T>(this, action, dst));
666              while ((r = result) == null) {
# Line 669 | Line 669 | public class CompletableFuture<T> implem
669                      break;
670              }
671          }
672 <        if (r != null && (d == null || d.compareAndSet(0, 1)) &&
672 >        if (r != null && (d == null || d.compareAndSet(0, 1)) &&
673              (r instanceof AltResult) && (ex = ((AltResult)r).ex) != null)  {
674              try {
675                  action.accept(ex);
# Line 729 | Line 729 | public class CompletableFuture<T> implem
729       * @param value the completion value
730       */
731      public void force(T value) {
732 <        result = (value == null)? NIL : value;
732 >        result = (value == null) ? NIL : value;
733          postComplete();
734      }
735  
# Line 756 | Line 756 | public class CompletableFuture<T> implem
756  
757      /* ------------- waiting for completions -------------- */
758  
759 <    /**
759 >    /**
760       * Heuristic spin value for waitingGet() before blocking on
761       * multiprocessors
762       */
# Line 790 | Line 790 | public class CompletableFuture<T> implem
790              }
791              else if (spins > 0) {
792                  h ^= h << 1;  // xorshift
793 <                h ^= h >>> 3;
793 >                h ^= h >>> 3;
794                  if ((h ^= h << 10) >= 0)
795                      --spins;
796 <            }  
796 >            }
797              else if (q == null)
798                  q = new WaitNode();
799              else if (!queued)
# Line 985 | Line 985 | public class CompletableFuture<T> implem
985      }
986  
987      static final class ThenFunction<T,U> extends Completion {
988 <        final CompletableFuture<? extends T> src;
989 <        final Function<? super T,? extends U> fn;
990 <        final CompletableFuture<U> dst;
988 >        final CompletableFuture<? extends T> src;
989 >        final Function<? super T,? extends U> fn;
990 >        final CompletableFuture<U> dst;
991          final Executor executor;
992          ThenFunction(CompletableFuture<? extends T> src,
993                       final Function<? super T,? extends U> fn,
# Line 995 | Line 995 | public class CompletableFuture<T> implem
995              this.src = src; this.fn = fn; this.dst = dst;
996              this.executor = executor;
997          }
998 <        public void run() {
998 >        public void run() {
999              CompletableFuture<? extends T> a;
1000              Function<? super T,? extends U> fn;
1001              CompletableFuture<U> dst;
1002 <            Object r; T t; Throwable ex;
1003 <            if ((dst = this.dst) != null &&
1002 >            Object r; T t; Throwable ex;
1003 >            if ((dst = this.dst) != null &&
1004                  (fn = this.fn) != null &&
1005                  (a = this.src) != null &&
1006                  (r = a.result) != null &&
1007                  compareAndSet(0, 1)) {
1008 <                if (r instanceof AltResult) {
1008 >                if (r instanceof AltResult) {
1009                      if ((ex = ((AltResult)r).ex) != null) {
1010                          dst.completeExceptionally(new RuntimeException(ex));
1011                          return;
# Line 1027 | Line 1027 | public class CompletableFuture<T> implem
1027      }
1028  
1029      static final class ThenRunnable<T> extends Completion {
1030 <        final CompletableFuture<? extends T> src;
1031 <        final Runnable fn;
1032 <        final CompletableFuture<Void> dst;
1030 >        final CompletableFuture<? extends T> src;
1031 >        final Runnable fn;
1032 >        final CompletableFuture<Void> dst;
1033          final Executor executor;
1034          ThenRunnable(CompletableFuture<? extends T> src,
1035                       Runnable fn,
# Line 1038 | Line 1038 | public class CompletableFuture<T> implem
1038              this.src = src; this.fn = fn; this.dst = dst;
1039              this.executor = executor;
1040          }
1041 <        public void run() {
1041 >        public void run() {
1042              CompletableFuture<? extends T> a;
1043              Runnable fn;
1044              CompletableFuture<Void> dst;
1045 <            Object r; Throwable ex;
1046 <            if ((dst = this.dst) != null &&
1045 >            Object r; Throwable ex;
1046 >            if ((dst = this.dst) != null &&
1047                  (fn = this.fn) != null &&
1048                  (a = this.src) != null &&
1049                  (r = a.result) != null &&
1050                  compareAndSet(0, 1)) {
1051 <                if (r instanceof AltResult) {
1051 >                if (r instanceof AltResult) {
1052                      if ((ex = ((AltResult)r).ex) != null) {
1053                          dst.completeExceptionally(new RuntimeException(ex));
1054                          return;
# Line 1069 | Line 1069 | public class CompletableFuture<T> implem
1069      }
1070  
1071      static final class AndFunction<T,U,V> extends Completion {
1072 <        final CompletableFuture<? extends T> src;
1073 <        final CompletableFuture<? extends U> snd;
1074 <        final BiFunction<? super T,? super U,? extends V> fn;
1075 <        final CompletableFuture<V> dst;
1072 >        final CompletableFuture<? extends T> src;
1073 >        final CompletableFuture<? extends U> snd;
1074 >        final BiFunction<? super T,? super U,? extends V> fn;
1075 >        final CompletableFuture<V> dst;
1076          final Executor executor;
1077          AndFunction(CompletableFuture<? extends T> src,
1078                      CompletableFuture<? extends U> snd,
# Line 1082 | Line 1082 | public class CompletableFuture<T> implem
1082              this.fn = fn; this.dst = dst;
1083              this.executor = executor;
1084          }
1085 <        public void run() {
1086 <            Object r, s; T t; U u; Throwable ex;
1085 >        public void run() {
1086 >            Object r, s; T t; U u; Throwable ex;
1087              CompletableFuture<? extends T> a;
1088              CompletableFuture<? extends U> b;
1089              BiFunction<? super T,? super U,? extends V> fn;
1090              CompletableFuture<V> dst;
1091 <            if ((dst = this.dst) != null &&
1091 >            if ((dst = this.dst) != null &&
1092                  (fn = this.fn) != null &&
1093                  (a = this.src) != null &&
1094                  (r = a.result) != null &&
1095                  (b = this.snd) != null &&
1096                  (s = b.result) != null &&
1097                  compareAndSet(0, 1)) {
1098 <                if (r instanceof AltResult) {
1098 >                if (r instanceof AltResult) {
1099                      if ((ex = ((AltResult)r).ex) != null) {
1100                          dst.completeExceptionally(new RuntimeException(ex));
1101                          return;
1102                      }
1103 <                    t = null;
1104 <                }
1105 <                else
1106 <                    t = (T) r;
1107 <                if (s instanceof AltResult) {
1103 >                    t = null;
1104 >                }
1105 >                else
1106 >                    t = (T) r;
1107 >                if (s instanceof AltResult) {
1108                      if ((ex = ((AltResult)s).ex) != null) {
1109                          dst.completeExceptionally(new RuntimeException(ex));
1110                          return;
1111                      }
1112 <                    u = null;
1113 <                }
1114 <                else
1115 <                    u = (U) s;
1112 >                    u = null;
1113 >                }
1114 >                else
1115 >                    u = (U) s;
1116                  try {
1117                      if (executor != null)
1118                          executor.execute(new AsyncBiFunction<T,U,V>(t, u, fn, dst));
# Line 1121 | Line 1121 | public class CompletableFuture<T> implem
1121                  } catch (Throwable rex) {
1122                      dst.completeExceptionally(rex);
1123                  }
1124 <            }
1125 <        }
1124 >            }
1125 >        }
1126      }
1127  
1128      static final class AndRunnable<T> extends Completion {
1129 <        final CompletableFuture<? extends T> src;
1130 <        final CompletableFuture<?> snd;
1131 <        final Runnable fn;
1132 <        final CompletableFuture<Void> dst;
1129 >        final CompletableFuture<? extends T> src;
1130 >        final CompletableFuture<?> snd;
1131 >        final Runnable fn;
1132 >        final CompletableFuture<Void> dst;
1133          final Executor executor;
1134          AndRunnable(CompletableFuture<? extends T> src,
1135                      CompletableFuture<?> snd,
# Line 1139 | Line 1139 | public class CompletableFuture<T> implem
1139              this.fn = fn; this.dst = dst;
1140              this.executor = executor;
1141          }
1142 <        public void run() {
1143 <            Object r, s; Throwable ex;
1142 >        public void run() {
1143 >            Object r, s; Throwable ex;
1144              final CompletableFuture<? extends T> a;
1145              final CompletableFuture<?> b;
1146              final Runnable fn;
1147              final CompletableFuture<Void> dst;
1148 <            if ((dst = this.dst) != null &&
1148 >            if ((dst = this.dst) != null &&
1149                  (fn = this.fn) != null &&
1150                  (a = this.src) != null &&
1151                  (r = a.result) != null &&
1152                  (b = this.snd) != null &&
1153                  (s = b.result) != null &&
1154                  compareAndSet(0, 1)) {
1155 <                if (r instanceof AltResult) {
1155 >                if (r instanceof AltResult) {
1156                      if ((ex = ((AltResult)r).ex) != null) {
1157                          dst.completeExceptionally(new RuntimeException(ex));
1158                          return;
1159                      }
1160 <                }
1161 <                if (s instanceof AltResult) {
1160 >                }
1161 >                if (s instanceof AltResult) {
1162                      if ((ex = ((AltResult)s).ex) != null) {
1163                          dst.completeExceptionally(new RuntimeException(ex));
1164                          return;
1165                      }
1166 <                }
1166 >                }
1167                  try {
1168                      if (executor != null)
1169                          executor.execute(new AsyncRunnable(fn, dst));
# Line 1174 | Line 1174 | public class CompletableFuture<T> implem
1174                  } catch (Throwable rex) {
1175                      dst.completeExceptionally(rex);
1176                  }
1177 <            }
1178 <        }
1177 >            }
1178 >        }
1179      }
1180  
1181      static final class OrFunction<T,U> extends Completion {
1182 <        final CompletableFuture<? extends T> src;
1183 <        final CompletableFuture<? extends T> snd;
1184 <        final Function<? super T,? extends U> fn;
1185 <        final CompletableFuture<U> dst;
1182 >        final CompletableFuture<? extends T> src;
1183 >        final CompletableFuture<? extends T> snd;
1184 >        final Function<? super T,? extends U> fn;
1185 >        final CompletableFuture<U> dst;
1186          final Executor executor;
1187          OrFunction(CompletableFuture<? extends T> src,
1188                     CompletableFuture<? extends T> snd,
# Line 1192 | Line 1192 | public class CompletableFuture<T> implem
1192              this.fn = fn; this.dst = dst;
1193              this.executor = executor;
1194          }
1195 <        public void run() {
1196 <            Object r; T t; Throwable ex;
1195 >        public void run() {
1196 >            Object r; T t; Throwable ex;
1197              CompletableFuture<? extends T> a;
1198              CompletableFuture<? extends T> b;
1199              Function<? super T,? extends U> fn;
1200              CompletableFuture<U> dst;
1201 <            if ((dst = this.dst) != null &&
1201 >            if ((dst = this.dst) != null &&
1202                  (fn = this.fn) != null &&
1203                  (((a = this.src) != null && (r = a.result) != null) ||
1204                   ((b = this.snd) != null && (r = b.result) != null)) &&
1205                  compareAndSet(0, 1)) {
1206 <                if (r instanceof AltResult) {
1206 >                if (r instanceof AltResult) {
1207                      if ((ex = ((AltResult)r).ex) != null) {
1208                          dst.completeExceptionally(new RuntimeException(ex));
1209                          return;
1210                      }
1211 <                    t = null;
1212 <                }
1213 <                else
1214 <                    t = (T) r;
1211 >                    t = null;
1212 >                }
1213 >                else
1214 >                    t = (T) r;
1215                  try {
1216                      if (executor != null)
1217                          executor.execute(new AsyncFunction(t, fn, dst));
# Line 1220 | Line 1220 | public class CompletableFuture<T> implem
1220                  } catch (Throwable rex) {
1221                      dst.completeExceptionally(rex);
1222                  }
1223 <            }
1224 <        }
1223 >            }
1224 >        }
1225      }
1226  
1227      static final class OrRunnable<T> extends Completion {
1228 <        final CompletableFuture<? extends T> src;
1229 <        final CompletableFuture<?> snd;
1230 <        final Runnable fn;
1231 <        final CompletableFuture<Void> dst;
1228 >        final CompletableFuture<? extends T> src;
1229 >        final CompletableFuture<?> snd;
1230 >        final Runnable fn;
1231 >        final CompletableFuture<Void> dst;
1232          final Executor executor;
1233          OrRunnable(CompletableFuture<? extends T> src,
1234                     CompletableFuture<?> snd,
# Line 1238 | Line 1238 | public class CompletableFuture<T> implem
1238              this.fn = fn; this.dst = dst;
1239              this.executor = executor;
1240          }
1241 <        public void run() {
1242 <            Object r; Throwable ex;
1241 >        public void run() {
1242 >            Object r; Throwable ex;
1243              CompletableFuture<? extends T> a;
1244              final CompletableFuture<?> b;
1245              final Runnable fn;
1246              final CompletableFuture<Void> dst;
1247 <            if ((dst = this.dst) != null &&
1247 >            if ((dst = this.dst) != null &&
1248                  (fn = this.fn) != null &&
1249                  (((a = this.src) != null && (r = a.result) != null) ||
1250                   ((b = this.snd) != null && (r = b.result) != null)) &&
1251                  compareAndSet(0, 1)) {
1252 <                if ((r instanceof AltResult) &&
1252 >                if ((r instanceof AltResult) &&
1253                      (ex = ((AltResult)r).ex) != null) {
1254                      dst.completeExceptionally(new RuntimeException(ex));
1255                  }
1256 <                else {
1256 >                else {
1257                      try {
1258                          if (executor != null)
1259                              executor.execute(new AsyncRunnable(fn, dst));
# Line 1265 | Line 1265 | public class CompletableFuture<T> implem
1265                          dst.completeExceptionally(rex);
1266                      }
1267                  }
1268 <            }
1269 <        }
1268 >            }
1269 >        }
1270      }
1271  
1272      static final class ExceptionAction<T> extends Completion {
1273 <        final CompletableFuture<? extends T> src;
1274 <        final Block<? super Throwable> fn;
1275 <        final CompletableFuture<Void> dst;
1273 >        final CompletableFuture<? extends T> src;
1274 >        final Block<? super Throwable> fn;
1275 >        final CompletableFuture<Void> dst;
1276          ExceptionAction(CompletableFuture<? extends T> src,
1277                          Block<? super Throwable> fn,
1278                          CompletableFuture<Void> dst) {
1279              this.src = src; this.fn = fn; this.dst = dst;
1280          }
1281 <        public void run() {
1281 >        public void run() {
1282              CompletableFuture<? extends T> a;
1283              Block<? super Throwable> fn;
1284              CompletableFuture<Void> dst;
1285 <            Object r; Throwable ex;
1286 <            if ((dst = this.dst) != null &&
1285 >            Object r; Throwable ex;
1286 >            if ((dst = this.dst) != null &&
1287                  (fn = this.fn) != null &&
1288                  (a = this.src) != null &&
1289                  (r = a.result) != null &&
# Line 1309 | Line 1309 | public class CompletableFuture<T> implem
1309          if (fn == null) throw new NullPointerException();
1310          CompletableFuture<U> dst = new CompletableFuture<U>();
1311          ThenFunction<T,U> d = null;
1312 <        Object r;
1313 <        if ((r = result) == null) {
1312 >        Object r;
1313 >        if ((r = result) == null) {
1314              CompletionNode p = new CompletionNode
1315                  (d = new ThenFunction<T,U>(this, fn, dst, executor));
1316              while ((r = result) == null) {
# Line 1319 | Line 1319 | public class CompletableFuture<T> implem
1319                      break;
1320              }
1321          }
1322 <        if (r != null && (d == null || d.compareAndSet(0, 1))) {
1323 <            T t; Throwable ex = null;
1324 <            if (r instanceof AltResult) {
1322 >        if (r != null && (d == null || d.compareAndSet(0, 1))) {
1323 >            T t; Throwable ex = null;
1324 >            if (r instanceof AltResult) {
1325                  if ((ex = ((AltResult)r).ex) != null)
1326 <                    dst.completeExceptionally(new RuntimeException(ex));
1327 <                t = null;
1326 >                    dst.completeExceptionally(new RuntimeException(ex));
1327 >                t = null;
1328              }
1329              else
1330 <                t = (T) r;
1331 <            if (ex == null) {
1330 >                t = (T) r;
1331 >            if (ex == null) {
1332                  try {
1333                      if (executor != null)
1334                          executor.execute(new AsyncFunction(t, fn, dst));
# Line 1338 | Line 1338 | public class CompletableFuture<T> implem
1338                      dst.completeExceptionally(rex);
1339                  }
1340              }
1341 <            postComplete();
1342 <        }
1341 >            postComplete();
1342 >        }
1343          return dst;
1344      }
1345  
1346 <    private CompletableFuture<Void> thenRunnable(Runnable action,
1346 >    private CompletableFuture<Void> thenRunnable(Runnable action,
1347                                                   Executor executor) {
1348          if (action == null) throw new NullPointerException();
1349          CompletableFuture<Void> dst = new CompletableFuture<Void>();
1350          ThenRunnable<T> d = null;
1351 <        Object r;
1352 <        if ((r = result) == null) {
1351 >        Object r;
1352 >        if ((r = result) == null) {
1353              CompletionNode p = new CompletionNode
1354                  (d = new ThenRunnable<T>(this, action, dst, executor));
1355              while ((r = result) == null) {
# Line 1358 | Line 1358 | public class CompletableFuture<T> implem
1358                      break;
1359              }
1360          }
1361 <        if (r != null && (d == null || d.compareAndSet(0, 1))) {
1361 >        if (r != null && (d == null || d.compareAndSet(0, 1))) {
1362              Throwable ex = null;
1363 <            if (r instanceof AltResult) {
1363 >            if (r instanceof AltResult) {
1364                  if ((ex = ((AltResult)r).ex) != null)
1365 <                    dst.completeExceptionally(new RuntimeException(ex));
1365 >                    dst.completeExceptionally(new RuntimeException(ex));
1366              }
1367 <            if (ex == null) {
1367 >            if (ex == null) {
1368                  try {
1369                      if (executor != null)
1370                          executor.execute(new AsyncRunnable(action, dst));
# Line 1376 | Line 1376 | public class CompletableFuture<T> implem
1376                      dst.completeExceptionally(rex);
1377                  }
1378              }
1379 <            postComplete();
1380 <        }
1379 >            postComplete();
1380 >        }
1381          return dst;
1382      }
1383  
# Line 1385 | Line 1385 | public class CompletableFuture<T> implem
1385                                                     BiFunction<? super T,? super U,? extends V> fn,
1386                                                     Executor executor) {
1387          if (other == null || fn == null) throw new NullPointerException();
1388 <        CompletableFuture<V> dst = new CompletableFuture<V>();
1389 <        AndFunction<T,U,V> d = null;
1390 <        Object r, s = null;
1391 <        if ((r = result) == null || (s = other.result) == null) {
1388 >        CompletableFuture<V> dst = new CompletableFuture<V>();
1389 >        AndFunction<T,U,V> d = null;
1390 >        Object r, s = null;
1391 >        if ((r = result) == null || (s = other.result) == null) {
1392              d = new AndFunction<T,U,V>(this, other, fn, dst, executor);
1393              CompletionNode q = null, p = new CompletionNode(d);
1394              while ((r == null && (r = result) == null) ||
# Line 1408 | Line 1408 | public class CompletableFuture<T> implem
1408                  }
1409              }
1410          }
1411 <        if (r != null && s != null && (d == null || d.compareAndSet(0, 1))) {
1412 <            T t; U u; Throwable ex = null;
1413 <            if (r instanceof AltResult) {
1411 >        if (r != null && s != null && (d == null || d.compareAndSet(0, 1))) {
1412 >            T t; U u; Throwable ex = null;
1413 >            if (r instanceof AltResult) {
1414                  if ((ex = ((AltResult)r).ex) != null)
1415 <                    dst.completeExceptionally(new RuntimeException(ex));
1416 <                t = null;
1415 >                    dst.completeExceptionally(new RuntimeException(ex));
1416 >                t = null;
1417              }
1418              else
1419 <                t = (T) r;
1419 >                t = (T) r;
1420              if (ex != null)
1421                  u = null;
1422              else if (s instanceof AltResult) {
# Line 1425 | Line 1425 | public class CompletableFuture<T> implem
1425                  u = null;
1426              }
1427              else
1428 <                u = (U) s;
1429 <            if (ex == null) {
1428 >                u = (U) s;
1429 >            if (ex == null) {
1430                  try {
1431                      if (executor != null)
1432                          executor.execute(new AsyncBiFunction<T,U,V>(t, u, fn, dst));
# Line 1436 | Line 1436 | public class CompletableFuture<T> implem
1436                      dst.completeExceptionally(rex);
1437                  }
1438              }
1439 <        }
1439 >        }
1440          if (r != null)
1441              postComplete();
1442          if (s != null)
1443              other.postComplete();
1444 <        return dst;
1444 >        return dst;
1445      }
1446  
1447      private CompletableFuture<Void> andRunnable(CompletableFuture<?> other,
1448                                                  Runnable action,
1449                                                  Executor executor) {
1450          if (other == null || action == null) throw new NullPointerException();
1451 <        CompletableFuture<Void> dst = new CompletableFuture<Void>();
1452 <        AndRunnable<T> d = null;
1453 <        Object r, s = null;
1454 <        if ((r = result) == null || (s = other.result) == null) {
1451 >        CompletableFuture<Void> dst = new CompletableFuture<Void>();
1452 >        AndRunnable<T> d = null;
1453 >        Object r, s = null;
1454 >        if ((r = result) == null || (s = other.result) == null) {
1455              d = new AndRunnable<T>(this, other, action, dst, executor);
1456              CompletionNode q = null, p = new CompletionNode(d);
1457              while ((r == null && (r = result) == null) ||
# Line 1471 | Line 1471 | public class CompletableFuture<T> implem
1471                  }
1472              }
1473          }
1474 <        if (r != null && s != null && (d == null || d.compareAndSet(0, 1))) {
1475 <            Throwable ex = null;
1476 <            if ((r instanceof AltResult) &&
1474 >        if (r != null && s != null && (d == null || d.compareAndSet(0, 1))) {
1475 >            Throwable ex = null;
1476 >            if ((r instanceof AltResult) &&
1477                  (ex = ((AltResult)r).ex) != null)
1478                  dst.completeExceptionally(new RuntimeException(ex));
1479              else if ((s instanceof AltResult) &&
1480                       (ex = ((AltResult)s).ex) != null)
1481                  dst.completeExceptionally(new RuntimeException(ex));
1482 <            else {
1482 >            else {
1483                  try {
1484                      if (executor != null)
1485                          executor.execute(new AsyncRunnable(action, dst));
# Line 1491 | Line 1491 | public class CompletableFuture<T> implem
1491                      dst.completeExceptionally(rex);
1492                  }
1493              }
1494 <        }
1494 >        }
1495          if (r != null)
1496              postComplete();
1497          if (s != null)
1498              other.postComplete();
1499 <        return dst;
1499 >        return dst;
1500      }
1501  
1502      private <U> CompletableFuture<U> orFunction(CompletableFuture<? extends T> other,
1503                                                  Function<? super T, U> fn,
1504                                                  Executor executor) {
1505          if (other == null || fn == null) throw new NullPointerException();
1506 <        CompletableFuture<U> dst = new CompletableFuture<U>();
1507 <        OrFunction<T,U> d = null;
1508 <        Object r;
1509 <        if ((r = result) == null && (r = other.result) == null) {
1506 >        CompletableFuture<U> dst = new CompletableFuture<U>();
1507 >        OrFunction<T,U> d = null;
1508 >        Object r;
1509 >        if ((r = result) == null && (r = other.result) == null) {
1510              d = new OrFunction<T,U>(this, other, fn, dst, executor);
1511              CompletionNode q = null, p = new CompletionNode(d);
1512              while ((r = result) == null && (r = other.result) == null) {
# Line 1519 | Line 1519 | public class CompletableFuture<T> implem
1519                           (this, COMPLETIONS, p.next = completions, p))
1520                      q = new CompletionNode(d);
1521              }
1522 <        }
1523 <        if (r != null && (d == null || d.compareAndSet(0, 1))) {
1524 <            T t; Throwable ex = null;
1525 <            if (r instanceof AltResult) {
1522 >        }
1523 >        if (r != null && (d == null || d.compareAndSet(0, 1))) {
1524 >            T t; Throwable ex = null;
1525 >            if (r instanceof AltResult) {
1526                  if ((ex = ((AltResult)r).ex) != null)
1527 <                    dst.completeExceptionally(new RuntimeException(ex));
1528 <                t = null;
1527 >                    dst.completeExceptionally(new RuntimeException(ex));
1528 >                t = null;
1529              }
1530              else
1531 <                t = (T) r;
1532 <            if (ex == null) {
1531 >                t = (T) r;
1532 >            if (ex == null) {
1533                  try {
1534                      if (executor != null)
1535                          executor.execute(new AsyncFunction(t, fn, dst));
# Line 1539 | Line 1539 | public class CompletableFuture<T> implem
1539                      dst.completeExceptionally(rex);
1540                  }
1541              }
1542 <        }
1542 >        }
1543          if (r != null) {
1544              if (result != null)
1545                  postComplete();
1546              if (other.result != null)
1547                  other.postComplete();
1548          }
1549 <        return dst;
1549 >        return dst;
1550      }
1551  
1552      private CompletableFuture<Void> orRunnable(CompletableFuture<?> other,
1553                                                 Runnable action,
1554                                                 Executor executor) {
1555          if (other == null || action == null) throw new NullPointerException();
1556 <        CompletableFuture<Void> dst = new CompletableFuture<Void>();
1557 <        OrRunnable<T> d = null;
1558 <        Object r;
1559 <        if ((r = result) == null && (r = other.result) == null) {
1556 >        CompletableFuture<Void> dst = new CompletableFuture<Void>();
1557 >        OrRunnable<T> d = null;
1558 >        Object r;
1559 >        if ((r = result) == null && (r = other.result) == null) {
1560              d = new OrRunnable<T>(this, other, action, dst, executor);
1561              CompletionNode q = null, p = new CompletionNode(d);
1562              while ((r = result) == null && (r = other.result) == null) {
# Line 1569 | Line 1569 | public class CompletableFuture<T> implem
1569                           (this, COMPLETIONS, p.next = completions, p))
1570                      q = new CompletionNode(d);
1571              }
1572 <        }
1573 <        if (r != null && (d == null || d.compareAndSet(0, 1))) {
1574 <            Throwable ex = null;
1575 <            if ((r instanceof AltResult) &&
1572 >        }
1573 >        if (r != null && (d == null || d.compareAndSet(0, 1))) {
1574 >            Throwable ex = null;
1575 >            if ((r instanceof AltResult) &&
1576                  (ex = ((AltResult)r).ex) != null)
1577                  dst.completeExceptionally(new RuntimeException(ex));
1578              else {
# Line 1587 | Line 1587 | public class CompletableFuture<T> implem
1587                      dst.completeExceptionally(rex);
1588                  }
1589              }
1590 <        }
1590 >        }
1591          if (r != null) {
1592              if (result != null)
1593                  postComplete();
1594              if (other.result != null)
1595                  other.postComplete();
1596          }
1597 <        return dst;
1597 >        return dst;
1598      }
1599  
1600      /* ------------- misc -------------- */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines