1 | /* |
---|
2 | * To change this license header, choose License Headers in Project Properties. |
---|
3 | * To change this template file, choose Tools | Templates |
---|
4 | * and open the template in the editor. |
---|
5 | */ |
---|
6 | package nl.mpi.annot.translate; |
---|
7 | |
---|
8 | import java.util.Map; |
---|
9 | import java.util.concurrent.ConcurrentHashMap; |
---|
10 | |
---|
11 | /** |
---|
12 | * |
---|
13 | * @author petbei |
---|
14 | */ |
---|
15 | public class SymbolTable { |
---|
16 | Map<String, Integer> stMap = new ConcurrentHashMap<String, Integer>(); |
---|
17 | |
---|
18 | int getSize() { |
---|
19 | return stMap.size(); |
---|
20 | } |
---|
21 | |
---|
22 | void add(String s) { |
---|
23 | // precondition: 's' is not null. Empty String value ("") is allowed |
---|
24 | if (stMap.containsKey(s)) { |
---|
25 | stMap.put(s, stMap.get(s) + 1); |
---|
26 | // System.out.println("SymbolTable.addSymbol(s): symbol \"" + s + "\" is already defined"); |
---|
27 | } |
---|
28 | else { |
---|
29 | // System.out.println("SymbolTable.addSymbol(s): new symbol \"" + s + "\" added"); |
---|
30 | stMap.put(s, 1); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | boolean isDefined(String s) { |
---|
35 | // precondition: 's' is not null. Empty String value ("") is allowed |
---|
36 | return (stMap.containsKey(s)); |
---|
37 | } |
---|
38 | |
---|
39 | Integer get(String s) { |
---|
40 | // precondition: 's' is not null. Empty String value ("") is allowed |
---|
41 | if (stMap.containsKey(s)) { |
---|
42 | //System.out.println("SymbolTable.isDefined(s): symbol \"" + s + "\" is defined"); |
---|
43 | return stMap.get(s); |
---|
44 | } |
---|
45 | else { |
---|
46 | //System.out.println("SymbolTable.isDefined(s): symbol \"" + s + "\" is NOT defined"); |
---|
47 | return null; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | void display() { |
---|
52 | System.out.println("========== START symbol table: " + stMap.size() + " elements =========="); |
---|
53 | for (String key: stMap.keySet()) { |
---|
54 | System.out.println(" \"" + key + "\" -> " + stMap.get(key)); |
---|
55 | } |
---|
56 | System.out.println("========== END symbol table " + " ==========\n"); |
---|
57 | } |
---|
58 | } |
---|