/*
 * Copyright (c) 2015 JBYoshi
 *
 * 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 jbyoshi.gitupdate;

import java.util.*;
import java.util.function.*;

public final class Task {
	private final Consumer<Report> code;
	private final Set<Task> children = new LinkedHashSet<>();
	private final Report report;

	Task(String text) {
		this(null, text, report -> {
		});
	}

	private Task(Report parentReport, String text, Consumer<Report> code) {
		this.report = new Report(parentReport, text);
		this.code = code;
	}

	public Task newChild(String text) {
		return newChild(text, report -> {
		});
	}

	public Task newChild(String text, Consumer<Report> code) {
		Task child = new Task(report, text, code);
		children.add(child);
		return child;
	}

	void start() {
		pre();
		go();
	}

	private void pre() {
		report.future = true;
		report.stateChanged();
		for (Task child : children) {
			child.pre();
		}
	}

	private void go() {
		report.future = false;
		report.working = true;
		report.stateChanged();
		code.accept(report);
		for (Task child : children) {
			child.go();
		}
		report.working = false;
		report.done = true;
		report.stateChanged();
	}
}

@@ -1003,16 +1003,75 @@
  text);%0A
+%09%09this.report.future = true;%0A%09%09this.report.stateChanged();%0A
 %09%09this.c
@@ -1082,16 +1082,16 @@
 = code;%0A
-
 %09%7D%0A%0A%09pub
@@ -1337,177 +1337,8 @@
 ) %7B%0A
-%09%09pre();%0A%09%09go();%0A%09%7D%0A%0A%09private void pre() %7B%0A%09%09report.future = true;%0A%09%09report.stateChanged();%0A%09%09for (Task child : children) %7B%0A%09%09%09child.pre();%0A%09%09%7D%0A%09%7D%0A%0A%09private void go() %7B%0A
 %09%09re
@@ -1472,18 +1472,21 @@
 %09%09child.
-go
+start
 ();%0A%09%09%7D%0A

Make new tasks immediately appear as future.