• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dealing with a class with the generic T extends Number is there a way to perform operations on it?  RSS feed

 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, what is the best way to implement a size() method for a Range of T extends Number:



It seems like it is not wise to even attempt this...but I may be missing something.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Shipp wrote:


It seems like it is not wise to even attempt this...but I may be missing something.


"n2 - n1" doesn't work because autoboxing doesn't work on the Number class. How about manually unboxing, taking the difference, and then boxing the result?

Henry
 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

Henry
 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I agree. And after thinking about it more, it makes the best sense to just return a double and let the client-calling code deal with casting it to something else if they really need to . . . thanks Henry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!