KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > MyTimer


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: MyTimer.java,v 1.5 2007/01/07 06:14:00 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 /**
25  * Class encapsulating timer functionality.
26  *
27  * @version $Id: MyTimer.java,v 1.5 2007/01/07 06:14:00 bastafidli Exp $
28  * @author Miro Halas
29  * @code.reviewer Miro Halas
30  * @code.reviewed Initial revision
31  */

32 public class MyTimer
33 {
34    // Attributes ///////////////////////////////////////////////////////////////
35

36    /**
37     * Remeber start time here.
38     */

39    private long m_lStartTime;
40    
41    /**
42     * Remeber stop time here.
43     */

44    private long m_lStopTime;
45
46    // Constructors /////////////////////////////////////////////////////////////
47

48    /**
49     * Default constructor.
50     * Starts counting fro the moment it is cosntructed.
51     */

52    public MyTimer(
53    )
54    {
55       reset();
56    }
57
58    // Logic ////////////////////////////////////////////////////////////////////
59

60    /**
61     * @return long - start time;
62     */

63    public long getStartTime()
64    {
65       return m_lStartTime;
66    }
67
68    /**
69     * @return - stop time;
70     */

71    public long getStopTime()
72    {
73       return m_lStopTime;
74    }
75
76    /**
77     * Reset the counter and start counting from scratch.
78     */

79    public void reset(
80    )
81    {
82       m_lStartTime = System.currentTimeMillis();
83       m_lStopTime = 0;
84    }
85
86    /**
87     * Stop the timer.
88     */

89    public void stop(
90    )
91    {
92       m_lStopTime = System.currentTimeMillis();
93    }
94
95    /**
96     * Get timer duration (the timer doesn't stop) in milliseconds.
97     *
98     * @return long - difference between stop and start time.
99     */

100    public long getDuration(
101    )
102    {
103       long lStopTime;
104
105       if (m_lStopTime == 0)
106       {
107          lStopTime = System.currentTimeMillis();
108       }
109       else
110       {
111          lStopTime = m_lStopTime;
112       }
113
114       return lStopTime - m_lStartTime;
115    }
116
117    /**
118     * Print the state of the timer without stopping it.
119     * @return String - timing information
120     */

121    public String JavaDoc toString(
122    )
123    {
124       long lTotalMS = getDuration();
125       long lMS = lTotalMS % 1000;
126       long lTotalSecs = lTotalMS / 1000;
127       long lSecs = lTotalSecs % 60;
128       long lTotalMins = lTotalSecs / 60;
129       long lMinutes = lTotalMins % 60;
130       long lHours = lTotalMins / 60;
131       StringBuffer JavaDoc sbBuffer = new StringBuffer JavaDoc();
132
133       if (lHours > 0)
134       {
135          sbBuffer.append(lHours);
136          sbBuffer.append(":");
137          sbBuffer.append(lMinutes);
138          sbBuffer.append(":");
139          sbBuffer.append(lSecs);
140          sbBuffer.append(".");
141          sbBuffer.append(lMS);
142       }
143       else if (lMinutes > 0)
144       {
145          sbBuffer.append(lMinutes);
146          sbBuffer.append(":");
147          sbBuffer.append(lSecs);
148          sbBuffer.append(".");
149          sbBuffer.append(lMS);
150       }
151       else if (lSecs > 0)
152       {
153          sbBuffer.append(lSecs);
154          sbBuffer.append(".");
155          sbBuffer.append(lMS);
156          sbBuffer.append(" seconds");
157       }
158       else
159       {
160          sbBuffer.append(lMS);
161          sbBuffer.append(" ms");
162       }
163       
164       return sbBuffer.toString();
165    }
166 }
167
168
Popular Tags