KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > util > tracing > Timer


1 package org.jacorb.util.tracing;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.util.Calendar JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 public class Timer
28 {
29     private Calendar JavaDoc date;
30     private Hashtable JavaDoc tableTable;
31
32     public Timer()
33     {
34         date = Calendar.getInstance();
35         tableTable = new Hashtable JavaDoc();
36     }
37
38     public void start(int rid, Object JavaDoc target)
39     {
40         try
41         {
42             Integer JavaDoc id = new Integer JavaDoc( rid );
43
44             Hashtable JavaDoc table = (Hashtable JavaDoc)tableTable.get( target );
45             if( table == null )
46             {
47                 table = new Hashtable JavaDoc();
48                 tableTable.put( target, table );
49             }
50             table.put( id, new Long JavaDoc( System.currentTimeMillis()));
51         }
52         catch( Exception JavaDoc e)
53     {
54             e.printStackTrace();
55         }
56     }
57
58     /**
59      * @return difference between start and stop time for
60      * request rif `
61      */

62
63     public long stop(int rid, Object JavaDoc target)
64     {
65         long t = System.currentTimeMillis();
66
67         Hashtable JavaDoc table = (Hashtable JavaDoc)tableTable.get( target );
68         if( table == null )
69             System.err.println("errorin timer: no request table for object");
70
71         Long JavaDoc startTime =
72             (Long JavaDoc)table.remove( new Integer JavaDoc(rid ));
73
74         if( startTime != null )
75         {
76             return t - startTime.longValue();
77         }
78         else
79             return -1;
80     }
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
Popular Tags