title: java composite design pattern with generics
body: <p>I have two interfaces which are generic, <code>IComparable&lt;T&gt;</code>, with method <code>public boolean gt(T t)</code> and <code>ICombinable&lt;T&gt;</code>, with method <code>public T combine(T t)</code>, and a third interface <code>IPreferenceValue&lt;T&gt;</code> which extends both these interfaces, i.e., <code>IPreferenceValue&lt;T&gt; extends IComparable&lt;T&gt;, ICombinable&lt;T&gt;</code>, so that a preference value of type <code>T</code> can be compared and combined with other <code>T</code>'s. I have two concrete implementations of IPreferenceValue, one being a single-valued implementation : <code>IntegerValue implements IPreferenceValue&lt;IntegerValue&gt;</code> and the other being a List of preference values: <code>PreferenceVector&lt;S extends IPreferenceValue&lt;S&gt;&gt; implements IPreferenceValue&lt;PreferenceVector&lt;S&gt;&gt;</code>.</p>
  
  <p>Elsewhere I have an algorithm that performs various things on IPreferenceValues and I want to treat the single valued and list valued Preference values the same way via the interface, like the Composite Design Pattern. I also want to have some sort of Factory that would create IPreferenceValues based on some parameter, i.e., create a single-value object, or create a list-valued object. However when I am creating these PreferenceValues, I don't care about the type Parameter, I just want to create objects of type IPreferenceValue, the factory would return the concrete implementation - and just use the interface to interact with the objects.</p>
  
  <p>However I am having some difficulty getting this to work. Firstly if I declare my values using the raw type IPreferenceValue, I am getting these warnings - "<code>IPreferenceValue is a raw type. References to generic type IPreferenceValue&lt;T&gt; should be parameterized</code>". Are these warnings avoidable? Elsewhere, where I declare them as<code>IPreferenceValue&lt;?&gt;</code>, I get the following error: <code>The method gt(capture#6-of ?) in the type IComparable&lt;capture#6-of ?&gt; is not applicable for the arguments (IPreferenceLevel&lt;capture#7-of ?&gt;)</code>. Any advice or pointers greatly appreciated...</p>
  
  <p>EDIT:
  Here is where I get the second error (<code>The method gt(capture#12-of ?) in the type IComparable&lt;capture#12-of ?&gt; is not applicable for the arguments (IPreferenceLevel&lt;capture#13-of ?&gt;)</code>)</p>
  
  <pre><code>private IPreferenceLevel&lt;?&gt; calculateMinValue()
  {
      IPreferenceLevel&lt;?&gt; min = null;
      for (IPreferenceLevel&lt;?&gt; value : tupleToValueMap.values())
      {
          if ((min == null) || (min.gt(value).equals(Comparison.TRUE)))
          {
              min = value;
          }
      }
      return min;
  }
  </code></pre>
  
  <p>However even when I put in a type for this method, i.e., if i rewrite as follows -</p>
  
  <pre><code>private &lt;X&gt; IPreferenceLevel&lt;X&gt; calculateMinValue()
  {
      IPreferenceLevel&lt;X&gt; min = null; ...
  </code></pre>
  
  <p>This still doesn't compile - <code>The method gt(X) in the type IComparable&lt;X&gt; is not applicable for the arguments (IPreferenceLevel&lt;capture#10-of ?&gt;)</code></p>