View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.jsr356.utils;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Set;
25  
26  public class Primitives
27  {
28      private static final Map<Class<?>, Class<?>> PRIMITIVE_CLASS_MAP;
29      private static final Map<Class<?>, Class<?>> CLASS_PRIMITIVE_MAP;
30  
31      static
32      {
33          Map<Class<?>, Class<?>> primitives = new HashMap<>();
34  
35          // Map of classes to primitive types
36          primitives.put(Boolean.class,Boolean.TYPE);
37          primitives.put(Byte.class,Byte.TYPE);
38          primitives.put(Character.class,Character.TYPE);
39          primitives.put(Double.class,Double.TYPE);
40          primitives.put(Float.class,Float.TYPE);
41          primitives.put(Integer.class,Integer.TYPE);
42          primitives.put(Long.class,Long.TYPE);
43          primitives.put(Short.class,Short.TYPE);
44          primitives.put(Void.class,Void.TYPE);
45  
46          CLASS_PRIMITIVE_MAP = Collections.unmodifiableMap(primitives);
47  
48          // Map of primitive types to classes
49          Map<Class<?>, Class<?>> types = new HashMap<>();
50          for (Map.Entry<Class<?>, Class<?>> classEntry : primitives.entrySet())
51          {
52              types.put(classEntry.getValue(),classEntry.getKey());
53          }
54  
55          PRIMITIVE_CLASS_MAP = Collections.unmodifiableMap(types);
56      }
57  
58      public static Class<?> getPrimitiveClass(Class<?> primitiveType)
59      {
60          return PRIMITIVE_CLASS_MAP.get(primitiveType);
61      }
62  
63      public static Set<Class<?>> getPrimitiveClasses()
64      {
65          return CLASS_PRIMITIVE_MAP.keySet();
66      }
67  
68      public static Set<Class<?>> getPrimitives()
69      {
70          return PRIMITIVE_CLASS_MAP.keySet();
71      }
72  
73      public static Class<?> getPrimitiveType(Class<?> primitiveClass)
74      {
75          return CLASS_PRIMITIVE_MAP.get(primitiveClass);
76      }
77  }