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