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 
26 package jdk.jshell;
27 
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.stream.Stream;
31 import jdk.jshell.Key.TypeDeclKey;
32 import jdk.jshell.Key.ErroneousKey;
33 import jdk.jshell.Key.ExpressionKey;
34 import jdk.jshell.Key.ImportKey;
35 import jdk.jshell.Key.MethodKey;
36 import jdk.jshell.Key.StatementKey;
37 import jdk.jshell.Key.VarKey;
38 import jdk.jshell.Snippet.SubKind;
39 import static jdk.jshell.Snippet.SubKind.SINGLE_STATIC_IMPORT_SUBKIND;
40 import static jdk.jshell.Snippet.SubKind.SINGLE_TYPE_IMPORT_SUBKIND;
41 
42 /**
43  * Maps signature to Key by Kind.
44  * @author Robert Field
45  */
46 class KeyMap {
47     private final JShell state;
48 
KeyMap(JShell state)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 
keyForClass(String name)58     TypeDeclKey keyForClass(String name) {
59         return classMap.computeIfAbsent(name, k -> new TypeDeclKey(state, name));
60     }
61 
keyForMethod(String name, String parameterTypes)62     MethodKey keyForMethod(String name, String parameterTypes) {
63         return methodMap.computeIfAbsent(name + ":" + parameterTypes,
64                                          k -> new MethodKey(state, name, parameterTypes));
65     }
66 
keyForVariable(String name)67     VarKey keyForVariable(String name) {
68         return varMap.computeIfAbsent(name, k -> new VarKey(state, name));
69     }
70 
keyForImport(String name, SubKind snippetKind)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 
keyForStatement()79     StatementKey keyForStatement() {
80         return new StatementKey(state);
81     }
82 
keyForExpression(String name, String type)83     ExpressionKey keyForExpression(String name, String type) {
84         return new ExpressionKey(state, name, type);
85     }
86 
keyForErroneous()87     ErroneousKey keyForErroneous() {
88         return new ErroneousKey(state);
89     }
90 
doesVariableNameExist(String name)91     boolean doesVariableNameExist(String name) {
92         return varMap.get(name) != null;
93     }
94 
importKeys()95     Stream<ImportKey> importKeys() {
96         return importMap.values().stream();
97     }
98 }
99