Skip to content

ufukhalis/when

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status

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.

<dependency>
    <groupId>io.github.ufukhalis</groupId>
    <artifactId>when</artifactId>
    <version>0.0.6</version>
</dependency>

Then, we can easily build when conditions like below. WHEN won't work until you call the toOptional method.


Optional<Integer> 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.


Optional<Integer> 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.


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);


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.


Mono<Integer> 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.