title: Case-insensitive matching of a string to a Java enum
body: <p><code>Java</code> provides a <code>valueOf()</code> method for every <code>Enum&lt;T&gt;</code> object, so given an <code>enum</code> like</p>
  
  <pre><code>public enum Day {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
  }
  </code></pre>
  
  <p>one can do a lookup like</p>
  
  <pre><code>Day day = Day.valueOf("Monday");
  </code></pre>
  
  <p>If the string passed to <code>valueOf()</code> does not match (case sensitive) an existing <code>Day</code> value, an <code>IllegalArgumentException</code> is thrown.</p>
  
  <p>To do a case-insensitive matching, one can write a custom method inside the <code>Day</code> enum, e.g.</p>
  
  <pre><code>public static Day lookup(String day) {
    for (Day d : Day.values()) {
      if (d.name().equalsIgnoreCase(day)) {
        return type;
      }
    }
    return null;
  }
  </code></pre>
  
  <p>Is there any generic way, without using caching of values or any other extra objects, to write a static <code>lookup()</code> method like the above only once (i.e., not for every <code>enum</code>), given that the <code>values()</code> method is implicitly added to the <code>Enum&lt;E&gt;</code> class at compile time?</p>
  
  <p>The signature of such a "generic" <code>lookup()</code> method would be similar to the <code>Enum.valueOf()</code> method, i.e.:</p>
  
  <pre><code>public static &lt;T extends Enum&lt;T&gt;&gt; T lookup(Class&lt;T&gt; enumType, String name);
  </code></pre>
  
  <p>and it would implement exactly the functionality of the <code>Day.lookup()</code> method for any <code>enum</code>, without the need to re-write the same method for each <code>enum</code>.</p>