001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.expression.spel.ast;
018
019/**
020 * Captures primitive types and their corresponding class objects, plus one special entry
021 * that represents all reference (non-primitive) types.
022 *
023 * @author Andy Clement
024 */
025public enum TypeCode {
026
027        OBJECT(Object.class),
028
029        BOOLEAN(Boolean.TYPE),
030
031        BYTE(Byte.TYPE),
032
033        CHAR(Character.TYPE),
034
035        DOUBLE(Double.TYPE),
036
037        FLOAT(Float.TYPE),
038
039        INT(Integer.TYPE),
040
041        LONG(Long.TYPE),
042
043        SHORT(Short.TYPE);
044
045
046        private Class<?> type;
047
048
049        TypeCode(Class<?> type) {
050                this.type = type;
051        }
052
053
054        public Class<?> getType() {
055                return this.type;
056        }
057
058
059        public static TypeCode forName(String name) {
060                String searchingFor = name.toUpperCase();
061                TypeCode[] tcs = values();
062                for (int i = 1; i < tcs.length; i++) {
063                        if (tcs[i].name().equals(searchingFor)) {
064                                return tcs[i];
065                        }
066                }
067                return OBJECT;
068        }
069
070        public static TypeCode forClass(Class<?> clazz) {
071                TypeCode[] allValues = TypeCode.values();
072                for (TypeCode typeCode : allValues) {
073                        if (clazz == typeCode.getType()) {
074                                return typeCode;
075                        }
076                }
077                return OBJECT;
078        }
079
080}