title: Java Generic Class Mapper Many to one
body: <p>I've realized a little framework to Convert a generic Entity of type <code>A</code> to one of Type <code>B</code>.</p>
  
  <p>So I created an interface:</p>
  
  <pre><code>public interface IConvert&lt;A,B&gt; {
      public B convert (A entity);
      public List&lt;B&gt; convertList(List&lt;A&gt; entitylist);
  }
  </code></pre>
  
  <p>And an abstract class to generalize the <code>convertList</code> method:</p>
  
  <pre><code>public abstract class AbstractConverter&lt;A,B&gt; implements IConvert&lt;A, B&gt; {
  
      @Override
      public abstract B convert(A entity) ;
  
      @Override
      public List&lt;B&gt; convertList(List&lt;A&gt; entitylist) {
          List&lt;B&gt; bs=new ArrayList&lt;B&gt;();
          for (A a : entitylist) {
              bs.add(convert(a));
          }
          return bs;
      }    
  }
  </code></pre>
  
  <p>Suppose we have to map a <code>BClass</code> instance to an <code>AClass</code> instance:</p>
  
  <pre><code>public class AClassConverter extends AbstractConverter&lt;BClass, AClass&gt;{
  
      @Override
      public AClass convert(BClass entity) {
          return new AClass(); //BClass to AClass mapping
      }
  }
  </code></pre>
  
  <p>I can simply define a new class <code>AClassConverter</code>, extending the abstract one and implement the specific convert method. The <code>convertList</code> method is free.</p>
  
  <p>What I'm  trying to understand is how to generalize an m to one type converter without have to always reimplement the <code>convertList</code> method for entity:</p>
  
  <pre><code>public class AClassConverter extends AbstractConverter&lt;BClass, AClass&gt; {
  
      @Override
      public AClass convert(BClass entity) {
      return new AClass(); //BClass to AClass mapping
      }
  
      public AClass convert(CClass entity) {
          return new AClass(); //CClass to AClass mapping
      }
  
      public List&lt;AClass&gt; convert(List&lt;CClass&gt; entityList) {
          //foreach &gt; call convert(c);
          //return aClassList;
      }
  }
  </code></pre>
  
  <p>Consider that <code>AClass</code>, <code>BClass</code>, and <code>CClass</code> don't extend any common class (apart from <code>Object</code>).
  And <code>AClass</code>, <code>BClass</code>, and <code>CClass</code> don't have cross reference (<code>AClass</code> is unaware of <code>BClass</code> so I can't define in <code>BClass</code> something as <code>convertToAClass</code>...).</p>
  
  <p>I'd need something as:</p>
  
  <pre><code>public class AClassConverter extends AbstractConverter&lt;BClass, AClass&gt; , AbstractConverter&lt;CClass, AClass&gt;
  </code></pre>
  
  <p>!!!</p>
  
  <p>Regards, and sorry for the confusion.</p>