Skip to content

Commit d2cf57e

Browse files
committed
Cleaned up examples for chapter 6 (Enums and Annotations)
1 parent 1385436 commit d2cf57e

File tree

29 files changed

+250
-95
lines changed

29 files changed

+250
-95
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package effectivejava.chapter6.item34;
2+
3+
// Switch on an enum to simulate a missing method (Page 167)
4+
public class Inverse {
5+
public static Operation inverse(Operation op) {
6+
switch(op) {
7+
case PLUS: return Operation.MINUS;
8+
case MINUS: return Operation.PLUS;
9+
case TIMES: return Operation.DIVIDE;
10+
case DIVIDE: return Operation.TIMES;
11+
12+
default: throw new AssertionError("Unknown op: " + op);
13+
}
14+
}
15+
16+
public static void main(String[] args) {
17+
double x = Double.parseDouble(args[0]);
18+
double y = Double.parseDouble(args[1]);
19+
for (Operation op : Operation.values()) {
20+
Operation invOp = inverse(op);
21+
System.out.printf("%f %s %f %s %f = %f%n",
22+
x, op, y, invOp, y, invOp.apply(op.apply(x, y), y));
23+
}
24+
}
25+
}

src/effectivejava/chapter6/item34/Operation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import static java.util.stream.Collectors.toMap;
66

