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

https://github.com/muthuishere/declarativex

Composable approach to exception & conditions in Java
https://github.com/muthuishere/declarativex

declarative-programming exception-handling java

Last synced: 5 months ago
JSON representation

Composable approach to exception & conditions in Java

Awesome Lists containing this project

README

        

### DeclarativeX
###### A Composable approach to exception & conditions in Java

![Coverage](.github/badges/jacoco.svg)
![Branches](.github/badges/branches.svg)

![Composing Exceptions with DeclarativeX](https://github.com/muthuishere/declarativex-example/blob/main/assets/withthis1.png?raw=true)

----

![Composing Exceptions with DeclarativeX for same parameters](https://github.com/muthuishere/declarativex-example/blob/main/assets/withthis2.png?raw=true)

----

![On Error](https://github.com/muthuishere/declarativex-example/blob/main/assets/onerror3.png?raw=true)

Maven

```

io.github.muthuishere
declarativex

```

Gradle
```
implementation "io.github.muthuishere:declarativex"
```

Usage
```
import declarativex.Try;
import declarativex.Filter;

```

## Try
Instead of Writing This

```
List results = null;

try {
results =newsService.downloadFromNyTimes(topic);
}catch (Exception e){

try {

results =newsService.downloadFromHerald(topic);
}catch (Exception e1){
try {
results =newsService.downloadFromSun(topic);
}catch (Exception e2){

}
}
}

```

Write This

```
import declarativex.Try;

results= Try.from(()->newsService.downloadFromNyTimes(topic))
.or(()->newsService.downloadFromHerald(topic))
.or(()->newsService.downloadFromSun(topic))
.get();

```

Or Even Better way
```
results= Try.any(newsService::downloadFromNyTimes,
newsService::downloadFromHerald,
newsService::downloadFromSun)
.with(topic)
.orElseGet(Arrays.asList("Some default"));

```

There are Additional Logging , Peek ,PeekError ,default value can convert to Optional as well

Also there is a lazy version of the same

```
results = Try.lazy.from(this::downloadCacheData)
.get();

//Evaluation happens only after get is Invoked

```