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); |
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 |
|
|
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 |
|
/** |
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 |
|
/** |
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 |
|
/** |
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 |
|
} |
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) { |
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); |
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 |
|
|
756 |
|
|
757 |
|
/* ------------- waiting for completions -------------- */ |
758 |
|
|
759 |
< |
/** |
759 |
> |
/** |
760 |
|
* Heuristic spin value for waitingGet() before blocking on |
761 |
|
* multiprocessors |
762 |
|
*/ |
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) |
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, |
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; |
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, |
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; |
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, |
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)); |
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, |
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)); |
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, |
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)); |
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, |
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)); |
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 && |
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) { |
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)); |
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) { |
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)); |
1376 |
|
dst.completeExceptionally(rex); |
1377 |
|
} |
1378 |
|
} |
1379 |
< |
postComplete(); |
1380 |
< |
} |
1379 |
> |
postComplete(); |
1380 |
> |
} |
1381 |
|
return dst; |
1382 |
|
} |
1383 |
|
|
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) || |
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) { |
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)); |
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) || |
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)); |
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) { |
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)); |
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) { |
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 { |
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 -------------- */ |