KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > NailStats


1 /*
2
3   Copyright 2004, Martian Software, Inc.
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10  
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16
17 */

18
19 package com.martiansoftware.nailgun;
20
21 /**
22  * <p>Collects and provides statistics on a nail.</p>
23  *
24  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
25  */

26
27 public class NailStats implements Cloneable JavaDoc {
28
29     private Class JavaDoc nailclass;
30     private long runCounter;
31     private long refCounter;
32     private Object JavaDoc lock;
33     
34     /**
35      * Creates a new NailStats object for the specified class
36      * @param nailclass the class for which we'll collect statistics
37      */

38     NailStats(Class JavaDoc nailclass) {
39         this.nailclass = nailclass;
40         runCounter = 0;
41         refCounter = 0;
42         lock = new Object JavaDoc();
43     }
44
45     /**
46      * Logs the fact that an instance of this nail has started
47      */

48     void nailStarted() {
49         synchronized(lock) {
50             ++runCounter;
51             ++refCounter;
52         }
53     }
54     
55     /**
56      * Logs the fact that an instance of this nail has finished
57      */

58     void nailFinished() {
59         synchronized(lock) {
60             --refCounter;
61         }
62     }
63
64     /**
65      * Returns the number of times this nail has been run. Nails
66      * that have started but not yet finished are included in this
67      * number.
68      * @return the number of times this nail has been run.
69      */

70     public long getRunCount() {
71         return (runCounter);
72     }
73     
74     /**
75      * Returns the number of sessions currently running this nail.
76      * @return the number of sessions currently running this nail.
77      */

78     public long getRefCount() {
79         return (refCounter);
80     }
81     
82     /**
83      * Returns the class for which we're tracking statistics
84      * @return the class for which we're tracking statistics
85      */

86     public Class JavaDoc getNailClass() {
87         return (nailclass);
88     }
89     
90     /**
91      * @see java.lang.Object#hashCode
92      */

93     public int hashCode() {
94         return (nailclass.hashCode());
95     }
96     
97     /**
98      * Returns true iff the specified <code>NailStats</code> object
99      * is tracking the same class.
100      * @param o the NailStats object to check
101      * @return true iff the specified <code>NailStats</code> object
102      * is tracking the same class.
103      */

104     public boolean equals(Object JavaDoc o) {
105         NailStats other = (NailStats) o;
106         return (nailclass.equals(other.nailclass));
107     }
108     
109     /**
110      * Creates a copy of this <code>NailStats</code> object.
111      * @return a copy of this <code>NailStats</code> object.
112      */

113     public Object JavaDoc clone() {
114         Object JavaDoc result = null;
115         try {
116             result = super.clone();
117         } catch (CloneNotSupportedException JavaDoc toDiscard) {}
118         return (result);
119     }
120     
121     /**
122      * Returns a String representation of this <code>NailStats</code>
123      * object, in the form "classname: runcount/refcount".
124      * *return a String representation of this <code>NailStats</code>
125      * object.
126      */

127     public String JavaDoc toString() {
128         return (nailclass.getName() + ": " + getRunCount() + "/" + getRefCount());
129     }
130 }
131
Popular Tags