001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.util;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.Stack;
023:
024: /**
025: * A utility class for performance statistics of Spoon.
026: */
027: public class Timer {
028: private static List<Timer> timestamps = new ArrayList<Timer>();
029:
030: private static Stack<Timer> current = new Stack<Timer>();
031:
032: /**
033: * Starts a timer.
034: *
035: * @param name
036: * the timer name
037: */
038: public static void start(String name) {
039: current.push(new Timer(name));
040: }
041:
042: /**
043: * Stops a timer.
044: *
045: * @param name
046: * the timer name
047: */
048: public static void stop(String name) {
049: if (!current.peek().getName().equals(name)) {
050: throw new RuntimeException("Must stop last timer");
051: }
052: current.peek().stop();
053: timestamps.add(current.pop());
054: }
055:
056: /**
057: * Displays all the timers.
058: */
059: public static void display() {
060: for (Timer time : timestamps) {
061: System.out.println(time);
062: }
063: }
064:
065: String name;
066:
067: long start, stop;
068:
069: /**
070: * Constructs a timer.
071: *
072: * @param name
073: * the timer name
074: */
075: public Timer(String name) {
076: super ();
077: this .name = name;
078: start = System.currentTimeMillis();
079: }
080:
081: /**
082: * Stops the current timer.
083: */
084: public void stop() {
085: stop = System.currentTimeMillis();
086: }
087:
088: /**
089: * Gets this timer's name.
090: *
091: * @return
092: */
093: public String getName() {
094: return name;
095: }
096:
097: /**
098: * Gets the current time of this timer.
099: */
100: public long getValue() {
101: return stop - start;
102: }
103:
104: /**
105: * A string representation of this timer.
106: */
107: @Override
108: public String toString() {
109: return getName() + " \t" + getValue() + "ms";
110: }
111:
112: }
|