when

Simple Pattern Matching

License

License

GroupId

GroupId

io.github.ufukhalis
ArtifactId

ArtifactId

when
Last Version

Last Version

0.0.6
Release Date

Release Date

Type

Type

jar
Description

Description

when
Simple Pattern Matching
Project URL

Project URL

https://github.com/ufukhalis/when
Source Code Management

Source Code Management

https://github.com/ufukhalis/when/tree/master

Download when

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.ufukhalis/when/ -->
<dependency>
    <groupId>io.github.ufukhalis</groupId>
    <artifactId>when</artifactId>
    <version>0.0.6</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.ufukhalis/when/
implementation 'io.github.ufukhalis:when:0.0.6'
// https://jarcasting.com/artifacts/io.github.ufukhalis/when/
implementation ("io.github.ufukhalis:when:0.0.6")
'io.github.ufukhalis:when:jar:0.0.6'
<dependency org="io.github.ufukhalis" name="when" rev="0.0.6">
  <artifact name="when" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.ufukhalis', module='when', version='0.0.6')
)
libraryDependencies += "io.github.ufukhalis" % "when" % "0.0.6"
[io.github.ufukhalis/when "0.0.6"]

Dependencies

compile (2)

Group / Artifact Type Version
io.projectreactor : reactor-core jar 3.3.3.RELEASE
io.projectreactor : reactor-test jar 3.3.3.RELEASE

test (1)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter jar 5.4.2

Project Modules

There are no modules declared in this project.

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.

Versions

Version
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1