001: /*
002:
003: Copyright 2004, Martian Software, Inc.
004:
005: Licensed under the Apache License, Version 2.0 (the "License");
006: you may not use this file except in compliance with the License.
007: You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016:
017: */
018:
019: package com.martiansoftware.nailgun;
020:
021: /**
022: * <p>Collects and provides statistics on a nail.</p>
023: *
024: * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
025: */
026:
027: public class NailStats implements Cloneable {
028:
029: private Class nailclass;
030: private long runCounter;
031: private long refCounter;
032: private Object lock;
033:
034: /**
035: * Creates a new NailStats object for the specified class
036: * @param nailclass the class for which we'll collect statistics
037: */
038: NailStats(Class nailclass) {
039: this .nailclass = nailclass;
040: runCounter = 0;
041: refCounter = 0;
042: lock = new Object();
043: }
044:
045: /**
046: * Logs the fact that an instance of this nail has started
047: */
048: void nailStarted() {
049: synchronized (lock) {
050: ++runCounter;
051: ++refCounter;
052: }
053: }
054:
055: /**
056: * Logs the fact that an instance of this nail has finished
057: */
058: void nailFinished() {
059: synchronized (lock) {
060: --refCounter;
061: }
062: }
063:
064: /**
065: * Returns the number of times this nail has been run. Nails
066: * that have started but not yet finished are included in this
067: * number.
068: * @return the number of times this nail has been run.
069: */
070: public long getRunCount() {
071: return (runCounter);
072: }
073:
074: /**
075: * Returns the number of sessions currently running this nail.
076: * @return the number of sessions currently running this nail.
077: */
078: public long getRefCount() {
079: return (refCounter);
080: }
081:
082: /**
083: * Returns the class for which we're tracking statistics
084: * @return the class for which we're tracking statistics
085: */
086: public Class getNailClass() {
087: return (nailclass);
088: }
089:
090: /**
091: * @see java.lang.Object#hashCode
092: */
093: public int hashCode() {
094: return (nailclass.hashCode());
095: }
096:
097: /**
098: * Returns true iff the specified <code>NailStats</code> object
099: * 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 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 clone() {
114: Object result = null;
115: try {
116: result = super .clone();
117: } catch (CloneNotSupportedException toDiscard) {
118: }
119: return (result);
120: }
121:
122: /**
123: * Returns a String representation of this <code>NailStats</code>
124: * object, in the form "classname: runcount/refcount".
125: * *return a String representation of this <code>NailStats</code>
126: * object.
127: */
128: public String toString() {
129: return (nailclass.getName() + ": " + getRunCount() + "/" + getRefCount());
130: }
131: }
|