I want to express a mathematical subset relation in Java. In Mathematics we have nice relations like: the integers are a subset of the real numbers. I try to somehow express that in java with a Point class with generics:
public class Point<T extends Number> {
protected Number xCoord;
public T getXCoordinate() {
// do some stuff
return (T) this.xCoord;
}
Unfortunately I have to cast here to T, but then I get the usual warning of unchecked conversion, what I want to avoid. Here it would be nice if Number were instanciable, but it is unfortunately abstract. So I wonder if there is a common instanciable Superclass of Integer and Double (with an explicit Zero). I didn't find one. But mathematically we really do have N as a subset of R. How can I reflect this in Java? Here I am at the end of my knowledge according to generics, because I cannot define a subclass (say InstNumber) of Number that is instanciable and superclass of Integer and Double since I am not able to change the java.lang package. So does anyone have an Idea how to "make Points of Integers a subset of Points of Double" or find a common instanciable class InstNumber. However if not, why is the type safety so hard at this point?
In the end I try to implement an algorithm to encrypt with elliptic curves. But to do so you often need that each Point in FpxFp (p prime) c NxN is also a Point in RxR. So it I want to compute with these "Numbers" even if I didn't know the current subtype. Since mathematically a field is interesting in here it would be nice to have a kind of "abstract" but instanciable Subclass called Field of Number which is Parent class of all the children of number. In Field it would be sufficient to have a concrete 0 and 1 which are present in most subclasses of Number. Thus could Field be some "mathematical field" hence a set with explicit 0 and 1 (like Z/2Z which is indeed a mathematical field). Now let for instance Integer be a subclass of Field. The only "additional job" of the Integer class is to map the "abstract" 0 and 1 of Field to the concrete 0 and 1 interpretation of Integer. This could be also implemented for other subclasses.
I know that associativity is not really possible. But for most calculations and decisions in programming it would be nice to test whether a Field element is 0 or 1 (Specifically to invert a Field element). So I want to restrict myself to the calculation of existing numbers in R, not such things as NaN.
I am sorry that I could not give you more code, but I think its too much Code that I have right now to post it here. For short: I have a ProjectivePoint class which has to do the projective addition of points in RxR. But it would be nice if the calculations in this class also are availible on FpxFp c RxR. Which is mathematically more or less trivial, but I see no current solution in Java.