Consider the UnaryFunction
interface defined in Effective Java generics chapter .
public interface UnaryFunction<T> {
T apply(T arg);
}
and the following code for returning the UnaryFunction
// Generic singleton factory pattern
private static UnaryFunction<Object> IDENTITY_FUNCTION = new UnaryFunction<Object>() {
public Object apply(Object arg) { return arg; }
};
// IDENTITY_FUNCTION is stateless and its type parameter is
// unbounded so it's safe to share one instance across all types.
@SuppressWarnings("unchecked")
public static <T> UnaryFunction<T> identityFunction() {
return (UnaryFunction<T>) IDENTITY_FUNCTION;
}
Why is the cast of IDENTITY_FUNCTION
to (UnaryFunction<T>)
safe ?
The book says this about the question I am asking but I can't follow the logic here . Where are we invoking the apply
function which does the identity operation ? i am confused because it is that function which returns the same object passed into it without modifying anything .
The cast of IDENTITY_FUNCTION to
(UnaryFunction<T>)
generates an unchecked cast warning, asUnaryFunction<Object>
is not aUnaryFunction<T>
for everyT
. But the identity function is special: it returns its argument unmodified, so we know that it is typesafe to use it as aUnaryFunction<T>
whatever the value ofT
. Therefore, we can confidently suppress the unchecked cast warning that is generated by this cast. Once we’ve done this, the code compiles without error or warning.