kucci / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Add function to get an enum's name #435

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'd expect the `Functions` class to contain a method that transforms an enum to 
its name (via `name()`).

While I originally implemented it as a constant using a naive approach, here is 
an implementation following `ToStringFunction` (though I've got to admit that I 
don't know the actual benefits of using the "enum singleton pattern"):

  public static Function<Enum<?>, String> enumName() {
    return EnumNameFunction.INSTANCE;
  }

  private enum EnumNameFunction implements Function<Enum<?>, String {
    INSTANCE;

    public String apply(Enum<?> anEnum) {
      checkNotNull(anEnum);
      return anEnum.name();
    }
  };

Would be nice if you could add this function well as others of common utilify 
that you or the Guava users come up with/I didn't think of yet.

Original issue reported on code.google.com by j...@nwsnet.de on 29 Sep 2010 at 8:13

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 18 Jul 2011 at 3:51

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09