/ocr/ocrservice/src/com/googlecode/eyesfree/env/Stopwatch.java
Java | 86 lines | 51 code | 14 blank | 21 comment | 5 complexity | 003920f2204287a00d4d6dd0785beb84 MD5 | raw file
1/* 2 * Copyright (C) 2011 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.googlecode.eyesfree.env; 18 19import android.os.SystemClock; 20 21/** 22 * An interval timing class. This class does not count time while the device 23 * is in deep sleep. 24 * 25 * @author Sharvil Nanavati 26 */ 27public class Stopwatch { 28 private final String name; 29 private long startTime; 30 private long totalTime; 31 private boolean isRunning; 32 33 public Stopwatch() { 34 name = null; 35 } 36 37 public Stopwatch(String name) { 38 this.name = name; 39 } 40 41 public void start() { 42 if (isRunning) { 43 return; 44 } 45 46 startTime = SystemClock.uptimeMillis(); 47 isRunning = true; 48 } 49 50 public void stop() { 51 if (!isRunning) { 52 return; 53 } 54 55 long now = SystemClock.uptimeMillis(); 56 totalTime += (now - startTime); 57 isRunning = false; 58 } 59 60 public void reset() { 61 startTime = SystemClock.uptimeMillis(); 62 totalTime = 0; 63 } 64 65 public boolean isRunning() { 66 return isRunning; 67 } 68 69 public long getElapsedMilliseconds() { 70 if (isRunning) { 71 long now = SystemClock.uptimeMillis(); 72 totalTime += (now - startTime); 73 startTime = now; 74 } 75 return totalTime; 76 } 77 78 @Override 79 public String toString() { 80 if (name != null) { 81 return "[" + name + ": " + totalTime + "ms]"; 82 } 83 84 return totalTime + "ms"; 85 } 86}