fun whenAll()

in facebook-bolts/src/main/java/com/facebook/bolts/Task.kt [842:880]


    fun whenAll(tasks: Collection<Task<*>>): Task<Void> {
      if (tasks.isEmpty()) {
        return forResult(null)
      }
      val allFinished = TaskCompletionSource<Void>()
      val causes = ArrayList<Exception?>()
      val errorLock = ReentrantLock()
      val count = AtomicInteger(tasks.size)
      val isCancelled = AtomicBoolean(false)
      for (task in tasks) {
        val t = task as Task<Any>
        t.continueWith {
          if (it.isFaulted) {
            errorLock.withLock { causes.add(it.error) }
          }
          if (it.isCancelled) {
            isCancelled.set(true)
          }
          if (count.decrementAndGet() == 0) {
            if (causes.size != 0) {
              if (causes.size == 1) {
                allFinished.setError(causes[0])
              } else {
                val error: Exception =
                    AggregateException(
                        String.format("There were %d exceptions.", causes.size), causes)
                allFinished.setError(error)
              }
            } else if (isCancelled.get()) {
              allFinished.setCancelled()
            } else {
              allFinished.setResult(null)
            }
          }
          null
        }
      }
      return allFinished.task
    }