File tree 2 files changed +50
-0
lines changed
src/main/java/com/lambdista/util
2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ apply plugin : ' java'
2
+
3
+ repositories {
4
+ mavenCentral()
5
+ }
6
+
7
+ sourceCompatibility = " 1.8"
8
+ targetCompatibility = " 1.8"
9
+
10
+ dependencies {
11
+ compile group : ' commons-collections' , name : ' commons-collections' , version : ' 3.2'
12
+ testCompile ' org.hamcrest:hamcrest-all:1.3'
13
+ testCompile ' org.mockito:mockito-all:1.9.5'
14
+ testCompile ' junit:junit:4.12'
15
+
16
+
17
+ }
Original file line number Diff line number Diff line change
1
+ package com .lambdista .util ;
2
+
3
+ import java .util .function .Consumer ;
4
+
5
+ /**
6
+ * Created by armin on 06/08/15.
7
+ */
8
+ public class Retry {
9
+
10
+ static int someCounter = 5 ;
11
+ public static void main (String [] args ) {
12
+ String x = "trying..." ;
13
+ FailableSupplier <Integer > integerFailableSupplier = () -> {
14
+ System .out .println (x + someCounter --);
15
+ if (someCounter > 3 ) {
16
+ throw new RuntimeException ();
17
+ }
18
+ return someCounter ;
19
+ };
20
+ System .out .println (retry (5 , 0 , integerFailableSupplier ));
21
+ }
22
+
23
+ private static <T > T retry (int maxRetries , int retryCount , FailableSupplier <T > supplier ) {
24
+
25
+ Try <T > aTry = Try .apply (supplier );
26
+ if (aTry .isFailure () && maxRetries > retryCount ) {
27
+ return retry (maxRetries , retryCount ++, supplier );
28
+ } else {
29
+ return aTry .get ();
30
+ }
31
+ }
32
+
33
+ }
You can’t perform that action at this time.
0 commit comments