Home > database >  Use EnumMap instead of ordinal indexing
Use EnumMap instead of ordinal indexing

Time:11-01

I am new to Java, so I am reading "Effective Java", 3rd edition from Joshua Bloch, written in German.

This is a piece of code, written in this 3rd edition, Theme 37, page 180:

public enum Phase {
   SOLID, LIQUID, GAS;

   public enum Transition {
      MELT(SOLID, LIQUID), FREEZE(LIQUID, SOLID),
      BOIL(LIQUID, GAS), CONDENSE(GAS, LIQUID),
      SUBLIME(SOLID, GAS), DEPOSIT(GAS, SOLID);

      private final Phase from;
      private final Phase to;

      Transition(Phase from, Phase to) {
         this.from = from;
         this.to = to;
      }

      // Initialize Phase-Transition map
      private static final Map<Phase, Map<Phase, Transition>>
         m = Stream.of(values()).collect(groupingBy(t -> t.from,
         toMap(t --> t.to, t -> t,
            (x, y) -> y,  // this unifying function of the 2nd collector won't be used, but it is necessary as we have to mention a Map-Factory.
            () -> new EnumMap<>(Phase.class))));

      public static Transition from(Phase from, Phase to)  {
         return m.get(from).get(to);
      }
   }
}

Joshua Bloch mentions in its description after this piece of code, that in the 2nd edition of his book, they used an explicite iteration, to initialize this Phase-Transition map, which required more lines of code, but was easier to understand.

My question: I am interested to see code example of this 2nd edition of his book. Can anybody provide me an example (or similar example) of what he is quoting to?

This is what I found: From here, https://github.com/HugoMatilla/Effective-JAVA-Summary#33-use-enummap-instead-of-ordinal-indexing I know in the 2nd edition it is theme 33, to where he is revering to. Theme 33 has the same headline, like this post. Unfortunatelly if you follow this link this specific code example is not shown there.

Besides: I do not know if it is allowed to put code out of a book into a question here. So if it is not allowed i will delete the code example, then just inform me and I will do it.

CodePudding user response:

I believe this is the equivalent code snippet from the 2nd edition of the book where plain for loops were used to initialize the phase transition map:

// Using a nested EnumMap to associate data with enum pairs
public enum Phase {
    SOLID, LIQUID, GAS;

    public enum Transition {
        MELT(SOLID, LIQUID), FREEZE(LIQUID, SOLID),
        BOIL(LIQUID, GAS), CONDENSE(GAS, LIQUID),
        SUBLIME(SOLID, GAS), DEPOSIT(GAS, SOLID);

        final Phase src;
        final Phase dst;

        Transition(Phase src, Phase dst) {
            this.src = src;
            this.dst = dst;
        }

        // Initialize the phase transition map
        private static final Map<Phase, Map<Phase, Transition>> m =
            new EnumMap<Phase, Map<Phase, Transition>>(Phase.class);
        static {
            for (Phase p : Phase.values()) {
                m.put(p, new EnumMap<Phase, Transition>(Phase.class));
            }
            for (Transition trans : Transition.values()) {
                m.get(trans.src).put(trans.dst, trans);
            }
        }

        public static Transition from(Phase src, Phase dst) {
            return m.get(src).get(dst);
        }
    }
}
  • Related