io.github.theangrydev.businessflows.BusinessCase Maven / Gradle / Ivy
/*
* Copyright 2016 Liam Williams .
*
* This file is part of business-flows.
*
* 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 io.github.theangrydev.businessflows;
import java.util.function.Function;
import static java.lang.String.format;
/**
* A {@link BusinessCase} is either a {@link HappyCase}, a {@link SadCase} or a {@link TechnicalFailure}.
*
* @param The type of happy object this case may represent
* @param The type of sad object this case may represent
*/
interface BusinessCase {
/**
* Turn this {@link BusinessFlow} into a {@link PotentialFailure} that will be a success if the business case is a
* success and otherwise will map the business case to a failure.
*
* @param technicalFailureMapping The mapping to apply to turn an existing technical failure into a {@link Sad}
* @return A {@link PotentialFailure} that will be a failure if the business case is sad or a technical failure
*/
PotentialFailure toPotentialFailure(Function technicalFailureMapping);
/**
* Join to a common result type. No matter what the {@link BusinessCase} actually is, the result type is the same.
*
* @param happyJoiner What to do if this is a {@link HappyCase}
* @param sadJoiner What to do if this is a {@link SadCase}
* @param technicalFailureJoiner What to do if this is a {@link TechnicalFailureCase}
* @param The type of the result
* @return The result after applying the joiner that corresponds to the underlying business case
*/
Result join(Mapping happyJoiner, Mapping sadJoiner, Function technicalFailureJoiner);
/**
* Same as {@link #join(Mapping, Mapping, Function)} but if the {@link BusinessCase} is a
* {@link TechnicalFailureCase}, then the underlying exception will be thrown instead of joined.
*
* @param happyJoiner What to do if this is a {@link HappyCase}
* @param sadJoiner What to do if this is a {@link SadCase}
* @param The type of the result
* @return The result after applying the joiner that corresponds to the underlying business case
* @throws Exception If this is a {@link TechnicalFailureCase}.
*/
Result joinOrThrow(Mapping happyJoiner, Mapping sadJoiner) throws Exception;
/**
* Same as {@link #join(Mapping, Mapping, Function)} but if the {@link BusinessCase} is a {@link TechnicalFailureCase},
* then the underlying exception will be thrown as a {@link RuntimeException} instead of joined.
*
* @param happyJoiner What to do if the underlying business case is a happy case
* @param sadJoiner What to do if the underlying business case is a sad case
* @param The type of the result
* @return The result after applying the joiner that corresponds to the underlying business case
* @throws IllegalStateException If this is a {@link TechnicalFailureCase} or there is a failure when joining.
*/
default Result join(Mapping happyJoiner, Mapping sadJoiner) throws IllegalStateException {
try {
return joinOrThrow(happyJoiner, sadJoiner);
} catch (Exception e) {
throw new IllegalStateException(format("Exception caught when joining. Business case is: '%s'.", this), e);
}
}
}