Contains static utility methods pertaining to primitive types and their corresponding wrapper types. @author Kevin Bourrillion @since 1.0
| 31 | * @since 1.0 |
| 32 | */ |
| 33 | @GwtIncompatible |
| 34 | public final class Primitives { |
| 35 | private Primitives() {} |
| 36 | |
| 37 | /** A map from primitive types to their corresponding wrapper types. */ |
| 38 | private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE; |
| 39 | |
| 40 | /** A map from wrapper types to their corresponding primitive types. */ |
| 41 | private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPE; |
| 42 | |
| 43 | // Sad that we can't use a BiMap. :( |
| 44 | |
| 45 | static { |
| 46 | Map<Class<?>, Class<?>> primToWrap = new HashMap<Class<?>, Class<?>>(16); |
| 47 | Map<Class<?>, Class<?>> wrapToPrim = new HashMap<Class<?>, Class<?>>(16); |
| 48 | |
| 49 | add(primToWrap, wrapToPrim, boolean.class, Boolean.class); |
| 50 | add(primToWrap, wrapToPrim, byte.class, Byte.class); |
| 51 | add(primToWrap, wrapToPrim, char.class, Character.class); |
| 52 | add(primToWrap, wrapToPrim, double.class, Double.class); |
| 53 | add(primToWrap, wrapToPrim, float.class, Float.class); |
| 54 | add(primToWrap, wrapToPrim, int.class, Integer.class); |
| 55 | add(primToWrap, wrapToPrim, long.class, Long.class); |
| 56 | add(primToWrap, wrapToPrim, short.class, Short.class); |
| 57 | add(primToWrap, wrapToPrim, void.class, Void.class); |
| 58 | |
| 59 | PRIMITIVE_TO_WRAPPER_TYPE = Collections.unmodifiableMap(primToWrap); |
| 60 | WRAPPER_TO_PRIMITIVE_TYPE = Collections.unmodifiableMap(wrapToPrim); |
| 61 | } |
| 62 | |
| 63 | private static void add( |
| 64 | Map<Class<?>, Class<?>> forward, |
| 65 | Map<Class<?>, Class<?>> backward, |
| 66 | Class<?> key, |
| 67 | Class<?> value) { |
| 68 | forward.put(key, value); |
| 69 | backward.put(value, key); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns an immutable set of all nine primitive types (including {@code |
| 74 | * void}). Note that a simpler way to test whether a {@code Class} instance is a member of this |
| 75 | * set is to call {@link Class#isPrimitive}. |
| 76 | * |
| 77 | * @since 3.0 |
| 78 | */ |
| 79 | public static Set<Class<?>> allPrimitiveTypes() { |
| 80 | return PRIMITIVE_TO_WRAPPER_TYPE.keySet(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns an immutable set of all nine primitive-wrapper types (including {@link Void}). |
| 85 | * |
| 86 | * @since 3.0 |
| 87 | */ |
| 88 | public static Set<Class<?>> allWrapperTypes() { |
| 89 | return WRAPPER_TO_PRIMITIVE_TYPE.keySet(); |
| 90 | } |
nothing calls this directly
no test coverage detected