I'm building a Java library, and I want the user to be able to define his own versions of some built-in classes, implementing interfaces.
Now I got the following classes, where Evaluator is the built-in main routine that does some processing on Values (built-in or user-defined classes, implementing the Value interface)
The Evaluator is constructed with a Processor (built-in or user-defined class, implementing the Processor interface), and the Processor does the work on the Values.
Thus now I want to work with the Value interface in the Evaluator class and the Processor interface, while working with UserValues in the UserProcessor class, but I don't seem to get this right
Value interface:
public interface Value {
public Value valueOf(String s)
}
User defined Value class which is processed by the program
public class UserValue implements Value { … }
public class DefaultValue implements Value { … }
Interface defining what actions on objects of Value there will be
public interface Processor<T extends Value> {
public T process(T value);
}
Class which does something with some objects of the user-defined Value class.
UserProcessor MUST use UserValue
public class UserProcessor implements Processor<UserValue>
public class DefaultProcessor implements Processor<DefaultValue>
Class containing the main routine:
public class Evaluator {
private Processor<Value> processor;
public Evaluator() {
setProcessor(new DefaultProcessor());
}
public void setProcessor(Processor<Value> processor) {
this.processor = processor;
}
public Value evaluate(String s) {
Value val = processor.ValueOf(s);
return processor.process(val);
}
}
setProcessor(new DefaultProcessor())
gives an error:
The method setProcessor(Processor<Value>) in the type Evaluator is not applicable for the arguments (DefaultProcessor)
So to clarify the routine: the Evaluator is constructed, optionally the user sets a custom Processor and Value class, and the evaluator tries to parse a String.