
io.github.theangrydev.businessflows.SadPath Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of business-flows Show documentation
Show all versions of business-flows Show documentation
A combination of the Try monad and the Either monad, to help tame complex business flows.
/*
* 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;
/**
* A {@link SadPath} is a {@link BusinessFlow} that is biased towards the result being {@link Sad}.
*
* {@inheritDoc}
*/
public class SadPath extends BusinessFlow {
SadPath(BusinessCase businessCase) {
super(BusinessCase::sadOptional, businessCase);
}
/**
* @param sad The sad object to initiate the flow with
* @param The type of happy object the resulting {@link SadPath} may represent
* @param The type of sad object the resulting {@link SadPath} may represent
* @return A {@link SadPath} that is sad on the inside
*/
public static SadPath sadPath(Sad sad) {
return new SadPath<>(new SadCase<>(sad));
}
/**
* @param happy The happy object to initiate the flow with
* @param The type of happy object the resulting {@link SadPath} may represent
* @param The type of sad object the resulting {@link SadPath} may represent
* @return A {@link SadPath} that is happy on the inside
*/
public static SadPath happyPath(Happy happy) {
return new SadPath<>(new HappyCase<>(happy));
}
/**
* @param technicalFailure The technical failure object to initiate the flow with
* @param The type of happy object the resulting {@link SadPath} may represent
* @param The type of sad object the resulting {@link SadPath} may represent
* @return A {@link SadPath} that is a technical failure on the inside
*/
public static SadPath technicalFailure(Exception technicalFailure) {
return new SadPath<>(new TechnicalFailureCase<>(technicalFailure));
}
/**
* If the underlying business case is sad, then apply the given action, otherwise do nothing to the underlying case.
*
* @param action The action to apply to an existing sad case
* @param The type of sad object that will be present after the action is applied to an existing sad object
* @return The result of applying the action to the existing sad path, if applicable
*/
public SadPath then(Mapping> action) {
return join(SadPath::happyPath, action, SadPath::technicalFailure);
}
/**
* If the underlying business case is sad, then apply the given mapping, otherwise do nothing to the underlying case.
*
* @param mapping The action to apply to an existing sad case
* @param The type of sad object that will be present after the mapping is applied to an existing sad object
* @return The result of applying the mapping to the existing sad path, if applicable
*/
public SadPath map(Mapping mapping) {
return then(mapping.andThen(SadPath::sadPath));
}
/**
* If the underlying business case is sad, recover to a happy path using the given recovery mapping.
*
* @param recovery The recovery to apply to an existing sad case
* @return The result of applying the recovery to the existing sad path, if applicable
*/
public HappyPath recover(Mapping recovery) {
return this.then(sad -> happyPath(recovery.map(sad))).ifHappy();
}
/**
* If the underlying business case is sad, recover to a happy path using the {@link Attempt}.
*
* @param recovery The recovery to apply to an existing sad case
* @return The result of applying {@link Attempt}, if applicable
*/
public HappyPath recover(Attempt recovery) {
return recover(sad -> recovery.attempt());
}
/**
* Take a look at the sad case (if there really is one).
*
* @param peek What to do if the underlying business case is sad
* @return The same {@link SadPath}
*/
public SadPath peek(Peek peek) {
return then(sad -> {
peek.peek(sad);
return this;
});
}
/**
* {@inheritDoc}
*/
@Override
public SadPath ifSad() {
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy