An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

[![Maven Central](https://img.shields.io/maven-central/v/io.github.ashr123/option.svg?label=Maven%20Central)](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.