Skip to content

Commit 745394e

Browse files
committed
Cleaned up code examples from Chapter 8 (Methods)
1 parent c641e58 commit 745394e

File tree

13 files changed

+146
-30
lines changed

13 files changed

+146
-30
lines changed

src/effectivejava/chapter8/item50/Attack.java renamed to src/effectivejava/chapter8/item50/Attacks.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package effectivejava.chapter8.item50;
22
import java.util.*;
33

4-
// Two attacks on the internals of an "immutable" period
5-
public class Attack {
4+
// Two attacks on the internals of an "immutable" period (232-3)
5+
public class Attacks {
66
public static void main(String[] args) {
7-
// Attack the internals of a Period instance (page 232)
7+
// Attack the internals of a Period instance (Page 232)
88
Date start = new Date();
99
Date end = new Date();
1010
Period p = new Period(start, end);
1111
end.setYear(78); // Modifies internals of p!
1212
System.out.println(p);
1313

14-
// Second attack on the internals of a Period instance
14+
// Second attack on the internals of a Period instance (Page 233)
1515
start = new Date();
1616
end = new Date();
1717
p = new Period(start, end);

src/effectivejava/chapter8/item50/Period.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package effectivejava.chapter8.item50;
22
import java.util.*;
33

4-
// Broken "immutable" time period class - Page 231-3
4+
// Broken "immutable" time period class (Pages 231-3)
55
public final class Period {
66
private final Date start;
77
private final Date end;
@@ -31,7 +31,7 @@ public String toString() {
3131
return start + " - " + end;
3232
}
3333

34-
// // Repaired constructor - makes defensive copies of parameters
34+
// // Repaired constructor - makes defensive copies of parameters (Page 232)
3535
// public Period(Date start, Date end) {
3636
// this.start = new Date(start.getTime());
3737
// this.end = new Date(end.getTime());
@@ -40,7 +40,7 @@ public String toString() {
4040
// throw new IllegalArgumentException(
4141
// this.start + " after " + this.end);
4242
// }
43-
43+
//
4444
// // Repaired accessors - make defensive copies of internal fields (Page 233)
4545
// public Date start() {
4646
// return new Date(start.getTime());
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package effectivejava.chapter8.item52;
22

3+
// Classification using method overrriding (Page 239)
34
class Champagne extends SparklingWine {
45
@Override String name() { return "champagne"; }
56
}

src/effectivejava/chapter8/item52/CollectionClassifier.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,4 @@ public static void main(String[] args) {
2626
for (Collection<?> c : collections)
2727
System.out.println(classify(c));
2828
}
29-
// Repaired static classifier method. (Page 240)
30-
// public static String classify(Collection<?> c) {
31-
// return c instanceof Set ? "Set" :
32-
// c instanceof List ? "List" : "Unknown Collection";
33-
// }
3429
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package effectivejava.chapter8.item52;
2+
3+
import java.math.BigInteger;
4+
import java.util.*;
5+
6+
// Repaired static classifier method. (Page 240)
7+
public class FixedCollectionClassifier {
8+
public static String classify(Collection<?> c) {
9+
return c instanceof Set ? "Set" :
10+
c instanceof List ? "List" : "Unknown Collection";
11+
}
12+
13+
public static void main(String[] args) {
14+
Collection<?>[] collections = {
15+
new HashSet<String>(),
16+
new ArrayList<BigInteger>(),
17+
new HashMap<String, String>().values()
18+
};
19+
20+
for (Collection<?> c : collections)
21+
System.out.println(classify(c));
22+
}
23+
}

src/effectivejava/chapter8/item52/Overriding.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44

5+
// Classification using method overrriding (Page 239)
56
public class Overriding {
67
public static void main(String[] args) {
78
List<Wine> wineList = List.of(

src/effectivejava/chapter8/item52/SetList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package effectivejava.chapter8.item52;
22
import java.util.*;
33

4-
// What does this program print? - Page 241
4+
// What does this program print? (Page 241)
55
public class SetList {
66
public static void main(String[] args) {
77
Set<Integer> set = new TreeSet<>();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package effectivejava.chapter8.item52;
22

3+
// Classification using method overrriding (Page 239)
34
class SparklingWine extends Wine {
45
@Override String name() { return "sparkling wine"; }
56
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package effectivejava.chapter8.item52;
22

3+
// Classification using method overrriding (Page 239)
34
class Wine {
45
String name() { return "wine"; }
56
}

src/effectivejava/chapter8/item53/Varargs.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22

33
import java.util.stream.IntStream;
44

5-
// Sample uses of varargs
5+
// Sample uses of varargs (Pages 245-6)
66
public class Varargs {
7-
8-
// Simple use of varargs - Page 245
7+
// Simple use of varargs (Page 245)
98
static int sum(int... args) {
109
int sum = 0;
1110
for (int arg : args)
1211
sum += arg;
1312
return sum;
1413
}
1514

16-
// Simple use of varargs - Page 197
17-
static int sum2(int... args) {
18-
return IntStream.of(args).sum();
19-
}
20-
21-
// // The WRONG way to use varargs to pass one or more arguments! - Page 245
15+
// // The WRONG way to use varargs to pass one or more arguments! (Page 245)
2216
// static int min(int... args) {
2317
// if (args.length == 0)
2418
// throw new IllegalArgumentException("Too few arguments");
@@ -29,7 +23,7 @@ static int sum2(int... args) {
2923
// return min;
3024
// }
3125

32-
// The right way to use varargs to pass one or more arguments - Page 246
26+
// The right way to use varargs to pass one or more arguments (Page 246)
3327
static int min(int firstArg, int... remainingArgs) {
3428
int min = firstArg;
3529
for (int arg : remainingArgs)
@@ -38,7 +32,6 @@ static int min(int firstArg, int... remainingArgs) {
3832
return min;
3933
}
4034

41-
4235
public static void main(String[] args) {
4336
System.out.println(sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
4437
System.out.println(min(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

0 commit comments

Comments
 (0)