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
033@ElementTypesAreNonnullByDefault
034public final class Primitives {
035  private Primitives() {}
036
037  /** A map from primitive types to their corresponding wrapper types. */
038  private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE;
039
040  /** A map from wrapper types to their corresponding primitive types. */
041  private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPE;
042
043  // Sad that we can't use a BiMap. :(
044
045  static {
046    Map<Class<?>, Class<?>> primToWrap = new LinkedHashMap<>(16);
047    Map<Class<?>, Class<?>> wrapToPrim = new LinkedHashMap<>(16);
048
049    add(primToWrap, wrapToPrim, boolean.class, Boolean.class);
050    add(primToWrap, wrapToPrim, byte.class, Byte.class);
051    add(primToWrap, wrapToPrim, char.class, Character.class);
052    add(primToWrap, wrapToPrim, double.class, Double.class);
053    add(primToWrap, wrapToPrim, float.class, Float.class);
054    add(primToWrap, wrapToPrim, int.class, Integer.class);
055    add(primToWrap, wrapToPrim, long.class, Long.class);
056    add(primToWrap, wrapToPrim, short.class, Short.class);
057    add(primToWrap, wrapToPrim, void.class, Void.class);
058
059    PRIMITIVE_TO_WRAPPER_TYPE = Collections.unmodifiableMap(primToWrap);
060    WRAPPER_TO_PRIMITIVE_TYPE = Collections.unmodifiableMap(wrapToPrim);
061  }
062
063  private static void add(
064      Map<Class<?>, Class<?>> forward,
065      Map<Class<?>, Class<?>> backward,
066      Class<?> key,
067      Class<?> value) {
068    forward.put(key, value);
069    backward.put(value, key);
070  }
071
072  /**
073   * Returns an immutable set of all nine primitive types (including {@code void}). Note that a
074   * simpler way to test whether a {@code Class} instance is a member of this set is to call {@link
075   * Class#isPrimitive}.
076   *
077   * @since 3.0
078   */
079  public static Set<Class<?>> allPrimitiveTypes() {
080    return PRIMITIVE_TO_WRAPPER_TYPE.keySet();
081  }
082
083  /**
084   * Returns an immutable set of all nine primitive-wrapper types (including {@link Void}).
085   *
086   * @since 3.0
087   */
088  public static Set<Class<?>> allWrapperTypes() {
089    return WRAPPER_TO_PRIMITIVE_TYPE.keySet();
090  }
091
092  /**
093   * Returns {@code true} if {@code type} is one of the nine primitive-wrapper types, such as {@link
094   * Integer}.
095   *
096   * @see Class#isPrimitive
097   */
098  public static boolean isWrapperType(Class<?> type) {
099    return WRAPPER_TO_PRIMITIVE_TYPE.containsKey(checkNotNull(type));
100  }
101
102  /**
103   * Returns the corresponding wrapper type of {@code type} if it is a primitive type; otherwise
104   * returns {@code type} itself. Idempotent.
105   *
106   * <pre>
107   *     wrap(int.class) == Integer.class
108   *     wrap(Integer.class) == Integer.class
109   *     wrap(String.class) == String.class
110   * </pre>
111   */
112  public static <T> Class<T> wrap(Class<T> type) {
113    checkNotNull(type);
114
115    // cast is safe: long.class and Long.class are both of type Class<Long>
116    @SuppressWarnings("unchecked")
117    Class<T> wrapped = (Class<T>) PRIMITIVE_TO_WRAPPER_TYPE.get(type);
118    return (wrapped == null) ? type : wrapped;
119  }
120
121  /**
122   * Returns the corresponding primitive type of {@code type} if it is a wrapper type; otherwise
123   * returns {@code type} itself. Idempotent.
124   *
125   * <pre>
126   *     unwrap(Integer.class) == int.class
127   *     unwrap(int.class) == int.class
128   *     unwrap(String.class) == String.class
129   * </pre>
130   */
131  public static <T> Class<T> unwrap(Class<T> type) {
132    checkNotNull(type);
133
134    // cast is safe: long.class and Long.class are both of type Class<Long>
135    @SuppressWarnings("unchecked")
136    Class<T> unwrapped = (Class<T>) WRAPPER_TO_PRIMITIVE_TYPE.get(type);
137    return (unwrapped == null) ? type : unwrapped;
138  }
139}