项目作者: ufukhalis

项目描述 :
Simple Matching
高级语言: Java
项目地址: git://github.com/ufukhalis/when.git
创建时间: 2020-03-06T20:37:04Z
项目社区:https://github.com/ufukhalis/when

开源协议:Apache License 2.0

下载


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.

  1. <dependency>
  2. <groupId>io.github.ufukhalis</groupId>
  3. <artifactId>when</artifactId>
  4. <version>0.0.6</version>
  5. </dependency>

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

  1. Optional<Integer> result = When.of(integer)
  2. .condition(i -> i == 10, i -> i + 1)
  3. .condition(i -> i == 20, i -> i + 2)
  4. .toOptional();

If there is no match the optional value will be empty.

You can also pass Optional.

  1. Optional<Integer> result = When.of(Optional.of(10))
  2. .condition(i -> i == 12, i -> i + 1)
  3. .condition(i -> i == 11, i -> i + 1)
  4. .condition(i -> i == 10, i -> i + 1)
  5. .toOptional();

And also you can use other methods to trigger pipeline such as getOrElse or getOrElseGet.

  1. Integer result = When.of(integer)
  2. .condition(i -> i == 11, i -> i + 1)
  3. .condition(i -> i == 12, i -> i + 1)
  4. .condition(i -> i == 13, i -> i + 1)
  5. .getOrElseGet(() -> 10);
  1. Integer result = When.of(integer)
  2. .condition(i -> i == 11, i -> i + 1)
  3. .condition(i -> i == 12, i -> i + 1)
  4. .condition(i -> i == 13, i -> i + 1)
  5. .getOrElse(10);

When project has also reactor Mono type support.

  1. Mono<Integer> result = When.of(Mono.just(10))
  2. .condition(i -> i == 10, i -> 1)
  3. .condition(i -> i == 20, i -> 2)
  4. .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.