@SuppressWarnings("unchecked")
  public static <T extends ScanTask> List<T> mergeTasks(List<T> tasks) {
    List<T> mergedTasks = Lists.newArrayList();

    T lastTask = null;

    for (T task : tasks) {
      if (lastTask != null) {
        if (lastTask instanceof MergeableScanTask<?>) {
          MergeableScanTask<? extends T> mergeableLastTask =
              (MergeableScanTask<? extends T>) lastTask;
          if (mergeableLastTask.canMerge(task)) {
            lastTask = mergeableLastTask.merge(task);
          } else {
            mergedTasks.add(lastTask);
            lastTask = task;
          }
        } else {
          mergedTasks.add(lastTask);
          lastTask = task;
        }
      } else {
        lastTask = task;
      }
    }

    if (lastTask != null) {
      mergedTasks.add(lastTask);
    }

    return mergedTasks;
  }
@Test
  public void testTaskMerging() {
    List<ParentTask> tasks =
        ImmutableList.of(
            new ChildTask1(64),
            new ChildTask1(64),
            new ChildTask2(128),
            new ChildTask3(32),
            new ChildTask3(32));
    List<ParentTask> mergedTasks = TableScanUtil.mergeTasks(tasks);
    assertThat(mergedTasks).as("Appropriate tasks should be merged").hasSize(3);
  }