178 |
|
* CompletionException unless it is one already. Otherwise uses |
179 |
|
* the given result, boxed as NIL if null. |
180 |
|
*/ |
181 |
< |
final void internalComplete(Object v, Throwable ex) { |
181 |
> |
final void internalComplete(T v, Throwable ex) { |
182 |
|
if (result == null) |
183 |
|
UNSAFE.compareAndSwapObject |
184 |
|
(this, RESULT, null, |
1119 |
|
static final class OrCompletion extends Completion { |
1120 |
|
final CompletableFuture<?> src; |
1121 |
|
final CompletableFuture<?> snd; |
1122 |
< |
final CompletableFuture<?> dst; |
1122 |
> |
final CompletableFuture<Void> dst; |
1123 |
|
OrCompletion(CompletableFuture<?> src, |
1124 |
|
CompletableFuture<?> snd, |
1125 |
< |
CompletableFuture<?> dst) { |
1125 |
> |
CompletableFuture<Void> dst) { |
1126 |
|
this.src = src; this.snd = snd; this.dst = dst; |
1127 |
|
} |
1128 |
|
public final void run() { |
1129 |
|
final CompletableFuture<?> a; |
1130 |
|
final CompletableFuture<?> b; |
1131 |
< |
final CompletableFuture<?> dst; |
1132 |
< |
Object r, t; Throwable ex; |
1131 |
> |
final CompletableFuture<Void> dst; |
1132 |
> |
Object r; Throwable ex; |
1133 |
|
if ((dst = this.dst) != null && |
1134 |
|
(((a = this.src) != null && (r = a.result) != null) || |
1135 |
|
((b = this.snd) != null && (r = b.result) != null)) && |
1136 |
|
compareAndSet(0, 1)) { |
1137 |
< |
if (r instanceof AltResult) { |
1137 |
> |
if (r instanceof AltResult) |
1138 |
|
ex = ((AltResult)r).ex; |
1139 |
< |
t = null; |
1140 |
< |
} |
1141 |
< |
else { |
1139 |
> |
else |
1140 |
|
ex = null; |
1141 |
< |
t = r; |
1144 |
< |
} |
1145 |
< |
dst.internalComplete(t, ex); |
1141 |
> |
dst.internalComplete(null, ex); |
1142 |
|
} |
1143 |
|
} |
1144 |
|
private static final long serialVersionUID = 5232453952276885070L; |
1181 |
|
private static final long serialVersionUID = 5232453952276885070L; |
1182 |
|
} |
1183 |
|
|
1184 |
< |
static final class ThenCopy extends Completion { |
1185 |
< |
final CompletableFuture<?> src; |
1186 |
< |
final CompletableFuture<?> dst; |
1187 |
< |
ThenCopy(CompletableFuture<?> src, |
1188 |
< |
CompletableFuture<?> dst) { |
1184 |
> |
static final class ThenCopy<T> extends Completion { |
1185 |
> |
final CompletableFuture<T> src; |
1186 |
> |
final CompletableFuture<T> dst; |
1187 |
> |
ThenCopy(CompletableFuture<T> src, |
1188 |
> |
CompletableFuture<T> dst) { |
1189 |
|
this.src = src; this.dst = dst; |
1190 |
|
} |
1191 |
|
public final void run() { |
1192 |
< |
final CompletableFuture<?> a; |
1193 |
< |
final CompletableFuture<?> dst; |
1194 |
< |
Object r; Object t; Throwable ex; |
1192 |
> |
final CompletableFuture<T> a; |
1193 |
> |
final CompletableFuture<T> dst; |
1194 |
> |
Object r; T t; Throwable ex; |
1195 |
|
if ((dst = this.dst) != null && |
1196 |
|
(a = this.src) != null && |
1197 |
|
(r = a.result) != null && |
1202 |
|
} |
1203 |
|
else { |
1204 |
|
ex = null; |
1205 |
< |
t = r; |
1205 |
> |
@SuppressWarnings("unchecked") T tr = (T) r; |
1206 |
> |
t = tr; |
1207 |
|
} |
1208 |
|
dst.internalComplete(t, ex); |
1209 |
|
} |
1211 |
|
private static final long serialVersionUID = 5232453952276885070L; |
1212 |
|
} |
1213 |
|
|
1214 |
+ |
// version of ThenCopy for CompletableFuture<Void> dst |
1215 |
+ |
static final class ThenPropagate extends Completion { |
1216 |
+ |
final CompletableFuture<?> src; |
1217 |
+ |
final CompletableFuture<Void> dst; |
1218 |
+ |
ThenPropagate(CompletableFuture<?> src, |
1219 |
+ |
CompletableFuture<Void> dst) { |
1220 |
+ |
this.src = src; this.dst = dst; |
1221 |
+ |
} |
1222 |
+ |
public final void run() { |
1223 |
+ |
final CompletableFuture<?> a; |
1224 |
+ |
final CompletableFuture<Void> dst; |
1225 |
+ |
Object r; Throwable ex; |
1226 |
+ |
if ((dst = this.dst) != null && |
1227 |
+ |
(a = this.src) != null && |
1228 |
+ |
(r = a.result) != null && |
1229 |
+ |
compareAndSet(0, 1)) { |
1230 |
+ |
if (r instanceof AltResult) |
1231 |
+ |
ex = ((AltResult)r).ex; |
1232 |
+ |
else |
1233 |
+ |
ex = null; |
1234 |
+ |
dst.internalComplete(null, ex); |
1235 |
+ |
} |
1236 |
+ |
} |
1237 |
+ |
private static final long serialVersionUID = 5232453952276885070L; |
1238 |
+ |
} |
1239 |
+ |
|
1240 |
|
static final class HandleCompletion<T,U> extends Completion { |
1241 |
|
final CompletableFuture<? extends T> src; |
1242 |
|
final BiFunction<? super T, Throwable, ? extends U> fn; |
1323 |
|
} |
1324 |
|
} |
1325 |
|
if (c != null) { |
1326 |
< |
ThenCopy d = null; |
1326 |
> |
ThenCopy<U> d = null; |
1327 |
|
Object s; |
1328 |
|
if ((s = c.result) == null) { |
1329 |
|
CompletionNode p = new CompletionNode |
1330 |
< |
(d = new ThenCopy(c, dst)); |
1330 |
> |
(d = new ThenCopy<U>(c, dst)); |
1331 |
|
while ((s = c.result) == null) { |
1332 |
|
if (UNSAFE.compareAndSwapObject |
1333 |
|
(c, COMPLETIONS, p.next = c.completions, p)) |
2947 |
|
else if ((f = cfs[0]) == null) |
2948 |
|
throw new NullPointerException(); |
2949 |
|
else { |
2950 |
< |
ThenCopy d = null; |
2950 |
> |
ThenPropagate d = null; |
2951 |
|
CompletionNode p = null; |
2952 |
|
Object r; |
2953 |
|
while ((r = f.result) == null) { |
2954 |
|
if (d == null) |
2955 |
< |
d = new ThenCopy(f, dst); |
2955 |
> |
d = new ThenPropagate(f, dst); |
2956 |
|
else if (p == null) |
2957 |
|
p = new CompletionNode(d); |
2958 |
|
else if (UNSAFE.compareAndSwapObject |
3029 |
|
* @throws NullPointerException if the array or any of its elements are |
3030 |
|
* {@code null} |
3031 |
|
*/ |
3032 |
< |
public static CompletableFuture<?> anyOf(CompletableFuture<?>... cfs) { |
3032 |
> |
public static CompletableFuture<Void> anyOf(CompletableFuture<?>... cfs) { |
3033 |
|
int len = cfs.length; // Same idea as allOf |
3034 |
|
if (len > 1) |
3035 |
|
return anyTree(cfs, 0, len - 1); |
3036 |
|
else { |
3037 |
< |
CompletableFuture<?> dst = new CompletableFuture<Object>(); |
3037 |
> |
CompletableFuture<Void> dst = new CompletableFuture<Void>(); |
3038 |
|
CompletableFuture<?> f; |
3039 |
|
if (len == 0) |
3040 |
|
; // skip |
3041 |
|
else if ((f = cfs[0]) == null) |
3042 |
|
throw new NullPointerException(); |
3043 |
|
else { |
3044 |
< |
ThenCopy d = null; |
3044 |
> |
ThenPropagate d = null; |
3045 |
|
CompletionNode p = null; |
3046 |
|
Object r; |
3047 |
|
while ((r = f.result) == null) { |
3048 |
|
if (d == null) |
3049 |
< |
d = new ThenCopy(f, dst); |
3049 |
> |
d = new ThenPropagate(f, dst); |
3050 |
|
else if (p == null) |
3051 |
|
p = new CompletionNode(d); |
3052 |
|
else if (UNSAFE.compareAndSwapObject |
3055 |
|
} |
3056 |
|
if (r != null && (d == null || d.compareAndSet(0, 1))) { |
3057 |
|
Throwable ex; Object t; |
3058 |
< |
if (r instanceof AltResult) { |
3058 |
> |
if (r instanceof AltResult) |
3059 |
|
ex = ((AltResult)r).ex; |
3060 |
< |
t = null; |
3061 |
< |
} |
3039 |
< |
else { |
3040 |
< |
ex = null; |
3041 |
< |
t = r; |
3042 |
< |
} |
3043 |
< |
dst.internalComplete(t, ex); |
3060 |
> |
ex = null; |
3061 |
> |
dst.internalComplete(null, ex); |
3062 |
|
} |
3063 |
|
f.helpPostComplete(); |
3064 |
|
} |
3069 |
|
/** |
3070 |
|
* Recursively constructs an Or'ed tree of CompletableFutures. |
3071 |
|
*/ |
3072 |
< |
private static CompletableFuture<?> anyTree(CompletableFuture<?>[] cfs, |
3073 |
< |
int lo, int hi) { |
3072 |
> |
private static CompletableFuture<Void> anyTree(CompletableFuture<?>[] cfs, |
3073 |
> |
int lo, int hi) { |
3074 |
|
CompletableFuture<?> fst, snd; |
3075 |
|
int mid = (lo + hi) >>> 1; |
3076 |
|
if ((fst = (lo == mid ? cfs[lo] : anyTree(cfs, lo, mid))) == null || |
3077 |
|
(snd = (hi == mid+1 ? cfs[hi] : anyTree(cfs, mid+1, hi))) == null) |
3078 |
|
throw new NullPointerException(); |
3079 |
< |
CompletableFuture<?> dst = new CompletableFuture<Object>(); |
3079 |
> |
CompletableFuture<Void> dst = new CompletableFuture<Void>(); |
3080 |
|
OrCompletion d = null; |
3081 |
|
CompletionNode p = null, q = null; |
3082 |
|
Object r; |
3097 |
|
if ((r != null || (r = fst.result) != null || |
3098 |
|
(r = snd.result) != null) && |
3099 |
|
(d == null || d.compareAndSet(0, 1))) { |
3100 |
< |
Throwable ex; Object t; |
3100 |
> |
Throwable ex; |
3101 |
|
if (r instanceof AltResult) { |
3102 |
|
ex = ((AltResult)r).ex; |
3085 |
– |
t = null; |
3103 |
|
} |
3104 |
< |
else { |
3104 |
> |
else |
3105 |
|
ex = null; |
3106 |
< |
t = r; |
3090 |
< |
} |
3091 |
< |
dst.internalComplete(t, ex); |
3106 |
> |
dst.internalComplete(null, ex); |
3107 |
|
} |
3108 |
|
fst.helpPostComplete(); |
3109 |
|
snd.helpPostComplete(); |