001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
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     * http://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    
017    package org.jetbrains.kotlin.resolve.jvm;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.kotlin.builtins.PrimitiveType;
021    import org.jetbrains.kotlin.name.FqName;
022    
023    import java.util.*;
024    
025    public enum JvmPrimitiveType {
026        BOOLEAN(PrimitiveType.BOOLEAN, "boolean", "Z", "java.lang.Boolean"),
027        CHAR(PrimitiveType.CHAR, "char", "C", "java.lang.Character"),
028        BYTE(PrimitiveType.BYTE, "byte", "B", "java.lang.Byte"),
029        SHORT(PrimitiveType.SHORT, "short", "S", "java.lang.Short"),
030        INT(PrimitiveType.INT, "int", "I", "java.lang.Integer"),
031        FLOAT(PrimitiveType.FLOAT, "float", "F", "java.lang.Float"),
032        LONG(PrimitiveType.LONG, "long", "J", "java.lang.Long"),
033        DOUBLE(PrimitiveType.DOUBLE, "double", "D", "java.lang.Double"),
034        ;
035    
036        private static final Set<FqName> WRAPPERS_CLASS_NAMES;
037        private static final Map<String, JvmPrimitiveType> TYPE_BY_NAME;
038        private static final Map<PrimitiveType, JvmPrimitiveType> TYPE_BY_PRIMITIVE_TYPE;
039    
040        static {
041            WRAPPERS_CLASS_NAMES = new HashSet<FqName>();
042            TYPE_BY_NAME = new HashMap<String, JvmPrimitiveType>();
043            TYPE_BY_PRIMITIVE_TYPE = new EnumMap<PrimitiveType, JvmPrimitiveType>(PrimitiveType.class);
044    
045            for (JvmPrimitiveType type : values()) {
046                WRAPPERS_CLASS_NAMES.add(type.getWrapperFqName());
047                TYPE_BY_NAME.put(type.getName(), type);
048                TYPE_BY_PRIMITIVE_TYPE.put(type.getPrimitiveType(), type);
049            }
050        }
051    
052        public static boolean isWrapperClassName(@NotNull FqName className) {
053            return WRAPPERS_CLASS_NAMES.contains(className);
054        }
055    
056        @NotNull
057        public static JvmPrimitiveType get(@NotNull String name) {
058            JvmPrimitiveType result = TYPE_BY_NAME.get(name);
059            if (result == null) {
060                throw new AssertionError("Non-primitive type name passed: " + name);
061            }
062            return result;
063        }
064    
065        @NotNull
066        public static JvmPrimitiveType get(@NotNull PrimitiveType type) {
067            return TYPE_BY_PRIMITIVE_TYPE.get(type);
068        }
069    
070        private final PrimitiveType primitiveType;
071        private final String name;
072        private final String desc;
073        private final FqName wrapperFqName;
074    
075        JvmPrimitiveType(@NotNull PrimitiveType primitiveType, @NotNull String name, @NotNull String desc, @NotNull String wrapperClassName) {
076            this.primitiveType = primitiveType;
077            this.name = name;
078            this.desc = desc;
079            this.wrapperFqName = new FqName(wrapperClassName);
080        }
081    
082        @NotNull
083        public PrimitiveType getPrimitiveType() {
084            return primitiveType;
085        }
086    
087        @NotNull
088        public String getName() {
089            return name;
090        }
091    
092        @NotNull
093        public String getDesc() {
094            return desc;
095        }
096    
097        @NotNull
098        public FqName getWrapperFqName() {
099            return wrapperFqName;
100        }
101    }