I would like to create a template function that relies on a generic type's ability to support ++ and --. As we know, Java does not support operator overloading so apparently I need to define an interface that requires an increment() and a decrement() function:
public interface Quantized<E> extends Comparable<E> {
public <E extends Quantized> E increment();
public <E extends Quantized> E decrement();
}
public class Sequencer<E extends Quantized>{
private E value;
public <E extends Quantized> E doSometingWithSequence(){
...
E rv = value.increment();
return rv;
}
}
If I want the template to support any class then even knowing of an interface that says that ++ and -- are supported would not be enough as Java does not allow us to implement operators for classes. However, it would be nice to allow the template function to support types such as Integer without the need for a wrapper class to implement the interfaceQuantized.
Is there a way around defining a new interface (other than defining an abstract class)?