Scott Shipp wrote:I'd love that...but to do that, I'd have to know what to unbox it to which means I'd have to know the type at runtime, which I don't. Or is there a way? Can you capture it somewhere? Maybe pass it in during construction or something?
Well, yes, no, or maybe... Technically, all the subclasses of the Number class supports all of its methods, so you can just assume its a double and use the arithmetic with the most range -- in this case, it's double. Unfortunately, it is not very efficient, and you will always be returning a Double instance for the result.
If you want the result to be the same type as T, then you have to use the instanceof operator.
Scott Shipp wrote:Or did you mean something like the bottom answer here: http://stackoverflow.com/questions/22191809/generic-type-subtraction-in-java ? I figure that's the only way to do it. I just hate, hate, hate using instanceof.
You could spend some time using the reflection library instead -- you will grow to love using the instanceof operator...
Scott Shipp wrote:How to handle the case where someone extends Number with their own custom class and creates a new Range?? I think this would be a problem with either approach. So maybe it is better to use "instanceof" and if it is not one of the known cases, throw some exception. Any idea what kind of exception this would be? UnsupportedOperationException?
I don't have a strong opinion regarding how an error is returned.
However, from what you are running into, I think it is safe to assume that autoboxing will likely never be supported with generics.