public static <T> T checkType(Object ref, Class<T> type)
	{
		if (type.isInstance(ref)) {
			return type.cast(ref);
		}
		throw new ClassCastException();
	}
public static < T > T checkType ( Object ref , Class < T > type ) { if ( type . isInstance ( ref ) ) { return type . cast ( ref ) ; } throw new ClassCastException ( ) ; }
Checks the type of the given reference and, in case of success, casts
and returns it.

@param <T> the expected type of the given reference.
@param ref the reference to test.
@param type the expected type of the given reference.

@return the given reference in the expected type.

@throws NullPointerException if {@code type} is {@code null}.
@throws ClassCastException if the given reference doesn't represent
an instance of the expected class.