title: Parameterized Type Parameters?
body: <p>I'm trying to create library with a container that releases instances of its contained objects according to descriptors it is passed.  I'd like to make it so the descriptor determines the type of the returned object, but the descriptor can specify a bounded type.  How do I implement this?  For example the closest I can get is:</p>
  
  <pre><code>/*Block 1 - First Attempt.  Compiles, but forces user to cast*/
  interface ItemDescriptor&lt;I&gt; {
      Class&lt;? extends I&gt; getType();
  }
  
  interface ArchiveContainer&lt;I, D extends ItemDescriptor&lt;? extends I&gt;&gt; {
      Iterable&lt;? extends D&gt; getDescriptors();
      I getItem(D descriptor);
  }
  
  //Implementations
  class ChannelItemDescriptor&lt;I extends ByteChannel&gt; implements ItemDescriptor&lt;I&gt;
  {
      final Class&lt;? extends I&gt;  type;
  
      ChannelItemDescriptor(Class&lt;I&gt; type) {
          this.type = type;
      }
  
      @Override Class&lt;? extends I&gt; getType() {return type;}
  }
  
  class ChannelArchive implements ArchiveContainer&lt;ByteChannel, ChannelItemDescriptor&lt;? extends ByteChannel&gt;&gt; {
      @Override ByteChannel getItem(ChannelItemDescriptor&lt;? extends ByteChannel&gt; descriptor) {...}
  }
  </code></pre>
  
  <p>The above code compiles, but the problem is <code>ChannelArchive</code>'s <code>getItem</code> can return <code>SeekableByteChannel</code>s as well.  The user of this library knows this at compile time (because they know the type parameter of the descriptor), so I'm trying to avoid adding a method parameter of type <code>Class</code> for forcing the user to explicitly cast the returned value to <code>SeekableByteChannel</code> when necessary.  I can't figure out how to get <code>getItem</code> to return a specific subtype of <code>ByteChannel</code> without forcing the user to cast.  I want to do this:</p>
  
  <pre><code>/*Block 2 - Test code*/
  ChannelArchive archive = ...;
  ChannelItemDescriptor&lt;SeekableByteChannel&gt; desc = ...;
  ChannelItemDescriptor&lt;ByteChannel&gt; otherDesc = ...;
  SeekableByteChannel sbc = archive.getItem(desc);
  SeekableByteChannel sbc = archive.getItem(otherDesc); //Should fail to compile, or compile with warning
  ByteChannel bc = archive.getItem(otherDesc);
  </code></pre>
  
  <p>I <em>could</em> add a <code>Class&lt;? extends I&gt;</code> parameter to each method, but the code for the method would completely ignore <code>Class</code> method parameter!  It's only purpose would be to help the compiler infer types.  I think it just obfuscates the code so much that it would be easier to just have the user use <code>instanceof</code> checks and casts.</p>
  
  <p>I've tried this:</p>
  
  <pre><code>/*Block 3 - Failed attempt.*/
  class ChannelArchive implements ArchiveContainer&lt;ByteChannel, ChannelItemDescriptor&lt;? extends ByteChannel&gt;&gt; {
      //Won't compile, getItem doesn't override
      @Override &lt;II extends ByteChannel&gt; II getItem(ChannelItemDescriptor&lt;II&gt; descriptor) {...}
  }
  </code></pre>
  
  <p>but that doesn't work:  <code>ChannelArchive is not abstract and does not override abstract method getItem(ChannelItemDescriptor&lt;? extends ByteChannel&gt;) in ArchiveContainer</code>.  I assume this is because the second type parameter <code>&lt;II extends ByteChannel&gt;</code> has different type erasure than <code>&lt;? extends ByteChannel&gt;</code>?</p>
  
  <p>I've also tried this, which compiles:</p>
  
  <pre><code>/*Block 4 - Almost specific enough*/
  interface ArchiveContainer&lt;I, D extends ItemDescriptor&lt;? extends I&gt;&gt; {
      Iterable&lt;? extends D&gt; getDescriptors();
      &lt;II extends I, DD extends ItemDescriptor&lt;II&gt;&gt; II getItem(DD descriptor);
  }
  
  class ChannelArchive implements ArchiveContainer&lt;ByteChannel, ChannelItemDescriptor&lt;? extends ByteChannel&gt;&gt; {
      @Override &lt;II extends ByteChannel, DD extends ItemDescriptor&lt;II&gt;&gt; II getItem(DD descriptor) {...}
  }
  </code></pre>
  
  <p>Even though it compiles, it won't <em>really</em> work because I need a <code>ChannelItemDescriptor</code> inside that method, and the resulting cast would defeat the purpose of using the added type-safety of generics.</p>
  
  <p>I don't see why I can't do it, because the right types are known at compile time.  What I really need on that <code>ArchiveContainer</code> interface is a parameterized type parameter, like: <code>&lt;II extends I, DD extends D&lt;II&gt;&gt;</code>.  What am I doing wrong?</p>
  
  <p>NOTE:  I don't actually use <code>ByteChannel</code> and <code>SeekableByteChannel</code>, but what I do use is quite similiar.</p>
  
  <hr>
  
  <p>That's to ruakh, I settled on the code in block 4.  In my case, its highly unlikely the user would send the wrong sublcass of <code>ItemDescriptor</code> in a call to a <code>getItem</code>, especially because the descriptors are all returned from the <code>ArchiveContainer</code> itself via <code>getDescriptors</code>!</p>