/Users/lyon/j4p/src/collections/stringswitch/SwitchTest.java

1    /* 
2     * SwitchTest.java 
3     * 
4     * Created on October 30, 2002, 1:18 PM 
5     */ 
6     
7    package collections.stringswitch; 
8     
9    import utils.Timer; 
10    
11   /* 
12    *  SwitchTest.java 
13    *  Shows how to use the StringSwitch class to use switch based on strings. 
14    *  Runs performance tests. 
15    * 
16    *  @author Thomas Rowland 
17    *  @version november 5, 2002 
18    */ 
19    
20   public class SwitchTest extends StringSwitch { 
21    
22       static SwitchTest st; 
23       static Timer timer; 
24       static int COUNT; 
25       static final int ITER = 20; 
26    
27       // StringSwitch constants 
28       public final int QUIT = 0; 
29       public final int DIR = 1; 
30       public final int RUN = 2; 
31       public final int STOP = 3; 
32       public final int DEBUG = 4; 
33       public final int FWD = 5; 
34       public static final int REV = 6; 
35    
36       /** Creates a new instance of SwitchTest */ 
37       public SwitchTest() { 
38    
39           /* Add the String/int pairs to the hashmap. 
40              key=String, value=static final int */ 
41           add("quit", 0); 
42           add("dir", 1); 
43           add("run", 2); 
44           add("stop", 3); 
45           add("debug", 4); 
46           add("forward", 5); 
47           add("reverse", 6); 
48       } 
49    
50       /** Tests StringSwitch */ 
51       public static void main(String[] args) { 
52           if (args.length < 1) { 
53               System.err.println("Usage: stringswitch.SwitchTest [COUNT]"); 
54               System.exit(0); 
55           } 
56    
57           COUNT = Integer.parseInt(args[0]); 
58           System.out.println("COUNT = " + COUNT + "\n"); 
59    
60           //st = new SwitchTest(); 
61           SwitchTest switchtest = new SwitchTest(); 
62           timer = new Timer(); 
63    
64           /*  Test If-Then where match is at bottom */ 
65           switchtest.runIfThen("reverse"); 
66    
67           /*  Test If-Then where match is at top */ 
68           switchtest.runIfThen("quit"); 
69    
70           /*  Test Switch with a (String) */ 
71           switchtest.runStringSwitch("reverse"); 
72    
73           /*  Test Switch with an (int) */ 
74           switchtest.runIntSwitch(REV); 
75    
76       } 
77    
78    
79       /** 
80        *  switch structure that accepts Strings. 
81        *  @param s the search String 
82        */ 
83       private void doStringSwitch(String s) { 
84           switch (getIdForString(s)) { 
85               case QUIT: 
86                   //System.out.print ("QUIT=" + QUIT); 
87                   break; 
88               case DIR: 
89                   //System.out.print ("DIR=" + DIR); 
90                   break; 
91               case RUN: 
92                   //System.out.print ("RUN=" + RUN); 
93                   break; 
94               case STOP: 
95                   //System.out.print ("STOP=" + STOP); 
96                   break; 
97               case DEBUG: 
98                   //System.out.print ("DEBUG=" + DEBUG); 
99                   break; 
100              case FWD: 
101                  //System.out.print ("FWD=" + FWD); 
102                  break; 
103              case REV: 
104                  //System.out.print ("REV=" + REV); 
105                  break; 
106          } 
107      } 
108   
109      /** 
110       *  Standard switch structure that accepts ints. 
111       *  @param i the search int 
112       */ 
113      private void doIntSwitch(int i) { 
114          switch (i) { 
115              case QUIT: 
116                  //System.out.print ("QUIT=" + QUIT); 
117                  break; 
118              case DIR: 
119                  //System.out.print ("DIR=" + DIR); 
120                  break; 
121              case RUN: 
122                  //System.out.print ("RUN=" + RUN); 
123                  break; 
124              case STOP: 
125                  //System.out.print ("STOP=" + STOP); 
126                  break; 
127              case DEBUG: 
128                  //System.out.print ("DEBUG=" + DEBUG); 
129                  break; 
130              case FWD: 
131                  //System.out.print ("FWD=" + FWD); 
132                  break; 
133              case REV: 
134                  //System.out.print ("REV=" + REV); 
135                  break; 
136          } 
137      } 
138   
139      /** 
140       *  if-then structure operating on a String. 
141       *  @param s the search String 
142       */ 
143      private void doIf(String s) { 
144          if (s.equals("quit")) { 
145              //System.out.print ("QUIT=" + QUIT); 
146          } else if (s.equals("dir")) { 
147              //System.out.print ("DIR=" + DIR); 
148          } else if (s.equals("run")) { 
149              //System.out.print ("RUN=" + RUN); 
150          } else if (s.equals("stop")) { 
151              //System.out.print ("STOP=" + STOP); 
152          } else if (s.equals("debug")) { 
153              //System.out.print ("DEBUG=" + DEBUG); 
154          } else if (s.equals("forward")) { 
155              //System.out.print ("FWD=" + FWD); 
156          } else if (s.equals("reverse")) { 
157              //System.out.print ("REV=" + REV); 
158          } 
159      } 
160   
161      private void runIfThen(String svalue) { 
162          System.out.println("\nTesting if-then with a String (Bottom)"); 
163          for (int i = 0; i < ITER; i++) { 
164              timer.clear(); 
165              timer.start(); 
166              for (int j = 0; j < COUNT; j++) { 
167                  st.doIf(svalue); 
168              } 
169              timer.stop(); 
170              System.out.println(timer.getElapsedTime()); 
171          } 
172      } 
173   
174      private void runIntSwitch(int ivalue) { 
175          System.out.println("\nTesting Switch with an int"); 
176          for (int i = 0; i < ITER; i++) { 
177              timer.clear(); 
178              timer.start(); 
179              for (int j = 0; j < COUNT; j++) { 
180                  st.doIntSwitch(ivalue); 
181              } 
182              timer.stop(); 
183              System.out.println(timer.getElapsedTime()); 
184          } 
185      } 
186   
187      private void runStringSwitch(String svalue) { 
188          System.out.println("\nTesting Switch with a String"); 
189          for (int i = 0; i < ITER; i++) { 
190              timer.clear(); 
191              timer.start(); 
192              for (int j = 0; j < COUNT; j++) { 
193                  st.doStringSwitch(svalue); 
194              } 
195              timer.stop(); 
196              System.out.println(timer.getElapsedTime()); 
197          } 
198      } 
199   
200  } 
201   
202