Reputation: 851
I am attempting to write JUnit tests for my Expression Tree. The tree is composed of BaseExpressionTree< ValueType> (terminal nodes), ExpressionOperator< T> (non-terminal nodes), and CompositeExpressionTree< ValueType> (Sub Trees).
The Data Structure is supposed to be compatible with String, Double, and List< String> or List< Double> as BaseExpression (terminal leaves).
The classes are implemented with generics. there were no problems with the Double and String implementations, however List< String> and List< Double> implementations are causing conflicts with the Generics.
The heart of the problem is the ListOperator constructor. ListOperator is ment to represent operations on structures such as ArrayList and LinkedList. I would like to declare the class as following:
public class ListOperator<List<T>> implements ExpressionOperator<List<T>>{
...
but instead 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 ListOperator (which represents a List) is conflicting with the < T> in ExpressionOperator (which is supposed to 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 doesn't involve using wild cards? The homework instructions were fairly explicit, the generics for class definitions are how they were described in the prompt.
I could use wild cards in the constructor parameters, but so far I haven't been able to do this.
public ListOperator (? extends ExpressionOperator<T> operator){
and
public ListOperator (< ? extends ExpressionOperator<T>> operator){
both give errors.
Upvotes: 3
Views: 134
Reputation: 198371
I think your problem has to do with using ArrayList<Double>
for the ValueType
type parameter, instead of List<Double>
, which results in a conflict with ListOperator
.
Try using List<Double>
everywhere instead of ArrayList<Double>
.
UPDATE:
Your ListOperator
class should look like
public class ListOperator<T> implements ExpressionOperator<List<T>> {
// unchanged
...
public ListOperator(ExpressionOperator<T> operator) {
...
}
}
and you should be invoking it as new ListOperator<Double>(myDoubleOperator)
.
Upvotes: 1