001    /*
002     * Copyright 2010-2014 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.jet.codegen.when;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.jet.lang.resolve.constants.EnumValue;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    public class WhenByEnumsMapping {
026        private static final String MAPPING_ARRAY_FIELD_PREFIX = "$EnumSwitchMapping$";
027        private static final String MAPPINGS_CLASS_NAME_POSTFIX = "$WhenMappings";
028    
029        private final Map<EnumValue, Integer> map = new HashMap<EnumValue, Integer>();
030        private final String enumClassInternalName;
031        private final String outerClassInternalNameForExpression;
032        private final String mappingsClassInternalName;
033        private final int fieldNumber;
034    
035        public WhenByEnumsMapping(String enumClassInternalName, String outerClassInternalNameForExpression, int fieldNumber) {
036            this.enumClassInternalName = enumClassInternalName;
037            this.outerClassInternalNameForExpression = outerClassInternalNameForExpression;
038            this.mappingsClassInternalName = outerClassInternalNameForExpression + MAPPINGS_CLASS_NAME_POSTFIX;
039            this.fieldNumber = fieldNumber;
040        }
041    
042        public int getIndexByEntry(@NotNull EnumValue value) {
043            Integer result = map.get(value);
044            assert result != null : "entry " + value + " has no mapping";
045            return result;
046        }
047    
048        public void putFirstTime(EnumValue value, int index) {
049            if (!map.containsKey(value)) {
050                map.put(value, index);
051            }
052        }
053    
054        public int size() {
055            return map.size();
056        }
057    
058        public String getFieldName() {
059            return MAPPING_ARRAY_FIELD_PREFIX + fieldNumber;
060        }
061    
062        public String getEnumClassInternalName() {
063            return enumClassInternalName;
064        }
065    
066        public String getOuterClassInternalNameForExpression() {
067            return outerClassInternalNameForExpression;
068        }
069    
070        public String getMappingsClassInternalName() {
071            return mappingsClassInternalName;
072        }
073    
074        public Iterable<Map.Entry<EnumValue, Integer>> enumValuesToIntMapping() {
075            return map.entrySet();
076        }
077    }