Re: Smuggling information to enums
Mark Space wrote:
Roedy Green wrote:
On Tue, 24 Mar 2009 11:44:54 -0400, Eric Sosman <Eric.Sosman@sun.com>
wrote, quoted or indirectly quoted someone who said :
Perhaps you might try a less colorful and more mundane term
than "smuggling" to explain what you want to do ..
"get" information from the outside world into the enum for its use. It
seems to be doing all it can to prevent me. I was prepared to jump
through some hoops to get a small amount of data in. Hence the term
"smuggling".
Why use enums? Enums are constants that don't change. If you are
reading data from the outside world, those aren't really enums.
I'd read the values you need into some custom object then dump the
object into a HashSet (or HashMap). Then you can implement your own
identity semantics on those objects, just like enum does, but with out
the restrictions that makes enums difficult to work with in these
situations.
The real question is: what are you actually trying to do here? What
kind of operations do you envision for these objects? Total, not just
the ones you think would be really cool for an enum. Don't forget "read
and write" because apparently that's what messing you up here.
I'll second Mark's comments, and offer an example that may
convince you you're on the wrong track.
It seems you want an enum that's something like a colored
integer: Each enum instance (ZERO, ONE, ...) has its immutable
ordinal value and a characteristic color established at construction
time. ZERO is a pale yellow, ONE is a frosty blue, and so on. And
the enum class has a getColor() method through which each instance
can report its color to a client. All straightforward and above-
board so far.
But the strange thing you want to do is to give each enum a
different color depending on who's looking at it. So if one of
the CSVReader instances calls ZERO.getColor() it gets pale yellow
as above, but another CSVReader calls ZERO.getColor() and gets
revolting chartreuse instead. On the very same ZERO instance,
mind you! Where is the "integrity" of the ColoredInteger's color
if different observers see different things? What does the color
property "mean?"
It's been suggested that you could implement a setColor()
in your enum class, and there's nothing stopping you from doing
so (in some circumstances it might even be useful). But if one
CSVReader paints all the digits to its liking and starts using
them, it will be upset and/or confused when a second CSVReader
comes along and imposes its own preferred color scheme. This is
probably a poor idea -- unless you're trying to write a Jackson
Pollock simulator.
Stepping away from the fanciful, I think you need to examine
your reasons for wanting to (ab)use enums this way. If you want
a per-CSVReader set of things, you can use an ordinary inner (non-
static) class whose instances belong to their containing CSVReader.
Or if you want (static) enums and the ability to `switch' on them,
you can give each CSVReader its own Map or Maps to associate enum
instances with the CSVReader's own choice of colors.
--
Eric.Sosman@sun.com