1/* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package jdk.jshell; 27 28import java.util.LinkedHashMap; 29import java.util.Map; 30import java.util.stream.Stream; 31import jdk.jshell.Key.TypeDeclKey; 32import jdk.jshell.Key.ErroneousKey; 33import jdk.jshell.Key.ExpressionKey; 34import jdk.jshell.Key.ImportKey; 35import jdk.jshell.Key.MethodKey; 36import jdk.jshell.Key.StatementKey; 37import jdk.jshell.Key.VarKey; 38import jdk.jshell.Snippet.SubKind; 39import static jdk.jshell.Snippet.SubKind.SINGLE_STATIC_IMPORT_SUBKIND; 40import static jdk.jshell.Snippet.SubKind.SINGLE_TYPE_IMPORT_SUBKIND; 41 42/** 43 * Maps signature to Key by Kind. 44 * @author Robert Field 45 */ 46class KeyMap { 47 private final JShell state; 48 49 KeyMap(JShell state) { 50 this.state = state; 51 } 52 53 private final Map<String, TypeDeclKey> classMap = new LinkedHashMap<>(); 54 private final Map<String, MethodKey> methodMap = new LinkedHashMap<>(); 55 private final Map<String, VarKey> varMap = new LinkedHashMap<>(); 56 private final Map<String, ImportKey> importMap = new LinkedHashMap<>(); 57 58 TypeDeclKey keyForClass(String name) { 59 return classMap.computeIfAbsent(name, k -> new TypeDeclKey(state, name)); 60 } 61 62 MethodKey keyForMethod(String name, String parameterTypes) { 63 return methodMap.computeIfAbsent(name + ":" + parameterTypes, 64 k -> new MethodKey(state, name, parameterTypes)); 65 } 66 67 VarKey keyForVariable(String name) { 68 return varMap.computeIfAbsent(name, k -> new VarKey(state, name)); 69 } 70 71 ImportKey keyForImport(String name, SubKind snippetKind) { 72 return importMap.computeIfAbsent(name + ":" 73 + ((snippetKind == SINGLE_TYPE_IMPORT_SUBKIND || snippetKind == SINGLE_STATIC_IMPORT_SUBKIND) 74 ? "single" 75 : snippetKind.toString()), 76 k -> new ImportKey(state, name, snippetKind)); 77 } 78 79 StatementKey keyForStatement() { 80 return new StatementKey(state); 81 } 82 83 ExpressionKey keyForExpression(String name, String type) { 84 return new ExpressionKey(state, name, type); 85 } 86 87 ErroneousKey keyForErroneous() { 88 return new ErroneousKey(state); 89 } 90 91 boolean doesVariableNameExist(String name) { 92 return varMap.get(name) != null; 93 } 94 95 Stream<ImportKey> importKeys() { 96 return importMap.values().stream(); 97 } 98} 99