001 /*--------------------------------------------------------------------------+ 002 $Id: EJavaPrimitive.java 26283 2010-02-18 11:18:57Z juergens $ 003 | | 004 | Copyright 2005-2010 Technische Universitaet Muenchen | 005 | | 006 | Licensed under the Apache License, Version 2.0 (the "License"); | 007 | you may not use this file except in compliance with the License. | 008 | You may obtain a copy of the License at | 009 | | 010 | http://www.apache.org/licenses/LICENSE-2.0 | 011 | | 012 | Unless required by applicable law or agreed to in writing, software | 013 | distributed under the License is distributed on an "AS IS" BASIS, | 014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 015 | See the License for the specific language governing permissions and | 016 | limitations under the License. | 017 +--------------------------------------------------------------------------*/ 018 package edu.tum.cs.commons.reflect; 019 020 import edu.tum.cs.commons.assertion.CCSMPre; 021 import edu.tum.cs.commons.enums.EnumUtils; 022 023 /** 024 * Enumeration of Java primitives. 025 * 026 * @author deissenb 027 * @author $Author: juergens $ 028 * @version $Rev: 26283 $ 029 * @levd.rating GREEN Hash: 7E5359C46ADC381B3A4FEC22DC494CBC 030 */ 031 public enum EJavaPrimitive { 032 033 /** void */ 034 VOID(void.class, Void.class), 035 036 /** byte */ 037 BYTE(byte.class, Byte.class), 038 039 /** char */ 040 CHAR(char.class, Character.class), 041 042 /** double */ 043 DOUBLE(double.class, Double.class), 044 045 /** float */ 046 FLOAT(float.class, Float.class), 047 048 /** int */ 049 INT(int.class, Integer.class), 050 051 /** long */ 052 LONG(long.class, Long.class), 053 054 /** short */ 055 SHORT(short.class, Short.class), 056 057 /** boolean */ 058 BOOLEAN(boolean.class, Boolean.class); 059 060 /** Class object of the primitive. */ 061 private final Class<?> primitiveClass; 062 063 /** Class object of the wrapper type primitive. */ 064 private final Class<?> wrapperClass; 065 066 /** Create new primitive. */ 067 private EJavaPrimitive(Class<?> primitiveClass, Class<?> wrapperClass) { 068 CCSMPre.isTrue(primitiveClass.isPrimitive(), 069 "Clazz object must be a primitive."); 070 this.primitiveClass = primitiveClass; 071 this.wrapperClass = wrapperClass; 072 } 073 074 /** Get the class object of the primitive. */ 075 public Class<?> getClassObject() { 076 return primitiveClass; 077 } 078 079 /** Returns the wrapper class for the primitive. */ 080 public Class<?> getWrapperClass() { 081 return wrapperClass; 082 } 083 084 /** 085 * Get primitive by name. 086 * 087 * @return primitive or <code>null</code> if unknown primitive was 088 * requested 089 */ 090 public static EJavaPrimitive getPrimitive(String name) { 091 return EnumUtils.valueOf(EJavaPrimitive.class, name); 092 } 093 094 /** 095 * Get primitive by name ignoring case. 096 * 097 * @return primitive or <code>null</code> if unknown primitive was 098 * requested 099 */ 100 public static EJavaPrimitive getPrimitiveIgnoreCase(String name) { 101 return EnumUtils.valueOfIgnoreCase(EJavaPrimitive.class, name); 102 } 103 104 /** 105 * Returns the enum literal belonging to the given primitive class (or 106 * null). 107 */ 108 public static EJavaPrimitive getForPrimitiveClass(Class<?> clazz) { 109 for (EJavaPrimitive javaPrimitive : values()) { 110 if (javaPrimitive.primitiveClass.equals(clazz)) { 111 return javaPrimitive; 112 } 113 } 114 return null; 115 } 116 117 /** Returns the enum literal belonging to the given wrapper class (or null). */ 118 public static EJavaPrimitive getForWrapperClass(Class<?> clazz) { 119 for (EJavaPrimitive javaPrimitive : values()) { 120 if (javaPrimitive.wrapperClass.equals(clazz)) { 121 return javaPrimitive; 122 } 123 } 124 return null; 125 } 126 127 /** 128 * Returns the enum literal belonging to the given primitive or wrapper 129 * class (or null). 130 */ 131 public static EJavaPrimitive getForPrimitiveOrWrapperClass(Class<?> clazz) { 132 for (EJavaPrimitive javaPrimitive : values()) { 133 if (javaPrimitive.primitiveClass.equals(clazz) 134 || javaPrimitive.wrapperClass.equals(clazz)) { 135 return javaPrimitive; 136 } 137 } 138 return null; 139 } 140 141 /** Returns whether the given class is a wrapper type for a primitive. */ 142 public static boolean isWrapperType(Class<?> clazz) { 143 return getForWrapperClass(clazz) != null; 144 } 145 }