Your motivation is to return an object of the same type as the one with which it was called.
Unfortunately, Generics cannot help you. On your line
return (T) -objeto.doubleValue();
you really need to say
return new T(-objecto.doubleValue());
but this cannot work, because T is not known at compile time, and that is when a decision must be made on this.
, ,
public interface AbsEvaluator<N extends Number> {
N abs(N n);
}
,
AbsEvaluator<Integer> intAbs =
new AbsEvaluator<Integer>() { public Integer abs(Integer n) { return -n; }};
, .
, , , (, Java , ). , ,
<T,U,V> Function<T, V> compose(Function<U, V> g, Function<T, U> f);
, , , , . , Java 8.