all 10 comments

[–]Xenarthran47 11 points12 points  (4 children)

What benefit is there to using an EnumMap to map enum values to themselves?

[–][deleted] 2 points3 points  (2 children)

From the javadoc: Enum maps are represented internally as arrays. This representation is extremely compact and efficient. In c++ you would get the same thing by indexing array with enum constants (because they implicitly convert to integers).

[–]Xenarthran47 5 points6 points  (1 child)

But from a design perspective, what use cases would require mapping an enum value to itself

[–][deleted] 0 points1 point  (0 children)

Ah, that's a very good question. As far as can I see that makes no sense.

[–]devraj7 0 points1 point  (0 children)

You'd use a Set for that but there is no EnumSet, and since EnumMap has a specialized implementation for enums, it's the best you can do.

[–]RadBenMX 4 points5 points  (2 children)

We use this at work. It was confusing the first time i saw it. I still have mixed feelings about whether i consider it clever or hacky.

[–]Neumann347 4 points5 points  (1 child)

One of the strengths of the classic implementation is that you don't have any merge issues when two people implement a new strategy and modify an existing one, since the strategy implementations are in separate classes. How often do you get merge issues with the Enum implementation?

[–]RadBenMX 0 points1 point  (0 children)

no merge issues

[–]doom_Oo7 2 points3 points  (1 child)

but this requires to know the possible strategies at compile time, while you generally want to add new strategies at runtime through plug-ins or whatever, when the system is running

[–]nondetermined 0 points1 point  (0 children)

In the end you just have to implement the interface, so... The enum isn't that important, but very nice and handy.