b797139b0dcafbfd0bd91164adca7aa4149fe9b6
[unical.git] / gson / com / google / gson / internal / Primitives.java
1 /*
2 * Copyright (C) 2008 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
17 package com.google.gson.internal;
18
19
20 import java.lang.reflect.Type;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 /**
26 * Contains static utility methods pertaining to primitive types and their
27 * corresponding wrapper types.
28 *
29 * @author Kevin Bourrillion
30 */
31 public final class Primitives {
32 private Primitives() {}
33
34 /** A map from primitive types to their corresponding wrapper types. */
35 private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE;
36
37 /** A map from wrapper types to their corresponding primitive types. */
38 private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPE;
39
40 // Sad that we can't use a BiMap. :(
41
42 static {
43 Map<Class<?>, Class<?>> primToWrap = new HashMap<Class<?>, Class<?>>(16);
44 Map<Class<?>, Class<?>> wrapToPrim = new HashMap<Class<?>, Class<?>>(16);
45
46 add(primToWrap, wrapToPrim, boolean.class, Boolean.class);
47 add(primToWrap, wrapToPrim, byte.class, Byte.class);
48 add(primToWrap, wrapToPrim, char.class, Character.class);
49 add(primToWrap, wrapToPrim, double.class, Double.class);
50 add(primToWrap, wrapToPrim, float.class, Float.class);
51 add(primToWrap, wrapToPrim, int.class, Integer.class);
52 add(primToWrap, wrapToPrim, long.class, Long.class);
53 add(primToWrap, wrapToPrim, short.class, Short.class);
54 add(primToWrap, wrapToPrim, void.class, Void.class);
55
56 PRIMITIVE_TO_WRAPPER_TYPE = Collections.unmodifiableMap(primToWrap);
57 WRAPPER_TO_PRIMITIVE_TYPE = Collections.unmodifiableMap(wrapToPrim);
58 }
59
60 private static void add(Map<Class<?>, Class<?>> forward,
61 Map<Class<?>, Class<?>> backward, Class<?> key, Class<?> value) {
62 forward.put(key, value);
63 backward.put(value, key);
64 }
65
66 /**
67 * Returns true if this type is a primitive.
68 */
69 public static boolean isPrimitive(Type type) {
70 return PRIMITIVE_TO_WRAPPER_TYPE.containsKey(type);
71 }
72
73 /**
74 * Returns {@code true} if {@code type} is one of the nine
75 * primitive-wrapper types, such as {@link Integer}.
76 *
77 * @see Class#isPrimitive
78 */
79 public static boolean isWrapperType(Type type) {
80 return WRAPPER_TO_PRIMITIVE_TYPE.containsKey(
81 $Gson$Preconditions.checkNotNull(type));
82 }
83
84 /**
85 * Returns the corresponding wrapper type of {@code type} if it is a primitive
86 * type; otherwise returns {@code type} itself. Idempotent.
87 * <pre>
88 * wrap(int.class) == Integer.class
89 * wrap(Integer.class) == Integer.class
90 * wrap(String.class) == String.class
91 * </pre>
92 */
93 public static <T> Class<T> wrap(Class<T> type) {
94 // cast is safe: long.class and Long.class are both of type Class<Long>
95 @SuppressWarnings("unchecked")
96 Class<T> wrapped = (Class<T>) PRIMITIVE_TO_WRAPPER_TYPE.get(
97 $Gson$Preconditions.checkNotNull(type));
98 return (wrapped == null) ? type : wrapped;
99 }
100
101 /**
102 * Returns the corresponding primitive type of {@code type} if it is a
103 * wrapper type; otherwise returns {@code type} itself. Idempotent.
104 * <pre>
105 * unwrap(Integer.class) == int.class
106 * unwrap(int.class) == int.class
107 * unwrap(String.class) == String.class
108 * </pre>
109 */
110 public static <T> Class<T> unwrap(Class<T> type) {
111 // cast is safe: long.class and Long.class are both of type Class<Long>
112 @SuppressWarnings("unchecked")
113 Class<T> unwrapped = (Class<T>) WRAPPER_TO_PRIMITIVE_TYPE.get(
114 $Gson$Preconditions.checkNotNull(type));
115 return (unwrapped == null) ? type : unwrapped;
116 }
117 }
This page took 0.023467 seconds and 3 git commands to generate.