Coverage Summary for Class: Primitives (com.google.common.primitives)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Primitives | 100% (1/1) | 50% (4/8) | 85.2% (23/27) |
1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.primitives; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 19 import com.google.common.annotations.GwtIncompatible; 20 import java.util.Collections; 21 import java.util.LinkedHashMap; 22 import java.util.Map; 23 import java.util.Set; 24 25 /** 26 * Contains static utility methods pertaining to primitive types and their corresponding wrapper 27 * types. 28 * 29 * @author Kevin Bourrillion 30 * @since 1.0 31 */ 32 @GwtIncompatible 33 @ElementTypesAreNonnullByDefault 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 LinkedHashMap<>(16); 47 Map<Class<?>, Class<?>> wrapToPrim = new LinkedHashMap<>(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 void}). Note that a 74 * simpler way to test whether a {@code Class} instance is a member of this set is to call {@link 75 * 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 } 91 92 /** 93 * Returns {@code true} if {@code type} is one of the nine primitive-wrapper types, such as {@link 94 * Integer}. 95 * 96 * @see Class#isPrimitive 97 */ 98 public static boolean isWrapperType(Class<?> type) { 99 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 }