All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.polaris.core.err.MultipleException Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
package io.polaris.core.err;

import io.polaris.core.collection.Iterables;
import io.polaris.core.consts.StdConsts;

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

/**
 * @author Qt
 * @since 1.8
 */
public class MultipleException extends IllegalArgumentException {
	private static final long serialVersionUID = 1L;
	private final List failures;

	public MultipleException(String message, List failures) {
		super(message);
		this.failures = failures == null ? Collections.emptyList() : failures;
	}

	public MultipleException(String message, Throwable... failures) {
		super(message);
		this.failures = failures == null ? Collections.emptyList() : Iterables.asList(failures);
	}

	public MultipleException(String message, Iterable failures) {
		super(message);
		this.failures = failures == null ? Collections.emptyList() : Iterables.asList(failures);
	}

	private static String nullSafeMessage(Throwable failure) {
		String str = failure.getMessage();
		if ((str == null || str.trim().length() == 0)) {
			return failure.getClass().getName() + ": ";
		}
		return failure.getClass().getName() + ": " + failure.getMessage();
	}

	@Override
	public String getMessage() {
		int failureCount = this.failures.size();
		String heading = super.getMessage();
		if (failureCount == 0) {
			return heading;
		}
		StringBuilder builder = new StringBuilder(heading)
			.append(" (")
			.append(failureCount).append(" ")
			.append(failureCount == 1 ? "failure" : "failures")
			.append(")")
			.append(StdConsts.EOL);
		int lastIndex = failureCount - 1;
		for (Throwable failure : this.failures.subList(0, lastIndex)) {
			builder.append("\t").append(nullSafeMessage(failure)).append(StdConsts.EOL);
		}
		builder.append('\t').append(nullSafeMessage(this.failures.get(lastIndex)));

		return builder.toString();
	}

	public List getFailures() {
		return Collections.unmodifiableList(this.failures);
	}

	public boolean hasFailures() {
		return !this.failures.isEmpty();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy