CHAPTER 6
ENUMS AND ANNOTATIONS
174
mistake in the transition table or forget to update it when you modify the
Phase
or
Phase.Transition
enum type, your program will fail at runtime. The failure may
be an
ArrayIndexOutOfBoundsException
, a
NullPointerException
, or (worse)
silent erroneous behavior. And the size of the table is quadratic in the number of
phases, even if the number of non-null entries is smaller.
Again, you can do much better with
EnumMap
. Because each phase transition
is indexed by a
pair
of phase enums, you are best off representing the relationship
as a map from one enum (the “from” phase) to a map from the second enum (the
“to” phase) to the result (the phase transition). The two phases associated with a
phase transition are best captured by associating them with the phase transition
enum, which can then be used to initialize the nested
EnumMap
:
Do'stlaringiz bilan baham: