001/*
002 * Copyright 2002-2019 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        /**
028         * An {@link Object}.
029         */
030        OBJECT(Object.class),
031
032        /**
033         * A {@code boolean}.
034         */
035        BOOLEAN(Boolean.TYPE),
036
037        /**
038         * A {@code byte}.
039         */
040        BYTE(Byte.TYPE),
041
042        /**
043         * A {@code char}.
044         */
045        CHAR(Character.TYPE),
046
047        /**
048         * A {@code double}.
049         */
050        DOUBLE(Double.TYPE),
051
052        /**
053         * A {@code float}.
054         */
055        FLOAT(Float.TYPE),
056
057        /**
058         * An {@code int}.
059         */
060        INT(Integer.TYPE),
061
062        /**
063         * A {@code long}.
064         */
065        LONG(Long.TYPE),
066
067        /**
068         * An {@link Object}.
069         */
070        SHORT(Short.TYPE);
071
072
073        private Class<?> type;
074
075
076        TypeCode(Class<?> type) {
077                this.type = type;
078        }
079
080
081        public Class<?> getType() {
082                return this.type;
083        }
084
085
086        public static TypeCode forName(String name) {
087                TypeCode[] tcs = values();
088                for (int i = 1; i < tcs.length; i++) {
089                        if (tcs[i].name().equalsIgnoreCase(name)) {
090                                return tcs[i];
091                        }
092                }
093                return OBJECT;
094        }
095
096        public static TypeCode forClass(Class<?> clazz) {
097                TypeCode[] allValues = TypeCode.values();
098                for (TypeCode typeCode : allValues) {
099                        if (clazz == typeCode.getType()) {
100                                return typeCode;
101                        }
102                }
103                return OBJECT;
104        }
105
106}