Closed GoogleCodeExporter closed 9 years ago
I've come across a couple of uses, mostly in tests, for a function like this.
What is the use case you are seeing for this function?
The enum singleton pattern is used since it guarantees that the class cannot be
instantiated without having to do everything all of the work normally necessary
to prevent instantiation (ie final class, private constructors). You also get
serializability for free.
Original comment by boppenh...@google.com
on 12 Oct 2010 at 10:42
My use case was joining some enum's names for use in a Lucene query. Something
similar with SQL comes to mind (both when building a query with e.g. the `IN`
operator or when serializing multiple enums with a `Joiner` etc.). Probably
anything regarding serialization of multiple enums.
On a side note: I also wonder if functions should be provided to parse a string
into Java's primitive types, including `int`, `float`, and maybe even
`boolean`. In Python, that's as simple and common as `map(int,
integer_strings)`. That might require a default behaviour for parsing
exceptions, probably returning `null` in those cases. In turn, one would have
to filter those out with a `Predicate`, so I can imagine a method doing all
this for each (appropriate) primitive type in classes like `Ints` et al.
Original comment by j...@nwsnet.de
on 20 Oct 2010 at 10:48
To add a use case:
I'm in the process of introducing enums in places where lots of strings are
used as parameters to configure stuff. The enum names directly correspond to
the values they replace.
Having the above function would make it easy to override or wrap methods that
accept string iterables/collections with ones that take iterables/collections
of an enum instead, thus easing the transition to enum parameters which provide
some compile-time safety (against typos and misspellings).
Original comment by j...@nwsnet.de
on 11 Nov 2010 at 2:32
Now that the `Enums` class has been introduced in r260, it might be extended
with this suggestion.
Original comment by j...@nwsnet.de
on 18 Mar 2011 at 6:12
Ah yes. Enums would be the place. This would be the reverse of
Enums.valueOfFunction(), although it could be a singleton.
And the name is goofy, IMO. How about fromName() and toName(), each returning a
function.
And: It would be nice if toName() would return null if the provided enum was
null. Similarly the new valueOfFunction will return null if you provide it with
a null String, but it will do the same for any invalid string. That's weird. I
think it would be better to throw the IllegalArgumentException.
Original comment by ray.j.gr...@gmail.com
on 19 Mar 2011 at 12:14
Original comment by kevinb@google.com
on 18 Jul 2011 at 3:51
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:15
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:09
Original issue reported on code.google.com by
j...@nwsnet.de
on 29 Sep 2010 at 8:13