| 167 | } |
| 168 | |
| 169 | private static class Many<T> implements CombinedBuilder<T> { |
| 170 | |
| 171 | private final Object[] array; |
| 172 | private int ix; |
| 173 | private int cfCount; |
| 174 | |
| 175 | private Many(int size) { |
| 176 | this.array = new Object[size]; |
| 177 | this.ix = 0; |
| 178 | cfCount = 0; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void add(CompletableFuture<T> completableFuture) { |
| 183 | array[ix++] = completableFuture; |
| 184 | cfCount++; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void addObject(Object object) { |
| 189 | array[ix++] = object; |
| 190 | if (object instanceof CompletableFuture) { |
| 191 | cfCount++; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | @SuppressWarnings("unchecked") |
| 196 | @Override |
| 197 | public CompletableFuture<List<T>> await() { |
| 198 | commonSizeAssert(); |
| 199 | |
| 200 | CompletableFuture<List<T>> overallResult = new CompletableFuture<>(); |
| 201 | if (cfCount == 0) { |
| 202 | overallResult.complete(materialisedList(array)); |
| 203 | } else { |
| 204 | CompletableFuture<T>[] cfsArr = copyOnlyCFsToArray(); |
| 205 | CompletableFuture.allOf(cfsArr) |
| 206 | .whenComplete((ignored, exception) -> { |
| 207 | if (exception != null) { |
| 208 | overallResult.completeExceptionally(exception); |
| 209 | return; |
| 210 | } |
| 211 | List<T> results = new ArrayList<>(array.length); |
| 212 | if (cfsArr.length == array.length) { |
| 213 | // they are all CFs |
| 214 | for (CompletableFuture<T> cf : cfsArr) { |
| 215 | results.add(cf.join()); |
| 216 | } |
| 217 | } else { |
| 218 | // it's a mixed bag of CFs and materialized objects |
| 219 | for (Object object : array) { |
| 220 | if (object instanceof CompletableFuture) { |
| 221 | CompletableFuture<T> cf = (CompletableFuture<T>) object; |
| 222 | // join is safe since they are all completed earlier via CompletableFuture.allOf() |
| 223 | results.add(cf.join()); |
| 224 | } else { |
| 225 | results.add((T) object); |
| 226 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…