Conflicting common types between interdependent classes

I am trying to write JUnit tests for my expression tree. The tree consists of BaseExpressionTree <ValueType> (terminal nodes), ExpressionOperator <T> (nonterminal nodes) and CompositeExpressionTree <ValueType> (Sub Trees).

The data structure is assumed to be compatible with String, Double, and List <String> or List <Double> as the base expression (terminal output).

Classes are implemented using generics. There were no problems with the Double and String implementations, however the List <String> and List <Double> implementations lead to conflicts with Generics.

The heart of the problem is the ListOperator constructor. ListOperator is designed to represent operations on structures such as ArrayList and LinkedList. I would like to declare the class as follows:

public class ListOperator<List<T>> implements ExpressionOperator<List<T>>{

...

but I can only declare it as follows:

public class ListOperator< T> implements ExpressionOperator<List<T>>{
    // a private field to store the String or Double operator to be used on the lists
    private ExpressionOperator<T> scalarOperator;

    /**
     * a constructor that takes one expression operator and stores it in the scalarOperator variable
     * @param       operator        an operation to be executed on a set of List operands
     */
    public ListOperator (ExpressionOperator<T> operator){
        this.scalarOperator=operator;
    }
}

basically the <T> in the ListOperator (which is a List) contradicts the <T> in the ExpressionOperator (which should represent what is inside the list).

Eclipse gives the following error output:

The constructor ListOperator<List<Double>>(DoubleOperator) is undefined

Is there a solution that does not involve the use of wild cards? The homework instructions were clear enough that the general definition of classes is how they were described in the tooltip.

I can use wild cards in the constructor options, but so far I have not been able to do this.

    public ListOperator (? extends ExpressionOperator<T> operator){

and

    public ListOperator (< ? extends ExpressionOperator<T>> operator){

both give errors.

+3
source share
1

, ArrayList<Double> ValueType List<Double>, ListOperator.

List<Double> , ArrayList<Double>.

UPDATE:

ListOperator

public class ListOperator<T> implements ExpressionOperator<List<T>> { 
    // unchanged
  ...
  public ListOperator(ExpressionOperator<T> operator) {
    ...
  }
}

new ListOperator<Double>(myDoubleOperator).

+1

All Articles