001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2005-2006 Robert Grimm
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * version 2 as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.parser;
020:
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: /**
025: * A mapping between module names. This class has been designed for
026: * the efficient renaming of both {@link ModuleDependency module
027: * dependencies} and {@link NonTerminal nonterminals}.
028: *
029: * @author Robert Grimm
030: * @version $Revision: 1.6 $
031: */
032: public class ModuleMap implements Renamer.Translation {
033:
034: /**
035: * The mapping from module names (represented as strings) to module
036: * names (represented as {@link ModuleName module names}).
037: */
038: protected Map<String, ModuleName> map;
039:
040: /** Create a new module map. */
041: public ModuleMap() {
042: map = new HashMap<String, ModuleName>();
043: }
044:
045: /**
046: * Create a new module map.
047: *
048: * @param key The single initial key.
049: * @param value The single initial value.
050: */
051: public ModuleMap(ModuleName key, ModuleName value) {
052: map = new HashMap<String, ModuleName>();
053: map.put(key.name, value);
054: }
055:
056: /**
057: * Create a new module map.
058: *
059: * @param keys The list of keys.
060: * @param values The list of values.
061: * @throws IllegalArgumentException
062: * Signals that the lists have different lengths.
063: */
064: public ModuleMap(ModuleList keys, ModuleList values) {
065: if (keys.size() != values.size()) {
066: throw new IllegalArgumentException(
067: "Different numbers of keys and values");
068: }
069:
070: map = new HashMap<String, ModuleName>();
071: int size = keys.size();
072: for (int i = 0; i < size; i++) {
073: map.put(keys.get(i).name, values.get(i));
074: }
075: }
076:
077: /**
078: * Add the specified mapping.
079: *
080: * @param key The key.
081: * @param value The value.
082: */
083: public void put(ModuleName key, ModuleName value) {
084: map.put(key.name, value);
085: }
086:
087: /**
088: * Determine whether this module map contains a mapping for the
089: * specified module name.
090: *
091: * @param key The module name.
092: * @return <code>true</code> if this module map has a value for the
093: * key.
094: */
095: public boolean containsKey(ModuleName key) {
096: return map.containsKey(key.name);
097: }
098:
099: /**
100: * Determine whether this module map contains a mapping for the
101: * specified module name.
102: *
103: * @param key The module name.
104: * @return <code>true</code> if this module map has a value for the
105: * key.
106: */
107: public boolean containsKey(String key) {
108: return map.containsKey(key);
109: }
110:
111: /**
112: * Look up the specified module name.
113: *
114: * @param key The module name.
115: * @return The corresponding value or <code>null</code> if there is
116: * no mapping for the module name.
117: */
118: public ModuleName get(ModuleName key) {
119: return map.get(key.name);
120: }
121:
122: /**
123: * Look up the specified module name.
124: *
125: * @param key The module name.
126: * @return The corresponding value or <code>null</code> if there is
127: * no mapping for the module name.
128: */
129: public String get(String key) {
130: ModuleName value = map.get(key);
131: return (null == value) ? null : value.name;
132: }
133:
134: public NonTerminal map(NonTerminal nt, Analyzer analyzer) {
135: return nt.rename(this);
136: }
137:
138: }
|