com.memority.toolkit.rule.api.ActionOutcomeException Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of toolkit-rule-api Show documentation
Show all versions of toolkit-rule-api Show documentation
This artifact provides the API classes that are necessary to implement the contracts of Memority configuration Rules.
/*
* Copyright (c) 2016-2023 Memority. All Rights Reserved.
*
* This file is part of Memority Toolkit API , a Memority project.
*
* This file is released under the Memority Public Artifacts End-User License Agreement,
* see
* Unauthorized copying of this file, via any medium is strictly prohibited.
*/
package com.memority.toolkit.rule.api;
import lombok.Getter;
import org.apache.commons.lang3.Validate;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList;
/**
* Simple exception that expresses that the outcome of a set of actions was {@link ActionOutcome.Outcome#FAILURE}.
* This mostly holds the failed outcomes and generate a standard message from their failure message.
*
* @see ActionOutcome#isFailure()
* @see ActionOutcome#getFailureMessage()
*/
@Getter
public class ActionOutcomeException extends Exception {
private final List failureOutcomes;
/**
* Instantiate a new exception from the given collection of outcomes. Non failed outcomes are ignored.
*
* @param outcomes the outcomes to construct the exception from
* @throws IllegalArgumentException if there is no failure outcome
*/
public ActionOutcomeException(Collection outcomes) {
super(messageFromOutcomes(outcomes));
this.failureOutcomes = outcomes.stream().filter(ActionOutcome::isFailure).collect(toList());
Validate.notEmpty(failureOutcomes, "There must be at least one failed outcome!");
}
private static String messageFromOutcomes(Collection outcomes) {
return outcomes.stream()
.filter(ActionOutcome::isFailure)
.map(ActionOutcome::getFailureMessage)
.collect(Collectors.joining(" / "));
}
}