How good do you want the result to be? If the answer is "good enough, mostly", then this should be sufficent:
public Number myAdd(Number a, Number b){
return a.doubleValue() + b.doubleValue();
}
But if you want something that, say, matches the promotion semantics of Java primitives, you're probably going to have to write it yourself. And then you'll have to figure out what the rules are for all combinations of "non-standard" Number
implementations, including BigDecimal
, BigInteger
, AtomicDouble
, AtomicLong
, everything in org.apache.commons.lang.mutable
, and any random implementation that somebody might decide to write next Tuesday.
It's not clear what the right thing to do is in most of these cases -- converting everything to BigDecimal
, for instance, is not an option if one of the arguments is Apache Commons' Fraction.ONE_THIRD
; and besides, doing the conversion in a general way presents the same problems as doing the addition in a general way. But having an add()
method on Number
would require every Number
implementation to handle all these cases -- and that's probably why it isn't there.