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