/Users/lyon/j4p/src/collections/stringswitch/StringSwitch.java
|
1 /*
2 * StringSwitch.java
3 *
4 * Created on October 30, 2002, 1:18 PM
5 */
6
7 package collections.stringswitch;
8
9 import java.util.HashMap;
10 import java.util.Map;
11
12 /**
13 * StringSwitch class - shows how to build a class that supports
14 * switch based on strings.
15 *
16 * @author Thomas Rowland and Douglas A. Lyon
17 * @version October 30, 2002, 1:18 PM
18 */
19 class StringSwitch {
20
21 /** Map used to store String-int mappings and provide fast lookup */
22 private Map map = new HashMap();
23
24 /**
25 * Adds a key-value pair to the hashmap.
26 * Map.put provides constant-time performance.
27 * @param key the key object, which is a String
28 * @param value the value, which is an int
29 */
30 protected final void add(String key, final int value) {
31 map.put(key, new Integer(value));
32 }
33
34 /**
35 * Retrieves a value from the hashmap give the key.
36 * Map.put provides constant-time performance.
37 * @param key the key object, which is a String
38 * @return the value to which the key is mapped
39 */
40 protected int getIdForString(String key) {
41 return ((Integer) map.get(key)).intValue();
42 }
43 }
44