/Users/lyon/j4p/src/bookExamples/ch05ControlStructs/SwitchTest.java
|
1 /*
2 * SwitchTest.java
3 * Shows how to use the StringSwitch
4 * class to use switch based on strings.
5 * Runs performance tests.
6 *
7 * @author Thomas Rowland
8 * @version november 5, 2002
9 */
10
11 package bookExamples.ch05ControlStructs;
12
13 import utils.Timer;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 public class SwitchTest extends StringSwitch {
19
20 static SwitchTest st;
21 static Timer timer;
22 static int COUNT;
23 static final int ITER = 20;
24
25 // StringSwitch constants
26 public final int QUIT = 0;
27 public final int DIR = 1;
28 public final int RUN = 2;
29 public final int STOP = 3;
30 public final int DEBUG = 4;
31 public final int FWD = 5;
32 public static final int REV = 6;
33
34 /** Hashmap for storing String/int pairs.
35 (local testing. see <fast>) */
36 static Map hashMap = new HashMap();
37
38 /** Creates a new instance of SwitchTest */
39 public SwitchTest() {
40 /* Add the String/int pairs to the hashmap.
41 key=String, value=static final int */
42 add("quit", 0);
43 add("dir", 1);
44 add("run", 2);
45 add("stop", 3);
46 add("debug", 4);
47 add("forward", 5);
48 add("reverse", 6);
49 initTypes();
50 }
51
52 /** Tests StringSwitch */
53 public static void main(String[] args) {
54 COUNT = 1000000;
55 System.out.println("COUNT = " + COUNT + "\n");
56
57
58 getSwitchTest();
59 int id = st.getIdForString("TIFF_BIG_ENDIAN");
60 System.out.println("id=" + id);
61
62 timer = new Timer();
63
64 ifTestBottom();
65
66 ifTestTop();
67
68 ifTestSwitch();
69
70 ifTestInt();
71
72 ifTestFast();
73
74 }//main
75
76 private static void getSwitchTest() {
77 st = new SwitchTest();
78
79 /* (for local testing. see <fast>) */
80 st.addKeyValue("quit", 0);
81 st.addKeyValue("dir", 1);
82 st.addKeyValue("run", 2);
83 st.addKeyValue("stop", 3);
84 st.addKeyValue("debug", 4);
85 st.addKeyValue("forward", 5);
86 st.addKeyValue("reverse", 6);
87 }
88
89 private static void ifTestBottom() {
90 /* Test If-Then where match is at bottom */
91 System.out.println("\nTesting if-then (Bottom)");
92 for (int i = 0; i < ITER; i++) {
93 timer.clear();
94 timer.start();
95 for (int j = 0; j < COUNT; j++) {
96 st.doIf("reverse");
97 }
98 timer.stop();
99 System.out.println(timer.getElapsedTime());
100 }
101 }
102
103 private static void ifTestFast() {
104 /* Test <fast> Switch with a (String) where hashmap is local.
105 The purpose of this is to determine the amount
106 of contribution that the method calls on the StringSwitch object
107 have on performance. */
108 System.out.println("\nTesting Switch with a (String) <fast>");
109 for (int i = 0; i < ITER; i++) {
110 timer.clear();
111 timer.start();
112 for (int j = 0; j < COUNT; j++)
113 st.fastSwitch("reverse");
114 timer.stop();
115 System.out.println(timer.getElapsedTime());
116 }
117 }
118
119 private static void ifTestInt() {
120 /* Test Switch with an (int) */
121 System.out.println("\nTesting Switch with an (int)");
122 for (int i = 0; i < ITER; i++) {
123 timer.clear();
124 timer.start();
125 for (int j = 0; j < COUNT; j++) {
126 st.doSwitch2(REV);
127 }
128 timer.stop();
129 System.out.println(timer.getElapsedTime());
130 }
131 }
132
133 private static void ifTestSwitch() {
134 /* Test Switch with a (String) */
135 System.out.println("\nTesting Switch with a (String)");
136 for (int i = 0; i < ITER; i++) {
137 timer.clear();
138 timer.start();
139 for (int j = 0; j < COUNT; j++) {
140 st.doSwitch("reverse");
141 }
142 timer.stop();
143 System.out.println(timer.getElapsedTime());
144 }
145 }
146
147 private static void ifTestTop() {
148 /* Test If-Then where match is at top */
149 System.out.println("\nTesting if-then (Top)");
150 for (int i = 0; i < ITER; i++) {
151 timer.clear();
152 timer.start();
153 for (int j = 0; j < COUNT; j++) {
154 st.doIf("quit");
155 }
156 timer.stop();
157 System.out.println(timer.getElapsedTime());
158 }
159 }
160
161
162 /**
163 * switch structure that accepts Strings.
164 * @param s the search String
165 */
166 private void doSwitch(String s) {
167 switch (getIdForString(s)) {
168 case QUIT:
169 //System.out.print ("QUIT=" + QUIT);
170 break;
171 case DIR:
172 //System.out.print ("DIR=" + DIR);
173 break;
174 case RUN:
175 //System.out.print ("RUN=" + RUN);
176 break;
177 case STOP:
178 //System.out.print ("STOP=" + STOP);
179 break;
180 case DEBUG:
181 //System.out.print ("DEBUG=" + DEBUG);
182 break;
183 case FWD:
184 //System.out.print ("FWD=" + FWD);
185 break;
186 case REV:
187 //System.out.print ("REV=" + REV);
188 break;
189 }
190 }
191
192 /**
193 * Standard switch structure that accepts ints.
194 * @param i the search int
195 */
196 private void doSwitch2(int i) {
197 switch (i) {
198 case QUIT:
199 //System.out.print ("QUIT=" + QUIT);
200 break;
201 case DIR:
202 //System.out.print ("DIR=" + DIR);
203 break;
204 case RUN:
205 //System.out.print ("RUN=" + RUN);
206 break;
207 case STOP:
208 //System.out.print ("STOP=" + STOP);
209 break;
210 case DEBUG:
211 //System.out.print ("DEBUG=" + DEBUG);
212 break;
213 case FWD:
214 //System.out.print ("FWD=" + FWD);
215 break;
216 case REV:
217 //System.out.print ("REV=" + REV);
218 break;
219 }
220 }
221
222 /**
223 * <fast> switch structure that accepts Strings.
224 * Accesses the hashmap locally instead of calling on object.
225 * @param s the search String
226 */
227 private void fastSwitch(String s) {
228 switch (((Integer) hashMap.get(s)).intValue()) {
229 case QUIT:
230 //System.out.print ("QUIT=" + QUIT);
231 break;
232 case DIR:
233 //System.out.print ("DIR=" + DIR);
234 break;
235 case RUN:
236 //System.out.print ("RUN=" + RUN);
237 break;
238 case STOP:
239 //System.out.print ("STOP=" + STOP);
240 break;
241 case DEBUG:
242 //System.out.print ("DEBUG=" + DEBUG);
243 break;
244 case FWD:
245 //System.out.print ("FWD=" + FWD);
246 break;
247 case REV:
248 //System.out.print ("REV=" + REV);
249 break;
250 }
251 }
252
253 /**
254 * if-then structure operating on a String.
255 * @param s the search String
256 */
257 private void doIf(String s) {
258 if (s.equals("quit")) {
259 //System.out.print ("QUIT=" + QUIT);
260 } else if (s.equals("dir")) {
261 //System.out.print ("DIR=" + DIR);
262 } else if (s.equals("run")) {
263 //System.out.print ("RUN=" + RUN);
264 } else if (s.equals("stop")) {
265 //System.out.print ("STOP=" + STOP);
266 } else if (s.equals("debug")) {
267 //System.out.print ("DEBUG=" + DEBUG);
268 } else if (s.equals("forward")) {
269 //System.out.print ("FWD=" + FWD);
270 } else if (s.equals("reverse")) {
271 //System.out.print ("REV=" + REV);
272 }
273 }
274
275 public String getStringForId(int id) {
276 return (String) hashMap.get(new Integer(id));
277 }
278
279 public int getIdForString(String s) {
280 Integer i = (Integer) hashMap.get(s);
281 return i.intValue();
282 }
283
284 /**
285 * Adds a new key-value pair to the hashmap.
286 * Used for testing using local hashmap.
287 */
288 private void addKeyValue(String s, final int id) {
289 hashMap.put(s, new Integer(id));
290 }
291
292 private void initTypes() {
293 add(TYPENOTFOUND, "TYPENOTFOUND");
294 add(UUENCODED, "UUENCODED");
295 add(BTOAD, "BTOAD");
296 add(PBM, "PBM");
297 add(PGM, "PGM");
298 add(PPM, "PPM");
299 add(PBM_RAWBITS, "PBM_RAWBITS");
300 add(PGM_RAWBITS, "PGM_RAWBITS");
301 add(PPM_RAWBITS, "PPM_RAWBITS");
302 add(MGR_BITMAP, "MGR_BITMAP");
303 add(GIF87a, "GIF87a");
304 add(GIF89a, "GIF89a");
305 add(IFF_ILBM, "IFF_ILBM");
306 add(SUNRASTER, "SUNRASTER");
307 add(SGI_IMAGE, "SGI_IMAGE");
308 add(CMU_WINDOW_MANAGER_BITMAP, "CMU_WINDOW_MANAGER_BITMAP");
309 add(SUN, "SUN");
310 add(TIFF_BIG_ENDIAN, "TIFF_BIG_ENDIAN");
311 add(TIFF_LITTLE_ENDIAN, "TIFF_LITTLE_ENDIAN");
312 add(FLI, "FLI");
313 add(MPEG, "MPEG");
314 add(SUN_NEXT_AUDIO, "SUN_NEXT_AUDIO");
315 add(STANDARD_MIDI, "STANDARD_MIDI");
316 add(MICROSOFT_RIFF, "MICROSOFT_RIFF");
317 add(BZIP, "BZIP");
318 add(IFF_DATA, "IFF_DATA");
319 add(NIFF_IMAGE, "NIFF_IMAGE");
320 add(PC_BITMAP, "PC_BITMAP");
321 add(PDF_DOCUMENT, "PDF_DOCUMENT");
322 add(POSTSCRIPT_DOCUMENT, "POSTSCRIPT_DOCUMENT");
323 add(SILICON_GRAPHICS_MOVIE, "SILICON_GRAPHICS_MOVIE");
324 add(APPLE_QUICKTIME_MOVIE, "APPLE_QUICKTIME_MOVIE");
325 add(ZIP_ARCHIVE, "ZIP_ARCHIVE");
326 add(UNIX_COMPRESS, "UNIX_COMPRESS");
327 add(GZIP, "GZIP");
328 add(HUFFMAN, "HUFFMAN");
329 add(PNG_IMAGE, "PNG_IMAGE");
330 add(JPEG, "JPEG");
331 add(JPG, "JPG");
332 add(PSHOP8, "Photo Shop...8 bits per pel");
333 add(ZIP, "Zip file...Wavelet encoded image sequence?");
334 }
335
336 public int ifTest(String s) {
337
338 if (s.startsWith("TYPENOTFOUND"))
339 return TYPENOTFOUND;
340 if (s.startsWith("UUENCODED"))
341 return UUENCODED;
342 if (s.startsWith("BTOAD"))
343 return BTOAD;
344 if (s.startsWith("PBM"))
345 return PBM;
346 if (s.startsWith("PGM"))
347 return PGM;
348 if (s.startsWith("PPM"))
349 return PPM;
350 if (s.startsWith("PBM_RAWBITS"))
351 return PBM_RAWBITS;
352 if (s.startsWith("PGM_RAWBITS"))
353 return PGM_RAWBITS;
354 if (s.startsWith("PPM_RAWBITS"))
355 return PPM_RAWBITS;
356 if (s.startsWith("MGR_BITMAP"))
357 return MGR_BITMAP;
358 if (s.startsWith("GIF87a"))
359 return GIF87a;
360 if (s.startsWith("GIF89a"))
361 return GIF89a;
362 if (s.startsWith("IFF_ILBM"))
363 return IFF_ILBM;
364 if (s.startsWith("SUNRASTER"))
365 return SUNRASTER;
366 if (s.startsWith("SGI_IMAGE"))
367 return SGI_IMAGE;
368 if (s.startsWith("CMU_WINDOW_MANAGER_BITMAP"))
369 return CMU_WINDOW_MANAGER_BITMAP;
370 if (s.startsWith("SUN"))
371 return SUN;
372 if (s.startsWith("TIFF_BIG_ENDIAN"))
373 return TIFF_BIG_ENDIAN;
374 if (s.startsWith("TIFF_LITTLE_ENDIAN"))
375 return TIFF_LITTLE_ENDIAN;
376 if (s.startsWith("FLI"))
377 return FLI;
378 if (s.startsWith("MPEG"))
379 return MPEG;
380 if (s.startsWith("SUN_NEXT_AUDIO"))
381 return SUN_NEXT_AUDIO;
382 if (s.startsWith("STANDARD_MIDI"))
383 return STANDARD_MIDI;
384 if (s.startsWith("MICROSOFT_RIFF"))
385 return MICROSOFT_RIFF;
386 if (s.startsWith("BZIP"))
387 return BZIP;
388 if (s.startsWith("IFF_DATA"))
389 return IFF_DATA;
390 if (s.startsWith("NIFF_IMAGE"))
391 return NIFF_IMAGE;
392 if (s.startsWith("PC_BITMAP"))
393 return PC_BITMAP;
394 if (s.startsWith("PDF_DOCUMENT"))
395 return PDF_DOCUMENT;
396 if (s.startsWith("POSTSCRIPT_DOCUMENT"))
397 return POSTSCRIPT_DOCUMENT;
398 if (s.startsWith("SILICON_GRAPHICS_MOVIE"))
399 return SILICON_GRAPHICS_MOVIE;
400 if (s.startsWith("APPLE_QUICKTIME_MOVIE"))
401 return APPLE_QUICKTIME_MOVIE;
402 if (s.startsWith("ZIP_ARCHIVE"))
403 return ZIP_ARCHIVE;
404 if (s.startsWith("UNIX_COMPRESS"))
405 return UNIX_COMPRESS;
406 if (s.startsWith("GZIP"))
407 return GZIP;
408 if (s.startsWith("HUFFMAN"))
409 return HUFFMAN;
410 if (s.startsWith("PNG_IMAGE"))
411 return PNG_IMAGE;
412 if (s.startsWith("JPEG"))
413 return JPEG;
414 if (s.startsWith("JPG"))
415 return JPG;
416 if (s.startsWith("Photo Shop...8 bits per pel"))
417 return PSHOP8;
418 if (s.startsWith("Zip file...Wavelet encoded image sequence?"))
419 return ZIP;
420 else
421 return -1;
422 }
423
424 protected void add(int i, String s) {
425 hashMap.put(s, new Integer(i));
426 }
427
428 public static final int TYPENOTFOUND = 0;
429 public static final int UUENCODED = 1;
430 public static final int BTOAD = 2;
431 public static final int PBM = 3;
432 public static final int PGM = 4;
433 public static final int PPM = 5;
434 public static final int PBM_RAWBITS = 6;
435 public static final int PGM_RAWBITS = 7;
436 public static final int PPM_RAWBITS = 8;
437 public static final int MGR_BITMAP = 9;
438 public static final int GIF87a = 10;
439 public static final int GIF89a = 11;
440 public static final int IFF_ILBM = 12;
441 public static final int SUNRASTER = 13;
442 public static final int SGI_IMAGE = 14;
443 public static final int CMU_WINDOW_MANAGER_BITMAP = 15;
444 public static final int SUN = 16;
445 public static final int TIFF_BIG_ENDIAN = 17;
446 public static final int TIFF_LITTLE_ENDIAN = 18;
447 public static final int FLI = 19;
448 public static final int MPEG = 20;
449 public static final int SUN_NEXT_AUDIO = 21;
450 public static final int STANDARD_MIDI = 22;
451 public static final int MICROSOFT_RIFF = 23;
452 public static final int BZIP = 24;
453 public static final int IFF_DATA = 25;
454 public static final int NIFF_IMAGE = 26;
455 public static final int PC_BITMAP = 27;
456 public static final int PDF_DOCUMENT = 28;
457 public static final int POSTSCRIPT_DOCUMENT = 29;
458 public static final int SILICON_GRAPHICS_MOVIE = 30;
459 public static final int APPLE_QUICKTIME_MOVIE = 31;
460 public static final int ZIP_ARCHIVE = 32;
461 public static final int UNIX_COMPRESS = 33;
462 public static final int GZIP = 34;
463 public static final int HUFFMAN = 35;
464 public static final int PNG_IMAGE = 38;
465 public static final int JPEG = 39;
466 public static final int JPG = 40;
467 public static final int PSHOP8 = 41;
468 public static final int ZIP = 42;
469
470 }//SwitchTest
471
472