FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/generics/FactoryConstraint.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
/
FactoryConstraint.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (57 loc) · 1.39 KB
Breadcrumbs
OnJava8-Examples
/
generics
/
FactoryConstraint.java
Copy path
File metadata and controls
62 lines (57 loc) · 1.39 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
// generics/FactoryConstraint.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
.*;
import
java
.
util
.
function
.*;
import
onjava
.*;
class
IntegerFactory
implements
Supplier
<
Integer
> {
private
int
i
=
0
;
@
Override
public
Integer
get
() {
return
++
i
;
}
}
class
Widget
{
private
int
id
;
Widget
(
int
n
) {
id
=
n
; }
@
Override
public
String
toString
() {
return
"Widget "
+
id
;
}
public
static
class
Factory
implements
Supplier
<
Widget
> {
private
int
i
=
0
;
@
Override
public
Widget
get
() {
return
new
Widget
(++
i
); }
}
}
class
Fudge
{
private
static
int
count
=
1
;
private
int
n
=
count
++;
@
Override
public
String
toString
() {
return
"Fudge "
+
n
;
}
}
class
Foo2
<
T
> {
private
List
<
T
>
x
=
new
ArrayList
<>();
Foo2
(
Supplier
<
T
>
factory
) {
Suppliers
.
fill
(
x
,
factory
,
5
);
}
@
Override
public
String
toString
() {
return
x
.
toString
();
}
}
public
class
FactoryConstraint
{
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
new
Foo2
<>(
new
IntegerFactory
()));
System
.
out
.
println
(
new
Foo2
<>(
new
Widget
.
Factory
()));
System
.
out
.
println
(
new
Foo2
<>(
Fudge
::
new
));
}
}
/* Output:
[1, 2, 3, 4, 5]
[Widget 1, Widget 2, Widget 3, Widget 4, Widget 5]
[Fudge 1, Fudge 2, Fudge 3, Fudge 4, Fudge 5]
*/
Back
|
FazBrowse Home
|
New Git URL