title: Design Help! java generics in a enum factory transformer!
body: <p>I am wondering if I could get some input on a good way to design this.  I will put my approach but I think that there is a better solution (hence the question :) ).</p>
  
  <p>I want to create an enum (to make clear the options and to avoid a singleton architecture) that has accessors for creating one object from another.  But what those objects are is pretty flexible.  </p>
  
  <p>Think of it as a way to limit the number of options for this transformation.  </p>
  
  <p>Let me go into a little of the hierarchy. If I am going from a diverse set of objects to something like this:</p>
  
  <pre><code>class Base {...}
  class ValueA extends Base {...}
  class ValueB extends Base {...}
  </code></pre>
  
  <p>I was thinking of doing something like this:</p>
  
  <pre><code>public enum ValueTransformer{
    VALUE_A{
  
      @Override
      public &lt;T&gt; T createVo (Class&lt;T&gt; expectedRtn, Object obj) {
        ValueA retObj = null;
  
        if (expectedRtn == getReturnType ()) {
          if (obj != null &amp;&amp; CanBeTranslatedToA.class == obj.getClass ()) {
            retObj = new ValueA ();
            /*...*/
          }
        }
  
        return retObj;
      }
  
      @Override
      public Class&lt;ValueA&gt; getReturnType () { return ValueA.class; }
  
    },
    VALUE_B {
      @Override
      public Class&lt;ValueB&gt; getReturnType () { return ValueB.class; }
  
      @Override
      public &lt;T&gt; T createVo (Class&lt;T&gt; expectedRtn, Object obj) {
        ValueB retObj = null;
  
        if (expectedRtn == getReturnType ()) {
          if (obj != null &amp;&amp; CanBeTranslatedToB.class == obj.getClass ()) {
            retObj = new ValueB ();
            /*...*/
          }  else if (obj != null &amp;&amp; AnotherClassForB.class = obj.getClass ()){
            retObj = new ValueB();
            /* ... */
          }
        }
  
        return retObj;
      }
    };
  
    public abstract &lt;T&gt; Class&lt;T&gt; getReturnType ();
    public abstract &lt;T&gt; T createVo (Class&lt;T&gt; expectedRtn, Object obj);
  }
  </code></pre>
  
  <p>Is this a decent design?  This enum will probably grow, and what ValueA and ValueB can be created from might change (as the sys grows).  I could return a 'Base' in all those cases, but it would require a cast and a check.  I'd prefer to not have that.</p>
  
  <p>Is it necessary for me to have the expectedRtn parameter?  Should I be using Generics at all?  I am fairly new to Java so I am not always sure the best way to handle this case.</p>
  
  <p>Thanks for any tips!!!!</p>