title: Java: What is an alternative to using generics in this design?
body: <p>I'm working on an open-source Java library that will allow one to compute certain quantities, such as Gini index, of an attribute that takes on a finite number of values. (Formally, it computes the Gini index of the discrete distribution associated with the attribute <code>A</code>, but this is not relevant here.)</p>
  
  <p>For example, one will be able to do the following</p>
  
  <pre><code>String[] namesArray = {"primary_school", "high_school", "university"};
  Calculator&lt;String&gt; calc =new Calculator&lt;String&gt;(namesArray);
  
  // p.getEducationLevel() returns `"primary_school"`, `"high_school"`, or `"university"`.
  for (Person p : peopleCollection) {
      calc.increment(p.getEducationLevel());
  }
  
  // e.g. the Gini index of the distribution
  double currentStat = calc.getCurrentValue();
  </code></pre>
  
  <p>The idea is to allow users of the library to use their own type to refer to attribute values; in this case, I am using strings (e.g. <code>"primary_school"</code>). But I might want to use integers or even my own type <code>AttributeValue</code>.</p>
  
  <p>I solve this by defining</p>
  
  <pre><code>public class Calculator&lt;T&gt; {
      /* ... */
  }
  </code></pre>
  
  <p>However, using generics causes some problems in the implementation: for example, if I want to maintain a collection of pairs of type <code>(T, double)</code>, I have to do nasty type casts:</p>
  
  <pre><code>public class Calculator&lt;T&gt;
          /* ... */
          private Queue&lt;StreamElement&lt;T&gt;&gt; slidingWindow;
          /* ... */
          class StreamElement&lt;T&gt; {
                  private T label;
                  private double value;
  
                  StreamElement(T label, double value) {
                          this.label = label;
                          this.value = value;
                  }
  
                  public T getLabel() {
                          return label;
                  }
                  public double getValue() {
                          return value;
                  }
          }
          /* ... */
              slidingWindow.add(new StreamElement&lt;T&gt;(label, value));
              if (slidingWindow.size() &gt; windowSize) {
                      StreamElement lastElement = slidingWindow.remove();
                      // XXX: Nasty type cast
                      decrement((T)lastElement.getLabel(), lastElement.getValue());
              }
          /* ... */
  }
  </code></pre>
  
  <p>Here is the warning produced by <code>javac</code>:</p>
  
  <pre><code>Calculator.java:163: warning: [unchecked] unchecked cast
              decrement((T)lastElement.getLabel(), lastElement.getValue());
                                               ^
    required: T
    found:    Object
    where T is a type-variable:
      T extends Object declared in class Calculator
  1 warning
  </code></pre>
  
  <p><strong>Update.</strong> If I do not do the type cast, I get</p>
  
  <pre><code>Calculator.java:163: error: no suitable method found for decrement(Object,double)
              decrement(lastElement.getLabel(), lastElement.getValue());
              ^
      method Calculator.decrement(T) is not applicable
        (actual and formal argument lists differ in length)
      method Calculator.decrement(T,double) is not applicable
        (actual argument Object cannot be converted to T by method invocation conversion)
    where T is a type-variable:
      T extends Object declared in class Calculator
  1 error
  </code></pre>
  
  <p>Questions:</p>
  
  <ul>
  <li>What is a proper, clean way to do the type cast?</li>
  <li>What would be an alternative to using generics here?</li>
  <li><strong>More concretely, would it be better to instead define a class <code>Label</code> which user could extend to <code>MyLabel</code> and then use <code>MyLabel</code> for attribute values?</strong> This means that <code>Calculator</code> would no longer be a generic type; in the implementation we'd have <code>class StreamElement { Label label; /* ... */ }</code> et cetera.</li>
  </ul>