https://github.com/ashr123/option
DOP version of Optional
https://github.com/ashr123/option
java java-21 maybe maybe-monad monad nothing option option-monad some
Last synced: 5 months ago
JSON representation
DOP version of Optional
- Host: GitHub
- URL: https://github.com/ashr123/option
- Owner: ashr123
- License: mit
- Created: 2024-02-23T16:24:36.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-09-22T06:54:03.000Z (9 months ago)
- Last Synced: 2025-09-22T08:43:16.063Z (9 months ago)
- Topics: java, java-21, maybe, maybe-monad, monad, nothing, option, option-monad, some
- Language: Java
- Homepage:
- Size: 91.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
[](https://search.maven.org/search?q=g:%22io.github.ashr123%22%20AND%20a:%22option%22)
# Option
Small package that brings DOP of Optional to Java.
Gives you the possibility to write code as
```java
import io.github.ashr123.option.None;
import io.github.ashr123.option.Option;
import io.github.ashr123.option.Some;
public record Pair(L left, R right) {
}
public static Integer intABSOption(Integer integer) {
return switch (Option.of(integer)) {
case Some(Integer i) when i < 0 -> -i;
case Some(Integer i) -> i;
case None ignored -> null;
};
}
public static Integer intMax(Pair pair) {
return switch (Option.of(pair)) {
case Some(Pair(Integer l, Integer r)) when l != null && r == null -> l;
case Some(Pair(Integer l, Integer r)) when l == null && r != null -> r;
case Some(Pair(Integer l, Integer r)) when l != null && r != null && l > r -> l;
case Some(Pair(Integer l, Integer r)) -> r;
case None> ignored -> null;
};
}
```
## Comparison against existing `Optional`
| `Optional` methods | `Option opt` equivalent |
|------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|
| `empty()` | `None.instance()` |
| `of(T value)` | `new Some<>(T value)` |
| `ofNullable(T value)` |
- `of(T value)`
- `of(Optional value)`
| `get()` |
- switch-case
- `if (opt instanceof Some(T value)) ...`
| `isPresent()` | `if (opt instanceof Some>) ...` |
| `isEmpty()` | `if (opt instanceof None>) ...` |
| `ifPresent(Consumer super T> action)` |
- switch-case
- `if (opt instanceof Some(T value)) value ...`
| `ifPresentOrElse(Consumer super T> action, Runnable emptyAction)` | switch-case |
| `filter(Predicate super T> predicate)` | `if (opt instanceof Some(T value) && value ...)` |
| `map(Function super T, ? extends U> mapper)` |
- `if (opt instanceof Some(T value)) value ...`
- `map(Function super T, ? extends U> mapper)`
| `flatMap(Function super T, ? extends Optional extends U>> mapper)` | `flatMap(Function super T, ? extends Option extends U>> mapper)` |
| `or(Supplier extends Optional extends T>> supplier)` | switch-case |
| `stream()` | `stream()` |
| `orElse(T other)` | switch-case |
| `orElseGet(Supplier extends T> supplier)` | switch-case |
| `orElseThrow()` | switch-case |
| `orElseThrow(Supplier extends X> exceptionSupplier)` | switch-case |
Requires JRE 21 or above.