JDK-5034509 : (enum) Add a function to query if enum name is a valid enum constant name
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • Submitted: 2004-04-20
  • Updated: 2024-04-12
  • Resolved: 2021-10-21
Related Reports
Duplicate :  
Relates :  
Description
###@###.### 2004-04-20

J2SE Version (please include all output from java -version flag):
  java version "1.5.0-beta2"
  Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b44)
  Java HotSpot(TM) Client VM (build 1.5.0-beta2-b46, mixed mode, sharing)

Does this problem occur on J2SE 1.3.x or 1.4.x?  Yes / No (pick one)
  N/A

Operating System Configuration Information (be specific):
  WinXP

Hardware Configuration Information (be specific):
  Dell Optiplex

Bug Description:
  Like the new enum support. It's great. However, just had to write 
  some ugly code...

  try {
   PrimitiveType.Kind kind = PrimitiveType.Kind.valueOf(typeName);
   // typeName must be the name of a primitive - we will process from here
   ...
 } catch (IllegalArgumentException e) {
   // The case where typeName is not a primitive - controlo-flow by 
   exception handling is evil
   ...
 }

  Each enum has a static valueOf(String) method. Enum itself defines 
  static <T extends Enum<T>> T valueOf(Class<T>, String), which I presume 
  the generated code for valueOf(String) in each enum class chains on to.

  Could we have an interogation function static boolean hasValue(String) 
  and static <T extends Enum<T>> boolean hasValue(Class<T>, String) to go 
  with this so that we can query the enum without resorting to exceptions?

Workarround:

  Instead of resorting to try/catch, use a loop over 
  PrimitiveType.Kind.values() searching for a matching name:

  boolean hasValue(String name) {
    for(Primitive.Kind kind: Primitive.Kind.values())
      if(kind.name().equals(name)) return true;
    return false;
  }

  This looks like utility code though.
###@###.### 10/28/04 00:53 GMT

Comments
Not a pressing issue; will re-open if there is sufficient cause.
21-10-2021

An alternative might be to have a non-throwing variant of valueOf(). For example, in an enum type of E, the following method might be added: public static Optional<E> getValueOf(String name) One can do this today (Java 8) using the array returned by values(): Optional<E> match = Arrays.stream(E.values()).filter(e -> name.equals(e.name())).findFirst(); Of course, this does a linear search of the Enum values, which might be slow, though we generally expect there to be relatively few enum values, don't we? This could be slightly optimized by JDK-8073381, which proposes to add a method to get the enum values as a list or a stream. In this case the Arrays.stream(E.values()) would be replaced by an API call, which would make the code shorter, and it would avoid creating a copy of the values array every time.
25-03-2015

EVALUATION Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=18752
26-02-2007

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
14-06-2004

EVALUATION Will consider such a convenience method for a future release. ###@###.### 2004-04-28
28-04-2004