https://github.com/ufukhalis/when
Simple Matching
https://github.com/ufukhalis/when
ifelse pattern-matching predicate-methods switchcase
Last synced: about 2 months ago
JSON representation
Simple Matching
- Host: GitHub
- URL: https://github.com/ufukhalis/when
- Owner: ufukhalis
- License: apache-2.0
- Created: 2020-03-06T20:37:04.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-13T06:53:10.000Z (almost 6 years ago)
- Last Synced: 2025-07-06T07:06:48.128Z (8 months ago)
- Topics: ifelse, pattern-matching, predicate-methods, switchcase
- Language: Java
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/ufukhalis/when)
[](https://coveralls.io/github/ufukhalis/when?branch=master)
WHEN
=======
`WHEN` is a simple matching library which only needs Java 8+ version.
How to Use
-------------
Firstly, you should add latest `WHEN` dependency to your project.
```$xslt
io.github.ufukhalis
when
0.0.6
```
Then, we can easily build when conditions like below. `WHEN` won't work until you call the `toOptional`
method.
```$xslt
Optional result = When.of(integer)
.condition(i -> i == 10, i -> i + 1)
.condition(i -> i == 20, i -> i + 2)
.toOptional();
```
If there is no match the optional value will be empty.
You can also pass `Optional`.
```$xslt
Optional result = When.of(Optional.of(10))
.condition(i -> i == 12, i -> i + 1)
.condition(i -> i == 11, i -> i + 1)
.condition(i -> i == 10, i -> i + 1)
.toOptional();
```
And also you can use other methods to trigger pipeline such as `getOrElse` or `getOrElseGet`.
```$xslt
Integer result = When.of(integer)
.condition(i -> i == 11, i -> i + 1)
.condition(i -> i == 12, i -> i + 1)
.condition(i -> i == 13, i -> i + 1)
.getOrElseGet(() -> 10);
```
```$xslt
Integer result = When.of(integer)
.condition(i -> i == 11, i -> i + 1)
.condition(i -> i == 12, i -> i + 1)
.condition(i -> i == 13, i -> i + 1)
.getOrElse(10);
```
`When` project has also reactor `Mono` type support.
```$xslt
Mono result = When.of(Mono.just(10))
.condition(i -> i == 10, i -> 1)
.condition(i -> i == 20, i -> 2)
.execute();
```
Important Note : If there are multiple match for `When`, it will return the last match.
But it won't execute previous matches.
License
------------
All code in this repository is licensed under the Apache License, Version 2.0. See [LICENCE](./LICENSE).