View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
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 org.springmodules.validation.util;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /**
23   * <p>Primitive class utilities.</p>
24   * 
25   * <p><strong>Note</strong>: Derived from <code>org.springframework.util.ClassUtils</code> 
26   * and ideally would be added to this class.</p>
27   * 
28   * @author David Winterfeldt
29   */
30  public class PrimitiveClassUtils {
31  
32      /**
33       * Map with primitive type name as key and corresponding object
34       * type as value, for example: "int" -> "Integer.class".
35       */
36      private static final Map<Class<?>, Class<?>> primitiveToObjectMap = new HashMap<Class<?>, Class<?>>(16);
37  
38  
39      static {
40          primitiveToObjectMap.put(boolean.class, Boolean.class);
41          primitiveToObjectMap.put(byte.class, Byte.class);
42          primitiveToObjectMap.put(char.class, Character.class);
43          primitiveToObjectMap.put(double.class, Double.class);
44          primitiveToObjectMap.put(float.class, Float.class);
45          primitiveToObjectMap.put(int.class, Integer.class);
46          primitiveToObjectMap.put(long.class, Long.class);
47          primitiveToObjectMap.put(short.class, Short.class);
48      }
49  
50      /**
51       * Resolve the given primitive class name as primitive wrapper class, if appropriate,
52       * according to the JVM's naming rules for primitive classes.
53       *
54       * @param       name        The name of the potentially primitive class.
55       * @return      Class       The primitive wrapper class, or <code>null</code> if the name does not denote
56       * a primitive class or primitive array class
57       */
58      public static Class<?> resolvePrimitiveClassName(Class<?> primitiveClass) {
59          Class<?> result = null;
60          
61          if (primitiveToObjectMap.containsKey(primitiveClass)) {
62              result = (Class<?>) primitiveToObjectMap.get(primitiveClass);
63          }
64          
65          return result;
66      }
67      
68  }