Skip to content

Commit 52e6507

Browse files
committed
fix
1 parent 2f4108d commit 52e6507

22 files changed

Lines changed: 640 additions & 464 deletions

‎generics/ArrayOfGeneric.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public static void main(String[] args) {
1717
gia = (Generic<Integer>[])new Generic[SIZE];
1818
System.out.println(gia.getClass().getSimpleName());
1919
gia[0] = new Generic<>();
20-
//- gia[1] = new Object(); // Compile-time error
20+
// gia[1] = new Object(); // Compile-time error
2121
// Discovers type mismatch at compile time:
22-
//- gia[2] = new Generic<Double>();
22+
// gia[2] = new Generic<Double>();
2323
}
2424
}
2525
/* Output:

‎generics/BasicBounds.java‎

Lines changed: 94 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,120 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55

6-
interface HasColor { java.awt.Color getColor(); }
6+
interface HasColor {
7+
java.awt.Color getColor();
8+
}
79

810
class WithColor<T extends HasColor> {
9-
T item;
10-
WithColor(T item) { this.item = item; }
11-
T getItem() { return item; }
12-
// The bound allows you to call a method:
13-
java.awt.Color color() { return item.getColor(); }
11+
T item;
12+
13+
WithColor(T item) {
14+
this.item = item;
15+
}
16+
17+
T getItem() {
18+
return item;
19+
}
20+
21+
// The bound allows you to call a method:
22+
java.awt.Color color() {
23+
return item.getColor();
24+
}
1425
}
1526

16-
class Coord { public int x, y, z; }
27+
class Coord {
28+
public int x, y, z;
29+
}
1730

1831
// This fails. Class must be first, then interfaces:
1932
// class WithColorCoord<T extends HasColor & Coord> {
2033

2134
// Multiple bounds:
2235
class WithColorCoord<T extends Coord & HasColor> {
23-
T item;
24-
WithColorCoord(T item) { this.item = item; }
25-
T getItem() { return item; }
26-
java.awt.Color color() { return item.getColor(); }
27-
int getX() { return item.x; }
28-
int getY() { return item.y; }
29-
int getZ() { return item.z; }
36+
T item;
37+
38+
WithColorCoord(T item) {
39+
this.item = item;
40+
}
41+
42+
T getItem() {
43+
return item;
44+
}
45+
46+
java.awt.Color color() {
47+
return item.getColor();
48+
}
49+
50+
int getX() {
51+
return item.x;
52+
}
53+
54+
int getY() {
55+
return item.y;
56+
}
57+
58+
int getZ() {
59+
return item.z;
60+
}
3061
}
3162

32-
interface Weight { int weight(); }
63+
interface Weight {
64+
int weight();
65+
}
3366

3467
// As with inheritance, you can have only one
3568
// concrete class but multiple interfaces:
3669
class Solid<T extends Coord & HasColor & Weight> {
37-
T item;
38-
Solid(T item) { this.item = item; }
39-
T getItem() { return item; }
40-
java.awt.Color color() { return item.getColor(); }
41-
int getX() { return item.x; }
42-
int getY() { return item.y; }
43-
int getZ() { return item.z; }
44-
int weight() { return item.weight(); }
70+
T item;
71+
72+
Solid(T item) {
73+
this.item = item;
74+
}
75+
76+
T getItem() {
77+
return item;
78+
}
79+
80+
java.awt.Color color() {
81+
return item.getColor();
82+
}
83+
84+
int getX() {
85+
return item.x;
86+
}
87+
88+
int getY() {
89+
return item.y;
90+
}
91+
92+
int getZ() {
93+
return item.z;
94+
}
95+
96+
int weight() {
97+
return item.weight();
98+
}
4599
}
46100

47101
class Bounded
48-
extends Coord implements HasColor, Weight {
49-
@Override
50-
public java.awt.Color getColor() { return null; }
51-
@Override public int weight() { return 0; }
102+
extends Coord implements HasColor, Weight {
103+
@Override
104+
public java.awt.Color getColor() {
105+
return null;
106+
}
107+
108+
@Override
109+
public int weight() {
110+
return 0;
111+
}
52112
}
53113

54114
public class BasicBounds {
55-
public static void main(String[] args) {
56-
Solid<Bounded> solid =
57-
new Solid<>(new Bounded());
58-
solid.color();
59-
solid.getY();
60-
solid.weight();
61-
}
115+
public static void main(String[] args) {
116+
Solid<Bounded> solid =
117+
new Solid<>(new Bounded());
118+
solid.color();
119+
solid.getY();
120+
solid.weight();
121+
}
62122
}

‎generics/ByteSet.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public class ByteSet {
99
Set<Byte> mySet =
1010
new HashSet<>(Arrays.asList(possibles));
1111
// But you can't do this:
12-
// Set<Byte> mySet2 = new HashSet<>(
13-
// Arrays.<Byte>asList(1,2,3,4,5,6,7,8,9));
12+
// Set<Byte> mySet2 = new HashSet<>(
13+
// Arrays.<Byte>asList(1,2,3,4,5,6,7,8,9));
1414
}

‎generics/CheckedList.java‎

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
//// generics/CheckedList.java
2-
//// (c)2021 MindView LLC: see Copyright.txt
3-
//// We make no guarantees that this code is fit for any purpose.
4-
//// Visit http://OnJava8.com for more book information.
5-
//// Using Collection.checkedList()
6-
//import reflection.pets.*;
7-
//import java.util.*;
8-
//
9-
//public class CheckedList {
10-
// @SuppressWarnings("unchecked")
11-
// static void oldStyleMethod(List probablyDogs) {
12-
// probablyDogs.add(new Cat());
13-
// }
14-
// public static void main(String[] args) {
15-
// List<Dog> dogs1 = new ArrayList<>();
16-
// oldStyleMethod(dogs1); // Quietly accepts a Cat
17-
// List<Dog> dogs2 = Collections.checkedList(
18-
// new ArrayList<>(), Dog.class);
19-
// try {
20-
// oldStyleMethod(dogs2); // Throws an exception
21-
// } catch(Exception e) {
22-
// System.out.println("Expected: " + e);
23-
// }
24-
// // Derived types work fine:
25-
// List<Pet> pets = Collections.checkedList(
26-
// new ArrayList<>(), Pet.class);
27-
// pets.add(new Dog());
1+
// generics/CheckedList.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// Using Collection.checkedList()
6+
import reflection.pets.*;
7+
import java.util.*;
8+
9+
public class CheckedList {
10+
@SuppressWarnings("unchecked")
11+
static void oldStyleMethod(List probablyDogs) {
12+
probablyDogs.add(new Cat());
13+
}
14+
public static void main(String[] args) {
15+
List<Dog> dogs1 = new ArrayList<>();
16+
oldStyleMethod(dogs1); // Quietly accepts a Cat
17+
List<Dog> dogs2 = Collections.checkedList(
18+
new ArrayList<>(), Dog.class);
19+
try {
20+
oldStyleMethod(dogs2); // Throws an exception
21+
} catch(Exception e) {
22+
System.out.println("Expected: " + e);
23+
}
24+
// Derived types work fine:
25+
List<Pet> pets = Collections.checkedList(
26+
new ArrayList<>(), Pet.class);
27+
pets.add(new Dog());
2828
// pets.add(new Cat());
29-
// }
30-
//}
31-
///* Output:
32-
//Expected: java.lang.ClassCastException: Attempt to
33-
//insert class reflection.pets.Cat element into collection
34-
//with element type class reflection.pets.Dog
35-
//*/
29+
}
30+
}
31+
/* Output:
32+
Expected: java.lang.ClassCastException: Attempt to
33+
insert class reflection.pets.Cat element into collection
34+
with element type class reflection.pets.Dog
35+
*/

