title: How do you refer to nested types using generics in Java?
body: <p>How do you create a generic class that refers to nested generic types?</p>
  
  <p>I'm trying to create a Comparator class which can compare the inner types of B without wanting to expose what those types are. In the following example I get a compiler warning for raw casting my T inner nested values to Comparable:</p>
  
  <pre><code>public class SSCCE {
  
      // Compare my A instances.
      class AComparator&lt;T extends B&gt; implements Comparator&lt;T&gt; {
  
          @Override
          public int compare(final T o1, final T o2) {
              return o1.getValue().compareTo(o2.getValue());
          }
      }
  
  
      class A extends B&lt;Integer&gt; {
          @Override Integer getValue() { return 1; }
      }
  
      class A2 extends B&lt;String&gt; {
          @Override String getValue() { return "Test String!"; }
      }
  
      abstract class B&lt;T extends Comparable&lt;T&gt;&gt; {
          abstract T getValue();
      }
  
      public static void main(String[] args) {
          SSCCE sscce = new SSCCE();
          AComparator&lt;A&gt; comparator = sscce.new AComparator&lt;&gt;();
          comparator.compare(sscce.new A(), sscce.new A());
      }
  }
  </code></pre>
  
  <p>Is it possible to represent the inner values using to safely allow casting?</p>
  
  <p>Things I've tried:</p>
  
  <ul>
  <li><p>Creating a wildcard comparable (uncompilable) :</p>
  
  <pre><code>class AComparator2&lt;T extends B&lt;? extends Comparable&lt;?&gt;&gt;&gt; implements Comparator&lt;T&gt; {
  
      @Override
      public int compare(final T o1, final T o2) {
          Comparable&lt;?&gt; o1value = (Comparable) o1.getValue();
          Comparable&lt;?&gt; o2value = (Comparable) o2.getValue();
          return o1value.compareTo(o2value);
      }
  }
  </code></pre></li>
  <li><p>Declaring a secondary generic parameter type (U), which simply postpones the problem:</p>
  
  <pre><code>class AComparator3&lt;T extends B&lt;U&gt;, U extends Comparable&lt;U&gt;&gt; implements Comparator&lt;T&gt; {
  
      @Override
      public int compare(final T o1, final T o2) {
          U o1value = o1.getValue();
          U o2value = o2.getValue();
          return o1value.compareTo(o2value);
      }
  }
  ...
  AComparator3&lt;A, Comparable&lt;U&gt;&gt; comparator = sscce.new AComparator3();
  </code></pre></li>
  </ul>
  
  <p>This comparator isn't to compare two instances of the classes A, rather part of their contents.</p>