Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jasongoodwin/better-java-monads
https://github.com/jasongoodwin/better-java-monads
Last synced: 4 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/jasongoodwin/better-java-monads
- Owner: jasongoodwin
- License: other
- Created: 2014-10-18T13:42:10.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-12-03T20:07:18.000Z (about 2 months ago)
- Last Synced: 2025-01-15T05:55:39.385Z (11 days ago)
- Language: Java
- Size: 1.01 MB
- Stars: 268
- Watchers: 15
- Forks: 44
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ccamel - jasongoodwin/better-java-monads - (Java)
README
better-java8-monads
==================This library was built immediately after Java8 became GA to help fill in some blanks (Try, Futures.sequence)
There are some other libraries mentioned w/ this project on Stack Overflow: https://stackoverflow.com/questions/27787772/try-monad-in-java-8Feature Overview
----------------*Try* - Optional exists to express nulls in types, but there is no way to express success/failure in types. Try to the rescue! The Try type is very similar to the Try in Scala's standard lib.
*CompletableFuture.sequence* - If you have a list of futures, there is no obvious way to get a future of a list. This will come in handy!
Usage
=====Import into your project:
SBT
---"com.jason-goodwin" % "better-monads" % "0.4.0"
Maven
-----
com.jason-goodwin
better-monads
0.4.0
Try
===The try monad was attributed to Twitter and placed into the Scala standard library.
While both Scala and Haskell have a monad Either which has a left and a right type,
a Try is specifically of a type T on success or an exception on failure.Usage
-----The Try api is meant to be similar to the Optional type so has the same functions.
- get() returns the held value or throws the thrown exception
- getUnchecked() returns the held value or throws the thrown exception wrapped in a RuntimeException instance
- map(x) maps the success value x to a new value and type or otherwise passes the Failure forward.
- flatMap((x) -> f(x)) maps the success value x to a new Try of f(x).
- recover((t) -> x) will return the success value of the Try in the success case or the value x in the failure case. Exposes the exception.
- recoverWith((t) -> f(x)) will return the success value of the Try in the success case or a new try of f(x) in the failure case. Exposes the exception.
- filter((x) -> isTrue(x)) - If Success, returns the same Success if the predicate succeeds, otherwise, returns a Failure with type NoSuchElementException.
- onSuccess((x) -> f(x)) execute some code on success - takes Consumer (eg requires no return value).
- onFailure((x) -> f(x)) execute some code on failure - takes Consumer (eg requires no return value).
- orElse(x) will return the success value of the Try in success case or the value x in failure case.
- orElseTry(f) will return the success value of the Try in success case or a new Try(f) in the failure case.
- orElseThrow(() -> throw new T) gets result or on failure will throw checked exception of type T
- toOptional() will return Optional of success value of Try (if not null), otherwise it will return an empty OptionalFutures
=======
There is no sequence method in the Java8 library so I've provided one. You'll find the function Futures.sequence which will convert a `List>` into a `CompletableFuture>`. This is useful in the common use case of processing all of the elements of a list concurrently.Usage
-----
Simply call Futures.sequence on a `List>` to get back a single future with the list of your items.List list = IntStream.range(0, 100).boxed().collect(Collectors.toList());
int size = list.size();
List> futures = list
.stream()
.map(x -> CompletableFuture.supplyAsync(() -> x))
.collect(Collectors.toList());CompletableFuture> futureList = Futures.sequence(futures);
Tests
=====See the tests for examples of all functionality.