Package org.organicdesign.fp.oneOf
Interface Or<G,B>
-
public interface Or<G,B>
`Or` represents the presence of a successful outcome, or an error. Contrast this with Option which represents the presence or absence of a value. Option.Some and Or.Good are just about identical. Unlike Option.None, Bad contains an error code or value. Bill Venners, Scalactic, SuperSafe, and Functional Error Handling talk at SF Scala 2015-02-24 convinced me that Or is friendlier than Either. This class is based on Bill Venners' Or. I did not make Every, One, and Many sub-classes, figuring that you can make an Or<GoodType,ImList<BadType>> if you expect that. Bill makes the point that there are still some reasons to throw exceptions, but he says to "Throw exceptions at developers, not at code" meaning that if there's code in your program that can recover from the issue, use a functional return type (like Or). Throw exceptions for things a program can't handle without developer intervention. Any errors are my own. This implementation is more like a sealed trait (in Kotlin or Scala) than a simpleOneOf2
union type. This makes it a little less general, and more meaningful to use.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description B
bad()
Returns the bad value if this is a Bad, or throws an exception if this is a Good.static <G,B>
Or<G,B>bad(B bad)
Construct a new Bad from the given object.G
good()
Returns the good value if this is a Good, or throws an exception if this is a Bad.static <G,B>
Or<G,B>good(G good)
Construct a new Good from the given object.boolean
isBad()
Returns true if this Or has a bad value.boolean
isGood()
Returns true if this Or has a good value.<R> R
match(Fn1<G,R> fg, Fn1<B,R> fb)
Exactly one of these functions will be executed - determined by whether this is a Good or a Bad.
-
-
-
Method Detail
-
good
static <G,B> Or<G,B> good(G good)
Construct a new Good from the given object.
-
bad
static <G,B> Or<G,B> bad(B bad)
Construct a new Bad from the given object.
-
isGood
boolean isGood()
Returns true if this Or has a good value.
-
isBad
boolean isBad()
Returns true if this Or has a bad value.
-
good
G good()
Returns the good value if this is a Good, or throws an exception if this is a Bad.
-
bad
B bad()
Returns the bad value if this is a Bad, or throws an exception if this is a Good.
-
match
<R> R match(Fn1<G,R> fg, Fn1<B,R> fb)
Exactly one of these functions will be executed - determined by whether this is a Good or a Bad.- Parameters:
fg
- the function to be executed if this OneOf stores the first type.fb
- the function to be executed if this OneOf stores the second type.- Returns:
- the return value of whichever function is executed.
-
-