title: Create a enum factory method out of a unique instance value
body: <p>I have created an Enum to define certain actions. Programming against a external API I am forced to use an <code>Integer</code> to express this action. That's why I have added an integer instance field to my Enum. This should be d'accord with Joshua Bloch's Effective Java, instead of relying on <code>ordinal()</code> or the order of the Enum constants using <code>values()[index]</code>.</p>
  
  <pre><code>public enum Action {
  
      START(0),
  
      QUIT(1);
  
      public final int code;
  
      Protocol(int code) {
           this.code = code;
       }
  }
  </code></pre>
  
  <p>I get an integer value <code>what</code> from the API and now I want to create an Enum value out of it, how can I implement this in the most generic fashion?</p>
  
  <p>Obviously, adding such a factory method, will not work. You cannot instantiate an Enum.</p>
  
  <pre><code>Action valueOf(int what) {
       return new Action(what);
  }
  </code></pre>
  
  <p>Of course, I can always make a switch-case statement and add all the possible codes and return the appropriate constant. But I want to avoid defining them in two places at the same time.</p>