001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.primitives;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import com.google.common.annotations.GwtIncompatible;
020import java.util.Collections;
021import java.util.LinkedHashMap;
022import java.util.Map;
023import java.util.Set;
024
025/**
026 * Contains static utility methods pertaining to primitive types and their corresponding wrapper
027 * types.
028 *
029 * @author Kevin Bourrillion
030 * @since 1.0
031 */
032@GwtIncompatible
033public final class Primitives {
034  private Primitives() {}
035
036  /** A map from primitive types to their corresponding wrapper types. */
037  private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE;
038
039  /** A map from wrapper types to their corresponding primitive types. */
040  private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPE;
041
042  // Sad that we can't use a BiMap. :(
043
044  static {
045    Map<Class<?>, Class<?>> primToWrap = new LinkedHashMap<>(16);
046    Map<Class<?>, Class<?>> wrapToPrim = new LinkedHashMap<>(16);
047
048    add(primToWrap, wrapToPrim, boolean.class, Boolean.class);
049    add(primToWrap, wrapToPrim, byte.class, Byte.class);
050    add(primToWrap, wrapToPrim, char.class, Character.class);
051    add(primToWrap, wrapToPrim, double.class, Double.class);
052    add(primToWrap, wrapToPrim, float.class, Float.class);
053    add(primToWrap, wrapToPrim, int.class, Integer.class);
054    add(primToWrap, wrapToPrim, long.class, Long.class);
055    add(primToWrap, wrapToPrim, short.class, Short.class);
056    add(primToWrap, wrapToPrim, void.class, Void.class);
057
058    PRIMITIVE_TO_WRAPPER_TYPE = Collections.unmodifiableMap(primToWrap);
059    WRAPPER_TO_PRIMITIVE_TYPE = Collections.unmodifiableMap(wrapToPrim);
060  }
061
062  private static void add(
063      Map<Class<?>, Class<?>> forward,
064      Map<Class<?>, Class<?>> backward,
065      Class<?> key,
066      Class<?> value) {
067    forward.put(key, value);
068    backward.put(value, key);
069  }
070
071  /**
072   * Returns an immutable set of all nine primitive types (including {@code void}). Note that a
073   * simpler way to test whether a {@code Class} instance is a member of this set is to call {@link
074   * Class#isPrimitive}.
075   *
076   * @since 3.0
077   */
078  public static Set<Class<?>> allPrimitiveTypes() {
079    return PRIMITIVE_TO_WRAPPER_TYPE.keySet();
080  }
081
082  /**
083   * Returns an immutable set of all nine primitive-wrapper types (including {@link Void}).
084   *
085   * @since 3.0
086   */
087  public static Set<Class<?>> allWrapperTypes() {
088    return WRAPPER_TO_PRIMITIVE_TYPE.keySet();
089  }
090
091  /**
092   * Returns {@code true} if {@code type} is one of the nine primitive-wrapper types, such as {@link
093   * Integer}.
094   *
095   * @see Class#isPrimitive
096   */
097  public static boolean isWrapperType(Class<?> type) {
098    return WRAPPER_TO_PRIMITIVE_TYPE.containsKey(checkNotNull(type));
099  }
100
101  /**
102   * Returns the corresponding wrapper type of {@code type} if it is a primitive type; otherwise
103   * returns {@code type} itself. Idempotent.
104   *
105   * <pre>
106   *     wrap(int.class) == Integer.class
107   *     wrap(Integer.class) == Integer.class
108   *     wrap(String.class) == String.class
109   * </pre>
110   */
111  public static <T> Class<T> wrap(Class<T> type) {
112    checkNotNull(type);
113
114    // cast is safe: long.class and Long.class are both of type Class<Long>
115    @SuppressWarnings("unchecked")
116    Class<T> wrapped = (Class<T>) PRIMITIVE_TO_WRAPPER_TYPE.get(type);
117    return (wrapped == null) ? type : wrapped;
118  }
119
120  /**
121   * Returns the corresponding primitive type of {@code type} if it is a wrapper type; otherwise
122   * returns {@code type} itself. Idempotent.
123   *
124   * <pre>
125   *     unwrap(Integer.class) == int.class
126   *     unwrap(int.class) == int.class
127   *     unwrap(String.class) == String.class
128   * </pre>
129   */
130  public static <T> Class<T> unwrap(Class<T> type) {
131    checkNotNull(type);
132
133    // cast is safe: long.class and Long.class are both of type Class<Long>
134    @SuppressWarnings("unchecked")
135    Class<T> unwrapped = (Class<T>) WRAPPER_TO_PRIMITIVE_TYPE.get(type);
136    return (unwrapped == null) ? type : unwrapped;
137  }
138}