private static List<TaskBucket> buckets(List<Task> tasks) {
    if (null == tasks || tasks.isEmpty()) {
      return Collections.emptyList();
    }

    List<TaskBucket> holders = new ArrayList<>();

    TaskBucket current = null;

    int i = 0;
    for (Task t : tasks) {
      if (i == 0) {
        current = new TaskBucket(t);
        holders.add(current);
      } else if (i > 0 && t.getType() != tasks.get(i-1).getType()) {
        current = new TaskBucket(t);
        holders.add(current);
      } else {
        current.tasks.add(t);
      }

      i++;
    }

    return holders;
  }
Group all like-typed tasks together.  When they change, create a new type.
ambari-server/src/main/java/org/apache/ambari/server/stack/upgrade/Grouping.java