package com.rtlabs.reqtool.util;

import static java.util.stream.Collectors.toList;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.eclipse.core.runtime.IStatus;

/**
 * Used to return a value from some operation, together with status information about the operation.
 * 
 * This is useful in the following scenarios:
 * - Returning the result from an operation which may fail with an error message.
 * - Returning a result together with information and/or warnings from the operation. 
 * 
 * @param <T> The type of the result value
 */
public class Result<T> {
	private final T result;
	private final List<IStatus> statuses;

	public Result(T result, List<IStatus> statuses) {
		this.result = result;
		this.statuses = Objects.requireNonNull(statuses);
	}

	public Result(T result) {
		this(result, Collections.emptyList());
	}

	public static <T> Result<T> failure(List<IStatus> statuses) {
		return new Result<>(null, statuses);
	}
	
	public boolean isNoErrors() {
		return isAllLessSevere(IStatus.ERROR);
	}

	public boolean isNoWarnings() {
		return isAllLessSevere(IStatus.WARNING);
	}
	
	public boolean isAllOk() {
		return isAllLessSevere(IStatus.INFO);
	}
	
	public List<String> getAllMessages() {
		return statuses.stream().map(s -> s.getMessage()).collect(toList());
	}

	public boolean isAllLessSevere(int severity) {
		return statuses.stream().allMatch(s -> s.getSeverity() < severity);
	}
	
	public T getResult() {
		return result;
	}

	public List<IStatus> getStatuses() {
		return statuses;
	}
}

@@ -1548,10 +1548,113 @@
 ses;%0A%09%7D%0A
+%09%0A%09public String toString() %7B%0A%09%09return getClass().getSimpleName() + %22%3C%22 + result + %22, %22 + statuses;%0A%09%7D%0A
 %7D%0A

Add Result toString.