7-
// Enum type with constant-specific class bodies and data (Page 161)
7+
// Enum type with constant-specific class bodies and data (Page 163-4)
88
public enum Operation {
99
PLUS("+") {
1010
public double apply(double x, double y) { return x + y; }
@@ -27,7 +27,7 @@ public enum Operation {
2727

2828
public abstract double apply(double x, double y);
2929

30-
// Implementing a fromString method on an enum type
30+
// Implementing a fromString method on an enum type (Page 164)
3131
private static final Map<String, Operation> stringToEnum =
3232
Stream.of(values()).collect(
3333
toMap(Object::toString, e -> e));

src/effectivejava/chapter6/item34/PayrollDay.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package effectivejava.chapter6.item34;
22

3-
// The strategy enum pattern
3+
// The strategy enum pattern (Page 166)
44
enum PayrollDay {
55
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
66
SATURDAY(PayType.WEEKEND), SUNDAY(PayType.WEEKEND);

src/effectivejava/chapter6/item34/Planet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package effectivejava.chapter6.item34;
22

3-
// Enum type with data and behavior (157-158)
3+
// Enum type with data and behavior (159-160)
44
public enum Planet {
55
MERCURY(3.302e+23, 2.439e6),
66
VENUS (4.869e+24, 6.052e6),
@@ -14,6 +14,7 @@ public enum Planet {
1414
private final double mass; // In kilograms
1515
private final double radius; // In meters
1616
private final double surfaceGravity; // In m / s^2
17+
1718
// Universal gravitational constant in m^3 / kg s^2
1819
private static final double G = 6.67300E-11;
1920

src/effectivejava/chapter6/item34/WeightTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package effectivejava.chapter6.item34;
22

3-
// Takes earth-weight and prints table of weights on all planets - Page 158
3+
// Takes earth-weight and prints table of weights on all planets (Page 160)
44
public class WeightTable {
55
public static void main(String[] args) {
66
double earthWeight = Double.parseDouble(args[0]);

src/effectivejava/chapter6/item35/Ensemble.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package effectivejava.chapter6.item35;
22

3-
// Enum with integer data stored in an instance field (Page 166)
3+
// Enum with integer data stored in an instance field (Page 168)
44
public enum Ensemble {
55
SOLO(1), DUET(2), TRIO(3), QUARTET(4), QUINTET(5),
66
SEXTET(6), SEPTET(7), OCTET(8), DOUBLE_QUARTET(8),
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package effectivejava.chapter6.item36;
2+
23
import java.util.*;
34

4-
// EnumSet - a modern replacement for bit fields (Page 168)
5+
// EnumSet - a modern replacement for bit fields (Page 170)
56
public class Text {
6-
public enum Style { BOLD, ITALIC, UNDERLINE, STRIKETHROUGH }
7+
public enum Style {BOLD, ITALIC, UNDERLINE, STRIKETHROUGH}
78

89
// Any Set could be passed in, but EnumSet is clearly best
9-
public void applyStyles(Set<Style> styles) { }
10+
public void applyStyles(Set<Style> styles) {
11+
System.out.printf("Applying styles %s to text%n",
12+
Objects.requireNonNull(styles));
13+
}
1014

1115
// Sample use
1216
public static void main(String[] args) {
1317
Text text = new Text();
1418
text.applyStyles(EnumSet.of(Style.BOLD, Style.ITALIC));
1519
}
16-
}
20+
}

src/effectivejava/chapter6/item37/Phase.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
import static java.util.stream.Collectors.*;
66

7-
// Using a nested EnumMap to associate data with enum pairs - (Pages 172-3)
7+
// Using a nested EnumMap to associate data with enum pairs - (Pages 174-5)
88
public enum Phase {
99
SOLID, LIQUID, GAS;
1010
public enum Transition {
1111
MELT(SOLID, LIQUID), FREEZE(LIQUID, SOLID),
1212
BOIL(LIQUID, GAS), CONDENSE(GAS, LIQUID),
1313
SUBLIME(SOLID, GAS), DEPOSIT(GAS, SOLID);
1414

15-
// Adding a new phase (Page 173)
15+
// // Adding a new phase (Page 175)
1616
// SOLID, LIQUID, GAS, PLASMA;
1717
// public enum Transition {
1818
// MELT(SOLID, LIQUID), FREEZE(LIQUID, SOLID),
@@ -33,17 +33,20 @@ public enum Transition {
3333
() -> new EnumMap<>(Phase.class),
3434
toMap(t -> t.to, t -> t,
3535
(x, y) -> y, () -> new EnumMap<>(Phase.class))));
36+
3637
public static Transition from(Phase from, Phase to) {
3738
return m.get(from).get(to);
3839
}
3940
}
4041

4142
// Simple demo program - prints a sloppy table
4243
public static void main(String[] args) {
43-
for (Phase src : Phase.values())
44-
for (Phase dst : Phase.values())
45-
if (src != dst)
46-
System.out.printf("%s to %s : %s %n", src, dst,
47-
Transition.from(src, dst));
44+
for (Phase src : Phase.values()) {
45+
for (Phase dst : Phase.values()) {
46+
Transition transition = Transition.from(src, dst);
47+
if (transition != null)
48+
System.out.printf("%s to %s : %s %n", src, dst, transition);
49+
}
50+
}
4851
}
4952
}

src/effectivejava/chapter6/item37/Plant.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import static java.util.stream.Collectors.groupingBy;
44
import static java.util.stream.Collectors.toSet;
55

6-
// Using an EnumMap to associate data with an enum - Pages 169-171
6+
// Using an EnumMap to associate data with an enum (Pages 171-3)
77

8-
// Simplistic class representing a plant - Page 169
8+
// Simplistic class representing a plant (Page 171)
99
class Plant {
1010
enum LifeCycle { ANNUAL, PERENNIAL, BIENNIAL }
1111

@@ -31,7 +31,7 @@ public static void main(String[] args) {
3131
new Plant("Rosemary", LifeCycle.PERENNIAL)
3232
};
3333

34-
// Using ordinal() to index into an array - DON'T DO THIS! (Page 170)
34+
// Using ordinal() to index into an array - DON'T DO THIS! (Page 171)
3535
Set<Plant>[] plantsByLifeCycleArr =
3636
(Set<Plant>[]) new Set[Plant.LifeCycle.values().length];
3737
for (int i = 0; i < plantsByLifeCycleArr.length; i++)
@@ -44,7 +44,7 @@ public static void main(String[] args) {
4444
Plant.LifeCycle.values()[i], plantsByLifeCycleArr[i]);
4545
}
4646

47-
// Using an EnumMap to associate data with an enum (P. 170)
47+
// Using an EnumMap to associate data with an enum (Page 172)
4848
Map<Plant.LifeCycle, Set<Plant>> plantsByLifeCycle =
4949
new EnumMap<>(Plant.LifeCycle.class);
5050
for (Plant.LifeCycle lc : Plant.LifeCycle.values())
@@ -53,11 +53,11 @@ public static void main(String[] args) {
5353
plantsByLifeCycle.get(p.lifeCycle).add(p);
5454
System.out.println(plantsByLifeCycle);
5555

56-
// Naive stream-based approach - unlikely to produce an EnumMap! (Page 171)
56+
// Naive stream-based approach - unlikely to produce an EnumMap! (Page 172)
5757
System.out.println(Arrays.stream(garden)
5858
.collect(groupingBy(p -> p.lifeCycle)));
5959

60-
// Using a stream and an EnumMap to associate data with an enum (Page 171)
60+
// Using a stream and an EnumMap to associate data with an enum (Page 173)
6161
System.out.println(Arrays.stream(garden)
6262
.collect(groupingBy(p -> p.lifeCycle,
6363
() -> new EnumMap<>(LifeCycle.class), toSet())));

src/effectivejava/chapter6/item38/BasicOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package effectivejava.chapter6.item38;
22

3-
// Emulated extensible enum using an interface - Basic implementation - Page 174
3+
// Emulated extensible enum using an interface - Basic implementation (Page 176)
44
public enum BasicOperation implements Operation {
55
PLUS("+") {
66
public double apply(double x, double y) { return x + y; }

0 commit comments

Comments
 (0)