回答編集履歴

5

コード修正

2022/02/23 06:37

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -66,12 +66,12 @@
66
66
  interface Retry {
67
67
  void onRetry(Exception cause);
68
68
  }
69
- T executeWithRetry(Retry l) {
69
+ T executeWithRetry(Retry retry) {
70
70
  while(true) {
71
71
  try {
72
72
  return exec();
73
73
  } catch(Func.Exception e) {
74
- if(l != null) l.onRetry(e);
74
+ if(retry != null) retry.onRetry(e);
75
75
  }
76
76
  }
77
77
  }

4

コード追加

2022/02/23 06:31

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -47,3 +47,34 @@
47
47
  api2 の処理 (arg1=100,arg2='ABC')
48
48
  a1=-123, a2=XYZ
49
49
  ```
50
+
51
+ リトライ自体を Func に入れてしまって main は例外時の処理を渡す形にも出来ます。
52
+ ```java
53
+ public class Main {
54
+ public static void main(String[] args) {
55
+ Func.Retry retry = (e) -> {
56
+ //input("enter")
57
+ };
58
+ int a1 = new Api1().executeWithRetry(retry);
59
+ String a2 = new Api2(100, "ABC").executeWithRetry(retry);
60
+ System.out.println("a1="+a1+", a2="+a2);
61
+ }
62
+ }
63
+
64
+ abstract class Func<T> {
65
+ static class Exception extends Throwable {}
66
+ interface Retry {
67
+ void onRetry(Exception cause);
68
+ }
69
+ T executeWithRetry(Retry l) {
70
+ while(true) {
71
+ try {
72
+ return exec();
73
+ } catch(Func.Exception e) {
74
+ if(l != null) l.onRetry(e);
75
+ }
76
+ }
77
+ }
78
+ abstract T exec() throws Func.Exception;
79
+ }
80
+ ```

3

package 削除

2022/02/23 04:53

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -1,7 +1,5 @@
1
1
  垢抜けない感じですが^^;
2
2
  ```java
3
- package teratail_java.q_9hvb8g6b8hkrnd;
4
-
5
3
  public class Main {
6
4
  public static void main(String[] args) {
7
5
  int a1 = execute(new Api1());

2

戻り値を忘れてました

2022/02/23 04:51

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -1,16 +1,18 @@
1
1
  垢抜けない感じですが^^;
2
2
  ```java
3
+ package teratail_java.q_9hvb8g6b8hkrnd;
4
+
3
5
  public class Main {
4
6
  public static void main(String[] args) {
5
- execute(new Api1());
7
+ int a1 = execute(new Api1());
6
- execute(new Api2(100, "ABC"));
8
+ String a2 = execute(new Api2(100, "ABC"));
9
+ System.out.println("a1="+a1+", a2="+a2);
7
10
  }
8
11
 
9
- static void execute(Func func) {
12
+ static <T> T execute(Func<T> func) {
10
13
  while(true) {
11
14
  try {
12
- func.exec();
15
+ return func.exec();
13
- return;
14
16
  } catch(Func.Exception e) {
15
17
  //input("enter")
16
18
  }
@@ -18,29 +20,32 @@
18
20
  }
19
21
  }
20
22
 
21
- abstract class Func {
23
+ abstract class Func<T> {
22
24
  static class Exception extends Throwable {}
23
- abstract void exec() throws Func.Exception;
25
+ abstract T exec() throws Func.Exception;
24
26
  }
25
27
 
26
- class Api1 extends Func {
28
+ class Api1 extends Func<Integer> {
27
- public void exec() throws Func.Exception {
29
+ public Integer exec() throws Func.Exception {
28
30
  System.out.println("api1 の処理");
31
+ return -123;
29
32
  }
30
33
  }
31
- class Api2 extends Func {
34
+ class Api2 extends Func<String> {
32
35
  final int arg1;
33
36
  final String arg2;
34
37
  Api2(int arg1, String arg2) {
35
38
  this.arg1 = arg1;
36
39
  this.arg2 = arg2;
37
40
  }
38
- public void exec() throws Func.Exception {
41
+ public String exec() throws Func.Exception {
39
42
  System.out.println("api2 の処理 (arg1="+arg1+",arg2='"+arg2+"')");
43
+ return "XYZ";
40
44
  }
41
45
  }
42
46
  ```
43
47
  ```plain
44
48
  api1 の処理
45
49
  api2 の処理 (arg1=100,arg2='ABC')
50
+ a1=-123, a2=XYZ
46
51
  ```

1

結果追加

2022/02/23 04:45

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -40,3 +40,7 @@
40
40
  }
41
41
  }
42
42
  ```
43
+ ```plain
44
+ api1 の処理
45
+ api2 の処理 (arg1=100,arg2='ABC')
46
+ ```