> WRAPPER_TYPES
= WRAPPER_TO_PRIMITIVE_TYPE.keySet();
/**
* Returns {@code true} if {@code type} is one of the nine
* primitive-wrapper types, such as {@link Integer}.
*
* @see Class#isPrimitive
*/
public static boolean isWrapperType(Class> type) {
return WRAPPER_TO_PRIMITIVE_TYPE.containsKey(checkNotNull(type));
}
/**
* Returns the corresponding wrapper type of {@code type} if it is a primitive
* type; otherwise returns {@code type} itself. Idempotent.
*
* wrap(int.class) == Integer.class
* wrap(Integer.class) == Integer.class
* wrap(String.class) == String.class
*
*/
public static Class wrap(Class type) {
checkNotNull(type);
// cast is safe: long.class and Long.class are both of type Class
@SuppressWarnings("unchecked")
Class wrapped = (Class) PRIMITIVE_TO_WRAPPER_TYPE.get(type);
return (wrapped == null) ? type : wrapped;
}
/**
* Returns the corresponding primitive type of {@code type} if it is a
* wrapper type; otherwise returns {@code type} itself. Idempotent.
*
* unwrap(Integer.class) == int.class
* unwrap(int.class) == int.class
* unwrap(String.class) == String.class
*
*/
public static Class unwrap(Class type) {
checkNotNull(type);
// cast is safe: long.class and Long.class are both of type Class
@SuppressWarnings("unchecked")
Class unwrapped = (Class) WRAPPER_TO_PRIMITIVE_TYPE.get(type);
return (unwrapped == null) ? type : unwrapped;
}
}