/*
 * Copyright 2012-2014 JetBrains s.r.o
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.jetpad.base;

import java.util.*;

public class CompositeAsync<ItemT> extends SimpleAsync<List<ItemT>> {
  private SortedMap<Integer, ItemT> mySucceeded = new TreeMap<>();
  private List<Throwable> myFailures = new ArrayList<>(0);
  private int myAsyncsCounter = 0;

  public CompositeAsync(List<Async<ItemT>> asyncs) {
    myAsyncsCounter = asyncs.size();

    for (int i = 0; i < asyncs.size(); i++) {
      final int finalI = i;
      asyncs.get(i).onSuccess(new Handler<ItemT>() {
        @Override
        public void handle(ItemT item) {
          mySucceeded.put(finalI, item);
          myAsyncsCounter--;
          onComponentResult();
        }
      }).onFailure(new Handler<Throwable>() {
        @Override
        public void handle(Throwable item) {
          myFailures.add(item);
          myAsyncsCounter--;
          onComponentResult();
        }
      });
    }

    if (asyncs.isEmpty()) {
      onComponentResult();
    }
  }

  private void onComponentResult() {
    if (myAsyncsCounter != 0) return;

    if (myFailures.isEmpty()) {
      success(new ArrayList<>(mySucceeded.values()));
    } else {
      if (myFailures.size() == 1) {
        failure(myFailures.get(0));
      } else {
        failure(new ThrowableCollectionException(myFailures));
      }
    }
  }
}
@@ -878,16 +878,52 @@
 ter = 0;
+%0A  private int mySuccessCounter = 0;
 %0A%0A  publ
@@ -1006,17 +1006,16 @@
 size();%0A
-%0A
     for 
@@ -1019,92 +1019,50 @@
 or (
-int i = 0; i %3C asyncs.size(); i++) %7B%0A      final int finalI = i;%0A      asyncs.get(i)
+Async%3CItemT%3E async : asyncs) %7B%0A      async
 .onS
@@ -1180,14 +1180,26 @@
 put(
-finalI
+mySuccessCounter++
 , it

improve asyncs success counter at CompositeAsync