‎generics/EpicBattle.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ public static void main(String[] args) {
6767
// You can do this:
6868
List<? extends SuperHearing> audioPeople;
6969
// But you can't do this:
70-
// List<? extends SuperHearing & SuperSmell> dogPs;
70+
// List<? extends SuperHearing & SuperSmell> dogPs;
7171
}
7272
}

‎generics/GenericArray.java‎

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,36 @@
44
// Visit http://OnJava8.com for more book information.
55

66
public class GenericArray<T> {
7-
private T[] array;
8-
@SuppressWarnings("unchecked")
9-
public GenericArray(int sz) {
10-
array = (T[])new Object[sz];
11-
}
12-
public void put(int index, T item) {
13-
array[index] = item;
14-
}
15-
public T get(int index) { return array[index]; }
16-
// Method that exposes the underlying representation:
17-
public T[] rep() { return array; }
18-
public static void main(String[] args) {
19-
GenericArray<Integer> gai = new GenericArray<>(10);
20-
try {
21-
Integer[] ia = gai.rep();
22-
} catch(ClassCastException e) {
23-
System.out.println(e.getMessage());
7+
private T[] array;
8+
9+
// @SuppressWarnings("unchecked")
10+
public GenericArray(int sz) {
11+
array = (T[]) new Object[sz];
12+
}
13+
14+
public void put(int index, T item) {
15+
array[index] = item;
16+
}
17+
18+
public T get(int index) {
19+
return array[index];
20+
}
21+
22+
// Method that exposes the underlying representation:
23+
public T[] rep() {
24+
return array;
25+
}
26+
27+
public static void main(String[] args) {
28+
GenericArray<Integer> gai = new GenericArray<>(10);
29+
try {
30+
Integer[] ia = gai.rep();
31+
} catch (ClassCastException e) {
32+
System.out.println(e.getMessage());
33+
}
34+
// This is OK:
35+
Object[] oa = gai.rep();
2436
}
25-
// This is OK:
26-
Object[] oa = gai.rep();
27-
}
2837
}
2938
/* Output:
3039
[Ljava.lang.Object; cannot be cast to

‎generics/GenericCast.java‎

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,50 @@
22
// (c)2021 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
5+
56
import java.util.*;
67
import java.util.stream.*;
78

89
class FixedSizeStack<T> {
9-
private final int size;
10-
private Object[] storage;
11-
private int index = 0;
12-
FixedSizeStack(int size) {
13-
this.size = size;
14-
storage = new Object[size];
15-
}
16-
public void push(T item) {
17-
if(index < size)
18-
storage[index++] = item;
19-
}
20-
@SuppressWarnings("unchecked")
21-
public T pop() {
22-
return index == 0 ? null : (T)storage[--index];
23-
}
24-
@SuppressWarnings("unchecked")
25-
Stream<T> stream() {
26-
return (Stream<T>)Arrays.stream(storage);
27-
}
10+
private final int size;
11+
private Object[] storage;
12+
private int index = 0;
13+
14+
FixedSizeStack(int size) {
15+
this.size = size;
16+
storage = new Object[size];
17+
}
18+
19+
public void push(T item) {
20+
if (index < size)
21+
storage[index++] = item;
22+
}
23+
24+
@SuppressWarnings("unchecked")
25+
public T pop() {
26+
return index == 0 ? null : (T) storage[--index];
27+
}
28+
29+
@SuppressWarnings("unchecked")
30+
Stream<T> stream() {
31+
return (Stream<T>) Arrays.stream(storage);
32+
}
2833
}
2934

3035
public class GenericCast {
31-
static String[] letters =
32-
"ABCDEFGHIJKLMNOPQRS".split("");
33-
public static void main(String[] args) {
34-
FixedSizeStack<String> strings =
35-
new FixedSizeStack<>(letters.length);
36-
Arrays.stream("ABCDEFGHIJKLMNOPQRS".split(""))
37-
.forEach(strings::push);
38-
System.out.println(strings.pop());
39-
strings.stream()
40-
.map(s -> s + " ")
41-
.forEach(System.out::print);
42-
}
36+
static String[] letters =
37+
"ABCDEFGHIJKLMNOPQRS".split("");
38+
39+
public static void main(String[] args) {
40+
FixedSizeStack<String> strings =
41+
new FixedSizeStack<>(letters.length);
42+
Arrays.stream("ABCDEFGHIJKLMNOPQRS".split(""))
43+
.forEach(strings::push);
44+
System.out.println(strings.pop());
45+
strings.stream()
46+
.map(s -> s + " ")
47+
.forEach(System.out::print);
48+
}
4349
}
4450
/* Output:
4551
S

0 commit comments

Comments
 (0)