View Javadoc

1   /*
2    * $Id: DefaultStopwatchReport.java,v 1.1 2006/03/01 17:48:05 azzazzel Exp $
3    *
4    * Copyright 2006 Commsen International
5    * 
6    * Licensed under the Common Public License, Version 1.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.opensource.org/licenses/cpl1.0.txt
11   * 
12   * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS 
14   * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15   *
16   */
17  package com.commsen.stopwatch.reports;
18  
19  import java.text.DecimalFormat;
20  import java.text.NumberFormat;
21  
22  import com.commsen.stopwatch.Report;
23  
24  /***
25   * 
26   * This is the default implementation of {@link com.commsen.stopwatch.Report} interface. It is
27   * mainly used by {@link com.commsen.stopwatch.engines.DefaultStopwatchEngine}.
28   * 
29   * Note: this class has a natural ordering that is inconsistent with equals.
30   * 
31   * @see com.commsen.stopwatch.Report
32   * @author Milen Dyankov
33   * 
34   */
35  public class DefaultStopwatchReport implements Report {
36  
37  	/***
38  	 * 
39  	 */
40  	private static final long serialVersionUID = 1L;
41  
42  	private String group;
43  	private String label;
44  	private long count;
45  	private double minTime;
46  	private double maxTime;
47  	private double averageTime;
48  	private double totalTime;
49  
50  
51  	/***
52  	 * Creates new instance of this class.
53  	 * 
54  	 * @param group
55  	 * @param label
56  	 * @param count
57  	 * @param minTime
58  	 * @param maxTime
59  	 * @param averageTime
60  	 * @param totalTime
61  	 */
62  	public DefaultStopwatchReport(String group, String label, long count, double minTime, double maxTime,
63  	        double averageTime, double totalTime) {
64  		this.group = group;
65  		this.label = label;
66  		this.count = count;
67  		this.minTime = minTime;
68  		this.maxTime = maxTime;
69  		this.averageTime = averageTime;
70  		this.totalTime = totalTime;
71  	}
72  
73  
74  	/***
75  	 * Generates string representation of this report
76  	 * 
77  	 * @see java.lang.Object#toString()
78  	 */
79  	public String toString() {
80  		NumberFormat formatter = new DecimalFormat("#.##");
81  		return new StringBuffer("Group=\"").append(group).append("\"") //
82  		        .append("  Label=\"").append(label).append("\"") //
83  		        .append("  Count=").append(count) //
84  		        .append("  Min=").append(formatter.format(minTime)).append("ms") //
85  		        .append("  Avg=").append(formatter.format(averageTime)).append("ms") //
86  		        .append("  Max=").append(formatter.format(maxTime)).append("ms") //
87  		        .append("  Total=").append(formatter.format(totalTime)).append("ms") // 
88  		        .toString(); //
89  
90  	}
91  
92  
93  	/***
94  	 * @return Returns the average.
95  	 * @see Report#getAverageTime()
96  	 */
97  	public double getAverageTime() {
98  		return averageTime;
99  	}
100 
101 
102 	/***
103 	 * @return Returns the count.
104 	 * @see Report#getCount()
105 	 */
106 	public long getCount() {
107 		return count;
108 	}
109 
110 
111 	/***
112 	 * @return Returns the fastest.
113 	 * @see Report#getMinTime()
114 	 */
115 	public double getMinTime() {
116 		return minTime;
117 	}
118 
119 
120 	/***
121 	 * @return Returns the group.
122 	 * @see Report#getGroup()
123 	 */
124 	public String getGroup() {
125 		return group;
126 	}
127 
128 
129 	/***
130 	 * @return Returns the label.
131 	 * @see Report#getLabel()
132 	 */
133 	public String getLabel() {
134 		return label;
135 	}
136 
137 
138 	/***
139 	 * @return Returns the slowest.
140 	 * @see Report#getMaxTime()
141 	 */
142 	public double getMaxTime() {
143 		return maxTime;
144 	}
145 
146 
147 	/***
148 	 * @return Returns the total.
149 	 * @see Report#getTotalTime()
150 	 */
151 	public double getTotalTime() {
152 		return totalTime;
153 	}
154 
155 
156 	/***
157 	 * Compares this report with the specified report for order. Returns a negative integer, zero,
158 	 * or a positive integer as this report is less than, equal to, or greater than the specified
159 	 * report.
160 	 * <p>
161 	 * 
162 	 * <b>Note:</b> this class has a natural ordering that is inconsistent with equals:
163 	 * 
164 	 * Reports are compared by the value of totalTime. Report <code>A</code> is considered to be
165 	 * less then report <code>B</code> when <code>A.getTotalTime() > B.getTotoalTime()</code>
166 	 * 
167 	 * @param arg0 the Object to be compared.
168 	 * 
169 	 * @return a negative integer, zero, or a positive integer as specified report is less than,
170 	 *         equal to, or greater than the current report.
171 	 * 
172 	 * @throws ClassCastException if the specified object is not an instnce of {@link Report}
173 	 * 
174 	 */
175 	public int compareTo(Object arg0) {
176 		Report report = (Report) arg0;
177 		if (report == this) return 0;
178 		return Double.compare(report.getTotalTime(), this.getTotalTime());
179 	}
180 
181 
182 	/*
183 	 * (non-Javadoc)
184 	 * 
185 	 * @see java.lang.Object#equals(java.lang.Object)
186 	 */
187 	public boolean equals(Object obj) {
188 		if (obj == null || !(obj instanceof Report)) return false;
189 		return super.equals(obj);
190 	}
191 
192 
193 	/*
194 	 * (non-Javadoc)
195 	 * 
196 	 * @see java.lang.Object#hashCode()
197 	 */
198 	public int hashCode() {
199 		// TODO Auto-generated method stub
200 		return super.hashCode();
201 	}
202 
203 }