1   // joi/6/juno/ShellCommandTable.java (version 6)                         
2   //                                                            
3   //                                                            
4   // Copyright 2003 Bill Campbell and Ethan Bolker                         
5                                                               
6   import java.util.*;
7   
8   /**
9    * A ShellCommandTable object maintains a dispatch table of 
10   * ShellCommand objects keyed by the command names used to invoke 
11   * them.
12   *
13   * To add a new shell command to the table, install it from 
14   * method fillTable().
15   *
16   * @see ShellCommand
17   *
18   * @version 6
19   */
20  
21  public class ShellCommandTable 
22  {
23      private Map table = new TreeMap(); 
24      
25      /**
26       * Construct and fill a shell command table.
27       */
28  
29      public ShellCommandTable()
30      {
31          fillTable();
32      }
33  
34      /**
35       * Get a ShellCommand, given the command name key.
36       *
37       * @param key the name associated with the command we're
38       *            looking for.
39       *
40       * @return the command we're looking for, null if none.
41       */
42  
43      public ShellCommand lookup( String key ) 
44      {
45          return (ShellCommand)table.get( key );
46      }
47  
48      /**
49       * Get an array of the command names.
50       *
51       * @return the array of command names.
52       */
53  
54      public String[] getCommandNames() 
55      {
56          return (String[]) table.keySet().toArray( new String[0] );
57      }
58  
59      // Associate a command name with a ShellCommand.
60  
61      private void install( String commandName, ShellCommand command )
62      {
63          table.put( commandName, command );
64      }
65  
66      // Fill the dispatch table with ShellCommands, keyed by their 
67      // command names.
68  
69      private void fillTable() 
70      {
71          install( "newfile", new NewfileCommand() );
72          install( "type", new TypeCommand() );
73          install( "mkdir", new MkdirCommand() );
74          install( "help", new HelpCommand() );
75      }
76  }
77