View Javadoc

1   /*
2    * Copyright (C) 2006  Tom Gibara
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  package com.tomgibara.pronto.util;
19  
20  /**
21   * Collects together utility methods for handling class objects.
22   * 
23   * @author Tom Gibara
24   */
25  
26  public final class Classes {
27  
28      /**
29       * Identifies the object class which corresponds to a specified primitive
30       * class, or throws an exception.
31       * 
32       * @param clss
33       *            the class
34       * @return the object class for the supplied primitive class, never null
35       * @throws IllegalArgumentException
36       *             if the supplied class is null or does not represent a
37       *             primitive type
38       */
39  
40      public static Class classForPrimitive(final Class clss) throws IllegalArgumentException {
41          if (clss == Boolean.TYPE) return Boolean.class;
42          if (clss == Integer.TYPE) return Integer.class;
43          if (clss == Double.TYPE) return Double.class;
44          if (clss == Long.TYPE) return Long.class;
45          if (clss == Float.TYPE) return Float.class;
46          if (clss == Short.TYPE) return Short.class;
47          if (clss == Byte.TYPE) return Byte.class;
48          if (clss == Character.TYPE) return Character.class;
49          if (clss == Void.TYPE) return Void.class;
50          throw new IllegalArgumentException(String.format("Class %s is not primitive.", clss.getName()));
51      }
52  
53      /**
54       * Whether the class is strictly primitive - not including the Void.TYPE.
55       * 
56       * @param clss
57       *            the class
58       * @return true if and only if the class is primitive and not void
59       */
60  
61      public static boolean isNonVoidPrimitive(final Class clss) {
62          return clss.isPrimitive() && clss != Void.TYPE;
63      }
64  
65      /**
66       * Whether the supplied class represents a number primitive.
67       * 
68       * @param clss
69       *            the class
70       * @return true if and only if clss equals the TYPE of some extension of
71       *         Number
72       */
73  
74      public static boolean isNumberPrimitive(final Class clss) {
75          // ordered by expected frequency
76          return clss == Integer.TYPE || clss == Double.TYPE || clss == Long.TYPE || clss == Float.TYPE
77                  || clss == Short.TYPE || clss == Byte.TYPE;
78      }
79  
80      /**
81       * Whether the supplied class represents the boolean primitive.
82       * 
83       * @param clss
84       *            the class
85       * @return true if and only if clss == Boolean.TYPE
86       */
87  
88      public static boolean isBooleanPrimitive(final Class clss) {
89          return clss == Boolean.TYPE;
90      }
91  
92      private Classes() { }
93  
94  }