______ _ _ _____ _ _ | ___| (_) / ___(_) | |
______ _____| | | |_ _ __ \
--. _ __| | ___ ______ ______ |______|______| _| | | | '_ \
–. \ |/ _` |/ _ ___|___|| | | | | |_) /\__/ / | (_| | __/ \_| |_|_| .__/\____/|_|\__,_|\___| | | |_|
Flipside
Flipside is a very simple Groovy / Java options library. It includes three distinct kinds of options and a Matcher. That’s it. Easy peasy.
I built it because I especially missed these idioms when transitioning from Scala back to Java / Groovy.
Install
You can install Flipside from the OSS Sonatype Maven repo https://oss.sonatype.org/content/repositories/snapshots/
You will probably need to manually exclude the Groovy dependency.
Maven:
<dependency>
<groupId>com.johnnywey</groupId>
<artifactId>flipside</artifactId>
<version>0.1.26-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</exclusion>
</exclusions>
</dependency>
Gradle:
compile('com.johnnywey:flipside:0.1.26-SNAPSHOT') {
exclude module: 'groovy-all'
}
Grails (versions < 3. For versions >= 3, see Gradle syntax above):
compile('com.johnnywey:flipside:0.1.26-SNAPSHOT') { excludes 'groovy-all' }
All files are built for Java versions >= 1.6.
Fail Enum
Two of the three options take the Fail
enum as part of their constructor params. This is to indicate what went wrong and to try and map the failure back to an HTTP response code. For now, these are hard-coded. In the future, they will be an interface that will allow you to drop in your own failure types.
Option Types
First of all, if you want to know more about Options, start here.
There are three different types of Options in Flipside:
- A
Box
(eitherSome
orNone
) - A
Marker
(eitherWorked
orDidNotWork
) - A
Failable
(eitherSuccess
orFailed
)
They each have distinct uses and similar interfaces.
Failable
A Failable
is similar to the Marker
and is designed for operations that succeed with a value or fail with an error condition. When the operation succeeds, you can call .get()
on the resulting object to get at the value.
import static com.johnnywey.flipside.Failable.*
import com.johnnywey.flipside.failable.Fail
def failed = Failed(Fail.NOT_FOUND, "The thing was not found") // Creates a new one indicating the operation failed and why
def success = Succeeded("It worked!") // Creates one indicating the operation succeeded
assert !failed.isSuccess()
assert success.isSuccess()
assert "The thing was not found" == failed.getDetail()
assert Fail.NOT_FOUND == failed.getReason()
failed.get() // will throw an FailableException with embedded details of the failure
assert success.get() == "It worked!"