@@ -32,6 +32,8 @@ Java scope functions inspired by Kotlin
3232 * [ Collection initialization] ( #collection-initialization )
3333 * [ Argument in a method chain] ( #argument-in-a-method-chain )
3434 * [ Nth Fibonacci number] ( #nth-fibonacci-number )
35+ * [ Get all related exceptions via recursion] ( #get-all-related-exceptions-via-recursion )
36+ * [ Get all related exceptions via iteration] ( #get-all-related-exceptions-via-iteration )
3537
3638## Motivation
3739
@@ -44,11 +46,10 @@ Java 8+ version required. The library has no dependencies.
4446Maven:
4547
4648``` xml
47-
4849<dependency >
4950 <groupId >com.plugatar.jkscope</groupId >
5051 <artifactId >jkscope</artifactId >
51- <version >3.0 </version >
52+ <version >3.1 </version >
5253 <scope >compile</scope >
5354</dependency >
5455```
@@ -57,7 +58,7 @@ Gradle:
5758
5859``` groovy
5960dependencies {
60- implementation 'com.plugatar.jkscope:jkscope:3.0 '
61+ implementation 'com.plugatar.jkscope:jkscope:3.1 '
6162}
6263```
6364
@@ -439,7 +440,7 @@ All presented functions allow you to not catch any checked exceptions.
439440
440441```
441442public static void main(String[] args) {
442- URI uri = let (() -> new URI("abc"));
443+ URI uri = it (() -> new URI("abc"));
443444}
444445```
445446
@@ -476,11 +477,39 @@ new MyBuilder()
476477#### Nth Fibonacci number
477478
478479```
479- int value = recur1(10, (n, func) -> {
480+ int result = recur1(10, (n, func) -> {
480481 if (n <= 1) {
481482 return 1;
482483 } else {
483484 return n * func.apply(n - 1);
484485 }
485486});
486487```
488+
489+ #### Get all related exceptions via recursion
490+
491+ ```
492+ Throwable mainException = ...;
493+ Set<Throwable> allRelated = recur1(mainException, new HashSet<>(), (currentEx, set, self) -> {
494+ if (currentEx != null && set.add(currentEx)) {
495+ self.accept(currentEx.getCause());
496+ for (final Throwable suppressedEx : currentEx.getSuppressed()) {
497+ self.accept(suppressedEx);
498+ }
499+ }
500+ });
501+ ```
502+
503+ #### Get all related exceptions via iteration
504+
505+ ```
506+ Throwable mainException = ...;
507+ Set<Throwable> allRelated = iterate1(mainException, new HashSet<>(), (currentEx, set, nextValues) -> {
508+ if (currentEx != null && set.add(currentEx)) {
509+ nextValues.push(currentEx.getCause());
510+ for (final Throwable suppressedEx : currentEx.getSuppressed()) {
511+ nextValues.push(suppressedEx);
512+ }
513+ }
514+ });
515+ ```
0 commit comments