@@ -32,6 +32,8 @@ Java scope functions inspired by Kotlin
32
32
* [ Collection initialization] ( #collection-initialization )
33
33
* [ Argument in a method chain] ( #argument-in-a-method-chain )
34
34
* [ 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 )
35
37
36
38
## Motivation
37
39
@@ -44,11 +46,10 @@ Java 8+ version required. The library has no dependencies.
44
46
Maven:
45
47
46
48
``` xml
47
-
48
49
<dependency >
49
50
<groupId >com.plugatar.jkscope</groupId >
50
51
<artifactId >jkscope</artifactId >
51
- <version >3.0 </version >
52
+ <version >3.1 </version >
52
53
<scope >compile</scope >
53
54
</dependency >
54
55
```
@@ -57,7 +58,7 @@ Gradle:
57
58
58
59
``` groovy
59
60
dependencies {
60
- implementation 'com.plugatar.jkscope:jkscope:3.0 '
61
+ implementation 'com.plugatar.jkscope:jkscope:3.1 '
61
62
}
62
63
```
63
64
@@ -439,7 +440,7 @@ All presented functions allow you to not catch any checked exceptions.
439
440
440
441
```
441
442
public static void main(String[] args) {
442
- URI uri = let (() -> new URI("abc"));
443
+ URI uri = it (() -> new URI("abc"));
443
444
}
444
445
```
445
446
@@ -476,11 +477,39 @@ new MyBuilder()
476
477
#### Nth Fibonacci number
477
478
478
479
```
479
- int value = recur1(10, (n, func) -> {
480
+ int result = recur1(10, (n, func) -> {
480
481
if (n <= 1) {
481
482
return 1;
482
483
} else {
483
484
return n * func.apply(n - 1);
484
485
}
485
486
});
486
487
```
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