upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / util / BulkTimer.java
1 /**
2  * Copyright 2001 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8 package com.sun.speech.freetts.util;
9
10 import java.util.Map;
11 import java.util.LinkedHashMap;
12 import java.util.Collection;
13 import java.util.Iterator;
14
15 /**
16  * Provides a suite of timers that are used to collect and generate
17  * performance metrics for FreeTTS.
18  */
19 public class BulkTimer {
20     /**
21      * A BulkTimer that can be used by classes that need to
22      * time their loading phase.
23      */
24     public final static BulkTimer LOAD = new BulkTimer();
25
26     private final static String SELF = "OverallTime";
27     private boolean verbose;
28     private Map timers;
29     
30
31     /**
32      * Creates a bulk timer.
33      */
34     public BulkTimer() {
35         this.verbose = false;
36         timers = new LinkedHashMap();
37     }
38
39     /**
40      * Starts the timer with the given name. A BulkTimer can manage
41      * any number of timers. The timers are referenced by name. A
42      * timer is created the first time it is referenced.
43      *
44      * @param name the name of the timer to start
45      */
46     public void start(String name) {
47         getTimer(name).start();
48     }
49
50     /**
51      * Stops the timer with the given name.
52      *
53      * @param name the name of the timer
54      */
55     public void stop(String name) {
56         getTimer(name).stop(verbose);
57     }
58
59     /**
60      * Starts the bulk timer.  The BulkTimer maintains a timer for
61      * itself (called SELF). This is used to measure the overall time
62      * for a bulk timer. When timing data is displayed, the percentage
63      * of total time is displayed. The total time is the time between
64      * <code> start </code> and <code> end </code>  calls on the
65      * <code> BulkTimer </code> .
66      */
67     public void start() {
68         getTimer(SELF).start();
69     }
70
71
72     /**
73      * Stops the bulk timer.
74      */
75     public void stop() {
76         getTimer(SELF).stop(verbose);
77     }
78
79     /**
80      * Sets verbose mode.
81      *
82      * @param verbose the verbose mode
83      */
84     public void setVerbose(boolean verbose) {
85         this.verbose = verbose;
86     }
87
88     /**
89      * Checks to see if we are in verbose mode.
90      *
91      * @return <code>true</code>  if verbose mode; otherwise
92      *     <code>false</code>.
93      */
94     public boolean isVerbose() {
95         return verbose;
96     }
97
98     /**
99      * Gets the timer with the given name.
100      *
101      * @param name the timer name
102      *
103      * @return the timer with that name
104      */
105     public Timer getTimer(String name) {
106         if (!timers.containsKey(name)) {
107             timers.put(name, new Timer(name));
108         }
109         return (Timer) timers.get(name);
110     }
111
112     /**
113      * Shows all of the collected times.
114      *
115      * @param title the title for the display
116      */
117     public void show(String title) {
118         long overall = getTimer(SELF).getCurrentTime();
119         Collection  values = timers.values();
120         Timer.showTimesShortTitle(title);
121         for (Iterator i = values.iterator(); i.hasNext(); ) {
122             Timer t = (Timer) i.next();
123             t.showTimes(overall);
124         }
125     }
126 }