1/*
2 * Copyright (C) 2007 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.primitives;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import java.util.Collections;
22import java.util.HashMap;
23import java.util.Map;
24import java.util.Set;
25
26/**
27 * Contains static utility methods pertaining to primitive types and their
28 * corresponding wrapper types.
29 *
30 * @author Kevin Bourrillion
31 * @since 2009.09.15 <b>tentative</b>
32 */
33public final class Primitives {
34  private Primitives() {}
35
36  /** A map from primitive types to their corresponding wrapper types. */
37  public static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE;
38
39  /** A map from wrapper types to their corresponding primitive types. */
40  public static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPE;
41
42  // Sad that we can't use a BiMap. :(
43
44  static {
45    Map<Class<?>, Class<?>> primToWrap = new HashMap<Class<?>, Class<?>>(16);
46    Map<Class<?>, Class<?>> wrapToPrim = new HashMap<Class<?>, Class<?>>(16);
47
48    add(primToWrap, wrapToPrim, boolean.class, Boolean.class);
49    add(primToWrap, wrapToPrim, byte.class, Byte.class);
50    add(primToWrap, wrapToPrim, char.class, Character.class);
51    add(primToWrap, wrapToPrim, double.class, Double.class);
52    add(primToWrap, wrapToPrim, float.class, Float.class);
53    add(primToWrap, wrapToPrim, int.class, Integer.class);
54    add(primToWrap, wrapToPrim, long.class, Long.class);
55    add(primToWrap, wrapToPrim, short.class, Short.class);
56    add(primToWrap, wrapToPrim, void.class, Void.class);
57
58    PRIMITIVE_TO_WRAPPER_TYPE = Collections.unmodifiableMap(primToWrap);
59    WRAPPER_TO_PRIMITIVE_TYPE = Collections.unmodifiableMap(wrapToPrim);
60  }
61
62  private static void add(Map<Class<?>, Class<?>> forward,
63      Map<Class<?>, Class<?>> backward, Class<?> key, Class<?> value) {
64    forward.put(key, value);
65    backward.put(value, key);
66  }
67
68  /** All nine primitive types (including void). */
69  public static final Set<Class<?>> PRIMITIVE_TYPES
70      = PRIMITIVE_TO_WRAPPER_TYPE.keySet();
71
72  /** All nine wrapper types (including Void). */
73  public static final Set<Class<?>> WRAPPER_TYPES
74      = WRAPPER_TO_PRIMITIVE_TYPE.keySet();
75
76  /**
77   * Returns {@code true} if {@code type} is one of the nine
78   * primitive-wrapper types, such as {@link Integer}.
79   *
80   * @see Class#isPrimitive
81   */
82  public static boolean isWrapperType(Class<?> type) {
83    return WRAPPER_TO_PRIMITIVE_TYPE.containsKey(checkNotNull(type));
84  }
85
86  /**
87   * Returns the corresponding wrapper type of {@code type} if it is a primitive
88   * type; otherwise returns {@code type} itself. Idempotent.
89   * <pre>
90   *     wrap(int.class) == Integer.class
91   *     wrap(Integer.class) == Integer.class
92   *     wrap(String.class) == String.class
93   * </pre>
94   */
95  public static <T> Class<T> wrap(Class<T> type) {
96    checkNotNull(type);
97
98    // cast is safe: long.class and Long.class are both of type Class<Long>
99    @SuppressWarnings("unchecked")
100    Class<T> wrapped = (Class<T>) PRIMITIVE_TO_WRAPPER_TYPE.get(type);
101    return (wrapped == null) ? type : wrapped;
102  }
103
104  /**
105   * Returns the corresponding primitive type of {@code type} if it is a
106   * wrapper type; otherwise returns {@code type} itself. Idempotent.
107   * <pre>
108   *     unwrap(Integer.class) == int.class
109   *     unwrap(int.class) == int.class
110   *     unwrap(String.class) == String.class
111   * </pre>
112   */
113  public static <T> Class<T> unwrap(Class<T> type) {
114    checkNotNull(type);
115
116    // cast is safe: long.class and Long.class are both of type Class<Long>
117    @SuppressWarnings("unchecked")
118    Class<T> unwrapped = (Class<T>) WRAPPER_TO_PRIMITIVE_TYPE.get(type);
119    return (unwrapped == null) ? type : unwrapped;
120  }
121}
122