FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/generics/ThrowGenericException.java at master · CSapprenticeWT/OnJava8-Examples · GitHub
CSapprenticeWT
OnJava8-Examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
generics
/
ThrowGenericException.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
82 lines (74 loc) · 1.92 KB
Breadcrumbs
OnJava8-Examples
/
generics
/
ThrowGenericException.java
Copy path
File metadata and controls
82 lines (74 loc) · 1.92 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// generics/ThrowGenericException.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import
java
.
util
.*;
interface
Processor
<
T
,
E
extends
Exception
> {
void
process
(
List
<
T
>
resultCollector
)
throws
E
;
}
class
ProcessRunner
<
T
,
E
extends
Exception
>
extends
ArrayList
<
Processor
<
T
,
E
>> {
List
<
T
>
processAll
()
throws
E
{
List
<
T
>
resultCollector
=
new
ArrayList
<>();
for
(
Processor
<
T
,
E
>
processor
:
this
)
processor
.
process
(
resultCollector
);
return
resultCollector
;
}
}
class
Failure1
extends
Exception
{}
class
Processor1
implements
Processor
<
String
,
Failure1
> {
static
int
count
=
3
;
@
Override
public
void
process
(
List
<
String
>
resultCollector
)
throws
Failure1
{
if
(
count
-- >
1
)
resultCollector
.
add
(
"Hep!"
);
else
resultCollector
.
add
(
"Ho!"
);
if
(
count
<
0
)
throw
new
Failure1
();
}
}
class
Failure2
extends
Exception
{}
class
Processor2
implements
Processor
<
Integer
,
Failure2
> {
static
int
count
=
2
;
@
Override
public
void
process
(
List
<
Integer
>
resultCollector
)
throws
Failure2
{
if
(
count
-- ==
0
)
resultCollector
.
add
(
47
);
else
{
resultCollector
.
add
(
11
);
}
if
(
count
<
0
)
throw
new
Failure2
();
}
}
public
class
ThrowGenericException
{
public
static
void
main
(
String
[]
args
) {
ProcessRunner
<
String
,
Failure1
>
runner
=
new
ProcessRunner
<>();
for
(
int
i
=
0
;
i
<
3
;
i
++)
runner
.
add
(
new
Processor1
());
try
{
System
.
out
.
println
(
runner
.
processAll
());
}
catch
(
Failure1
e
) {
System
.
out
.
println
(
e
);
}
ProcessRunner
<
Integer
,
Failure2
>
runner2
=
new
ProcessRunner
<>();
for
(
int
i
=
0
;
i
<
3
;
i
++)
runner2
.
add
(
new
Processor2
());
try
{
System
.
out
.
println
(
runner2
.
processAll
());
}
catch
(
Failure2
e
) {
System
.
out
.
println
(
e
);
}
}
}
/* Output:
[Hep!, Hep!, Ho!]
Failure2
*/
Back
|
FazBrowse Home
|
New Git URL