Built-in (or popular 3rd party library) way to map between primitive and corresponding reference type ("wrapper")?
I'm looking for a bidirectional map that, for example, produces java.lang.Boolean.class
when a lookup is done on java.lang.Boolean.TYPE
, and visa versa. Basically, a primitive ⇆ wrapper map.
It seems easy enough to construct such a map, but I'm wondering if one is already available in some popular, common place (this example uses Google Guava):
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
public static final BiMap<Class<?>, Class<?>> PRIMITIVE_TO_OBJECT = ImmutableBiMap.<Class<?>, Class<?>>builder()
.put( Boolean.TYPE, Boolean.class)
.put( Byte.TYPE, Byte.class)
.put(Character.TYPE, Character.class)
.put( Double.TYPE, Double.class)
.put( Float.TYPE, Float.class)
.put( Integer.TYPE, Integer.class)
.put开发者_如何学Python( Long.TYPE, Long.class)
.put( Short.TYPE, Short.class)
.build();
org.apache.commons.lang3.ClassUtils in the Apache Commons Lang project:
static Class<?> primitiveToWrapper(Class<?> cls)
Converts the specified primitive Class object
to its corresponding wrapper Class object.
static Class<?> wrapperToPrimitive(Class<?> cls)
Converts the specified wrapper class to its
corresponding primitive class.
精彩评论