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
- Host: GitHub
- URL: https://github.com/muthuishere/declarativex
- Owner: muthuishere
- License: apache-2.0
- Created: 2021-08-11T12:37:08.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-12T05:29:39.000Z (over 3 years ago)
- Last Synced: 2024-07-30T18:00:36.208Z (9 months ago)
- Topics: declarative-programming, exception-handling, java
- Language: Java
- Homepage:
- Size: 931 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: ReadMe.md
- License: LICENSE
Awesome Lists containing this project
README
### DeclarativeX
###### A Composable approach to exception & conditions in Java

----

----

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
```