Source code for /engineering/autohit-2003/src/autohit/call/modules/StopwatchModule.javaOriginal file StopwatchModule.java
   1 /**
   2  * AUTOHIT 2003
   3  * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
   4  * 
   5  * This program is free software; you can redistribute it and/or modify 
   6  * it under the terms of the GNU General Public License as published by 
   7  * the Free Software Foundation; either version 2 of the License, or (at
   8  * your option) any later version.
   9  * This program is distributed in the hope that it will be useful, but
  10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12  * more details.
  13  * 
  14  * You should have received a copy of the GNU General Public License along
  15  * with this program; if not, write to the Free Software Foundation, Inc.,
  16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17  *
  18  * Additional license information can be found in the documentation.
  19  * @author Erich P Gatejen
  20  */
  21 package autohit.call.modules;
  22 
  23 import autohit.call.CallException;
  24 import autohit.common.Constants;
  25 
  26 /**
  27  * A stopwatch that can time in seconds or milliseconds.<p>
  28  *
  29  * start() starts the stopwatch.  if it is already running, it will reset it<br>
  30  * time() returns the time that has passed in seconds<br>
  31  * millis() returns the time that has passed in seconds<br>
  32  * 
  33  * @author Erich P. Gatejen
  34  * @version 1.0
  35  * <i>Version History</i>
  36  * <code>EPG - Initial - 28Jul03</code>
  37  */
  38 public class StopwatchModule extends Module {
  39 
  40 	private final static String myNAME = "Stopwatch";
  41 
  42 	/**
  43 	 * Start time
  44 	 */
  45 	private long starttime;
  46 
  47 	/**
  48 	 * METHODS
  49 	 */
  50 	private final static String method_START = "start";
  51 	private final static String method_TIME = "time";
  52 	private final static String method_MILLIS = "millis";
  53 
  54 	/**
  55 	 * Constructor
  56 	 */
  57 	public StopwatchModule() {
  58 
  59 	}
  60 
  61 	// IMPLEMENTORS
  62 
  63 	/**
  64 	 * Execute a named method.  You must implement this method.
  65 	 * You can call any of the helpers for data and services.
  66 	 * The returned object better be a string (for now).
  67 	 * @param name name of the method
  68 	 * @see autohit.common.NOPair
  69 	 * @throws CallException
  70 	 */
  71 	public Object execute_chain(String name) throws CallException {
  72 
  73 		Object response = Constants.EMPTY_LEFT;
  74 
  75 		if (name.equals(method_START)) {
  76 			this.start();
  77 
  78 		} else if (name.equals(method_TIME)) {
  79 			response = this.time();
  80 
  81 		} else if (name.equals(method_MILLIS)) {
  82 			response = this.millis();
  83 
  84 		} else {
  85 			error("Not a provided method.  method=" + name);
  86 			response = Constants.EMPTY_LEFT;
  87 		}
  88 		return response;
  89 	}
  90 
  91 	/**
  92 	 * Allow the subclass a chance to initialize.  At a minium, an 
  93 	 * implementor should create an empty method.
  94 	 * @throws CallException
  95 	 * @return the name
  96 	 */
  97 	protected String instantiation_chain() throws CallException {
  98 		// Make sure we aren't started
  99 		starttime = System.currentTimeMillis();
 100 		return myNAME;
 101 	}
 102 
 103 	/**
 104 	 * Allow the subclass a chance to cleanup on free.  At a minium, an 
 105 	 * implementor should create an empty method.
 106 	 * @throws CallException
 107 	 */
 108 	protected void free_chain() throws CallException {
 109 		// NOTHING AT THIS TIME
 110 	}
 111 
 112 	// PRIVATE IMPLEMENTATIONS
 113 
 114 	/**
 115 	 * Start the stopwatch
 116 	 * @throws CallException
 117 	 */
 118 	private void start() throws CallException {
 119 		// get it from system time
 120 		starttime = System.currentTimeMillis();
 121 	}
 122 
 123 	/**
 124 	 * Get the time elapsed in seconds
 125 	 * @throws CallException
 126 	 */
 127 	private String time() throws CallException {
 128 
 129 		String result = Constants.ZERO;
 130 
 131 		// get it from system time
 132 		long deltatime = System.currentTimeMillis() - starttime;
 133 		deltatime = deltatime / 1000;
 134 		return Long.toString(deltatime);
 135 	}
 136 
 137 	/**
 138 	 * Get the time elapsed in seconds
 139 	 * @throws CallException
 140 	 */
 141 	private String millis() throws CallException {
 142 
 143 		String result = Constants.ZERO;
 144 
 145 		// get it from system time
 146 		long deltatime = System.currentTimeMillis() - starttime;
 147 		return Long.toString(deltatime);
 148 	}
 149 
 150 }