44
55import 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 )
88public 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}
0 commit comments