1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.timer;
22  
23  import java.text.DecimalFormat;
24  
25  /***
26   * A simple interface for measuring time intervals.
27   *
28   * Created on 19.11.2004
29   *
30   * Contributing Author(s):
31   *
32   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
33   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
34   * 
35   * @author vlads
36   * @version $Revision: 1.5 $ ($Author: vlads $) $Date: 2004/12/03 09:14:03 $
37   */
38  public class SimpleTimer {
39      private double start_time;
40      private double end_time;
41      private double pause_time;
42      
43      private Timer timer;
44      
45      public SimpleTimer() {
46          init(true);
47      }
48      
49      public SimpleTimer(boolean useNative) {
50          init(useNative);
51      }
52      
53      public void init(boolean useNative) {    
54          if (useNative && NativeTimer.isAvalabel()) {
55              timer = new NativeTimer(); 
56          } else {
57              timer = new SystemTimer();
58          }
59          start_time = timer.getTimeCounter();
60          end_time = 0;
61          pause_time = 0;
62      }
63  
64      public void start() {
65          start_time = timer.getTimeCounter();
66      }
67      
68      public void end() {
69          end_time = timer.getTimeCounter();
70          un_pause();
71      }
72  
73      public void pause() {
74          if (pause_time == 0) {
75              pause_time = timer.getTimeCounter();
76          }
77      }
78  
79      public void un_pause() {
80          if (pause_time != 0) {
81              double un_pause_time = timer.getTimeCounter();
82              start_time = start_time + (un_pause_time - pause_time);
83              pause_time = 0;
84          }
85      }
86      
87      public double duration() {
88  		return end_time - start_time;
89  	}
90  
91      
92      public String text() {
93          return text(duration());
94      }
95      
96      public String toString() {
97      	return text();
98      }
99      
100     public String text2(long cnt) {
101         return text(duration()/ cnt);
102     }
103     
104     public static String text(double duration) {
105         final DecimalFormat format = new DecimalFormat();
106         format.setMinimumFractionDigits(4);
107         format.setMaximumFractionDigits(4);
108         return format.format(duration);
109     }
110     
111 	public static String textPrc(double duration, double durationTotal) {
112 		if (durationTotal == 0.0) {
113 			return "n/a";
114 		}
115 		final DecimalFormat format = new DecimalFormat("##0");
116 		format.setMinimumIntegerDigits(1);
117 		format.setMaximumFractionDigits(0);
118 				
119 		int prc = (new Double(Math.floor(100 * duration/durationTotal))).intValue();
120 		return format.format(prc);
121 	}
122 }