Business Flows
A combination of the Try monad and the Either monad, to help tame complex business flows
Example Usage 1 (thanks @tjheslin1)
Example Usage 2 (thanks @hanfak)
Frequently asked questions
What is it?
A small Java 8 library that can help you to model a 3-part (happy/sad/exception) business flow. No more try-catch blocks if you don't want to use them!
When should I use it?
If you have a flow that fits into the "two track" model, as spoken about by Scott W in his Railyway Oriented Programming model, which this library owes a lot to. This library has 3 tracks instead of 2; the third is supposed to be reserved for "stuff that is never supposed to happen" in the form of uncaught exceptions.
When should I not use it?
There is no point in using this if your flows are "all or nothing". If there are no expected sad paths and all failures are exceptional circumstances, then this is not a good fit. The Try monad is a better fit. You could use e.g. better-java-monads instead.
How to get it?
<dependency>
<groupId>io.github.theangrydev</groupId>
<artifactId>business-flows</artifactId>
<version>10.3.0</version>
</dependency>
Releases
The versioning scheme follows Semantic Versioning 2.0.0, to help you identify non backwards-compatible changes when you are upgrading.
10.3.0
- Added
BusinessFlow.isTechnicalFailuremethod to complement the existingisHappyandisSadmethods - Added
BusinessFlow.getTechnicalFailuremethod to complement the existinggetHappyandgetSadmethods
10.2.0
- Starting to document the API with some new annotations
@ApiFeature, which describesApiFeatureStabilityfor a version in theApiVersionHistory. These features are EXPERIMENTAL and may be removed at any time
10.1.1 to 10.1.12
- Experimenting with the release process and generating documentation, no functional changes
10.1.0
HappyPath.happyPathAttemptnow accepts anyBusinessFlow, not just aHappyPath, which is useful when you have a collaborator that returns aBusinessFlowthat is not aHappyPathview (closes #13)
10.0.0
- This release picks up where 8.3.0 left off, just reverting the version 9.0.0 and 9.1.0 changes
9.0.0 and 9.1.0
- These releases were a failed attempt to remove the
HappyPath.happyAttemptmethod that takes anAttempt<Happy>parameter (see #12)
8.3.0
- Add methods
consumeandconsumeOrThrowto end a flow by performing some action that ends in a void (closes #8)
8.2.0
- Add methods to help with multiple return style:
isHappy,isSad,getHappyandgetSad(closes #10)
8.1.1
throwItAsARuntimeExceptionandthrowItshould not be void (closes #5)
8.0.0
- Removed the
Biasparameter fromBusinessFlowto make it more viable to use as an unbiased return type, instead of having to wildcard the types as<Happy, Sad, ?>. This is a breaking change. - Made the
thenmethods accept? extends BusinessFlow.
7.6.0
- Exposed some methods on
PotentialFailureto make it easier to test when it is the return type of a method. This involved extracting aWithOptionalinterface that is used byPotentialFailureandBusinessFlow.
7.5.0
- Added helper method
HappyPath.actionsthat turns an array ofActionThatMightFailinto a list. This is a workaround for the fact that interfaces can't have@SafeVarargsand so some methods accept a list when they would ideally accept varargs
7.4.0
- Added
HappyPath.attemptAllconvenience method that is equivalent to multiple chainedattemptcalls
7.3.0
- Added method
TechnicalFailure.throwItAsRuntimeExceptionthat will throw aRuntimeExceptionif the business case is a technical failure
7.2.0
- Added method
TechnicalFailure.throwItthat will throw anExceptionif the business case is a technical failure
7.0.0
- Changed
Validatorso that it produces just one failure not a list of failures FieldValidatornow adapts just oneValidatornot a list of them to match theValidatorchange- Renamed
ValidationPathmethods fromvalidate*tovalidateAll*to make it clear that they run all the validators not just until the first failure
6.2.0
TechnicalFailureCase.toStringnow includes the stack trace
6.1.0
- Added method
BusinessCase.toPotentialFalurethat turns aBusinessCaseinto aPotentialFailure
6.0.0
ValidationPathnow has aSadAggregatetype parameter that defaults toList<Sad>and can be used to map validation errors into an aggregate. ValidationPath now has a SadAggregate type parameter that defaults to List and can be used to map validation errors into an aggregate. There are correspondingvalidateIntomethods inValidationPaththat allow specifying aMappingto aSadAggregateandvalidatemethods that default toList<Sad>. This change is not backwards compatible
5.1.2
- Helper methods for
FieldValidatorto cope with the case that a common field name should be passed to validators so they can use the name in the validation message
5.0.0
- Validation revamp. Helper
FieldValidatorfor field validation, a newValidatortype,PotentialFailure.failureshelper method andValidationPathhelper methods. This change is not backwards compatible
4.0.3
- Updated javadoc and license headers
4.0.1
- Inline methods to make debugging easier (closes #1)
- Removed varargs methods from
ValidationPath. This change is not backwards compatible - Removed
andThenmethods fromAttemptandMapping. This change is not backwards compatible
3.1.1
- Optimized overrides for
ifHappyetc (closes #2)
3.1.0
- Allow
? extends ActionThatMightFail<Happy, Sad>in theValidationPath
3.0.2
- Made
PotentialFailure.toHappyPathpackage private since it is only supposed to be used internally
3.0.1
- License headers updated
3.0.0
- Using a new class
PotentialFailureto represent the result of anActionThatMightFailinstead of anOptional. This change is not backwards compatible
2.7.1
- Updated javadoc
2.7.0
- If a technical failure occurs while joining to happy or sad, it is joined to a technical failure instead
2.6.0
- Unchecked version of
joinwith only happy and sad joiners has taken thejoinname. The checked version is calledjoinOrThrownow
2.5.0
- Recovery methods no longer need to be e.g.
exception -> sad, they can choose to ignore the parameter and just supply a value, e.g.() -> sad
2.4.0
- Added
HappyPath.happyPathAttemptto cover the case where the entry point to a flow is a method that could e.g. return eitherHappyPath.happyPathorSadPath.sadPathand may also throw an uncaught exception
2.3.0
- Made all the static factory methods for
HappyPath,SadPathandTechnicalFailurepublic so that it is possible to do e.g.HappyPath.sadPath(sad)to get a happy biased view of a sad path without having to doSadPath.sadPath(sad).ifHappy()
2.2.0
- Updated javadoc
2.1.0
HappyAttemptnow has an exception to technical failure factory method and an exception to sad path factory method
2.0.0
- Switched order from
<Sad, Happy>to<Happy, Sad>because it is more intuitive to users. This change is not backwards compatible
1.1.0
- Added another
joinmethod in which you can omit the technical failure mapping and allow it to be thrown as anException - Reduced visibility of methods that should have been
private
1.0.1
- Changed
orElseThrowso that it throwsX extends Exceptionrather thanX extends Throwable - Changed
getfailure message wording
1.0.0
- Initial stab at Business Flows