2 * Copyright (C) 2008 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com
.google
.gson
.internal
;
20 import java
.lang
.reflect
.Type
;
21 import java
.util
.Collections
;
22 import java
.util
.HashMap
;
26 * Contains static utility methods pertaining to primitive types and their
27 * corresponding wrapper types.
29 * @author Kevin Bourrillion
31 public final class Primitives
{
32 private Primitives() {}
34 /** A map from primitive types to their corresponding wrapper types. */
35 private static final Map
<Class
<?
>, Class
<?
>> PRIMITIVE_TO_WRAPPER_TYPE
;
37 /** A map from wrapper types to their corresponding primitive types. */
38 private static final Map
<Class
<?
>, Class
<?
>> WRAPPER_TO_PRIMITIVE_TYPE
;
40 // Sad that we can't use a BiMap. :(
43 Map
<Class
<?
>, Class
<?
>> primToWrap
= new HashMap
<Class
<?
>, Class
<?
>>(16);
44 Map
<Class
<?
>, Class
<?
>> wrapToPrim
= new HashMap
<Class
<?
>, Class
<?
>>(16);
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);
56 PRIMITIVE_TO_WRAPPER_TYPE
= Collections
.unmodifiableMap(primToWrap
);
57 WRAPPER_TO_PRIMITIVE_TYPE
= Collections
.unmodifiableMap(wrapToPrim
);
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
);
67 * Returns true if this type is a primitive.
69 public static boolean isPrimitive(Type type
) {
70 return PRIMITIVE_TO_WRAPPER_TYPE
.containsKey(type
);
74 * Returns {@code true} if {@code type} is one of the nine
75 * primitive-wrapper types, such as {@link Integer}.
77 * @see Class#isPrimitive
79 public static boolean isWrapperType(Type type
) {
80 return WRAPPER_TO_PRIMITIVE_TYPE
.containsKey(
81 $Gson$Preconditions
.checkNotNull(type
));
85 * Returns the corresponding wrapper type of {@code type} if it is a primitive
86 * type; otherwise returns {@code type} itself. Idempotent.
88 * wrap(int.class) == Integer.class
89 * wrap(Integer.class) == Integer.class
90 * wrap(String.class) == String.class
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
;
102 * Returns the corresponding primitive type of {@code type} if it is a
103 * wrapper type; otherwise returns {@code type} itself. Idempotent.
105 * unwrap(Integer.class) == int.class
106 * unwrap(int.class) == int.class
107 * unwrap(String.class) == String.class
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
;