SimpleMetric.java

biz/hammurapi/metrics/SimpleMetric.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 073 [java.lang.StringBuffer] In Java 5 use StringBuilder instead of StringBuffer if access is single-threaded, e.g. StringBuffer is used as a local variable . 2 160:17
Java Inspector 089 Type is not documented 2 37:1
Java Inspector 089 Undocumented constructor 2 45:9
Java Inspector 089 Undocumented constructor 2 49:9
Java Inspector 089 Undocumented method 2 60:5
Java Inspector 089 Undocumented method 2 64:5
Java Inspector 089 Undocumented method 2 68:5
Java Inspector 089 Undocumented method 2 72:5
Java Inspector 089 Undocumented method 2 76:5
Java Inspector 089 Undocumented method 2 80:5
Java Inspector 089 Undocumented method 2 82:25
Java Inspector 089 Undocumented method 2 86:25
Java Inspector 089 Method is not properly documented 2 96:9
Java Inspector 089 Parameter measurement is not documented 2 96:9
Java Inspector 089 Javadoc contains tag for non-existent parameter value 2 96:9
Java Inspector 089 Method is not properly documented 2 119:9
Java Inspector 089 Parameter value is not documented 2 119:9
Java Inspector 089 Parameter time is not documented 2 119:9
Java Inspector 089 Method return value is not properly documented 2 119:9
Java Inspector 089 Undocumented method 2 121:25
Java Inspector 089 Undocumented method 2 125:25
Java Inspector 089 Undocumented method 2 131:9
Java Inspector 089 Undocumented method 2 151:9
Java Inspector 089 Undocumented method 2 155:9
Java Inspector 089 Undocumented method 2 159:9
Java Inspector 089 Undocumented method 2 178:9
Java Inspector 089 Undocumented method 2 186:5
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 162:33
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 164:33
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 166:33
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 170:36
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 172:36
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 173:36
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 45:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 49:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser 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 program 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 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23
24package biz.hammurapi.metrics;
25
26import java.io.Serializable;
27import java.util.ArrayList;
28import java.util.Collection;
29import java.util.Iterator;
30import java.util.List;
31
32/**
33 *
34 * @author Pavel Vlasov
35 * @version $Revision: 1.8 $
36 */
37public class SimpleMetric implements Metric, Comparable, Serializable {
38 /**
39 * Comment for <code>serialVersionUID</code>
40 */
41 private static final long serialVersionUID = -3636560361707914006L;
42 private String name;
43 private boolean keepMeasurements;
44
45 public SimpleMetric(String name) {
46 this.name=name;
47 }
48
49 public SimpleMetric(String name, boolean keepMeasurements) {
50 this.name=name;
51 this.keepMeasurements=keepMeasurements;
52 }
53
54 private int measurements;
55 private double total;
56 private double min;
57 private double max;
58 private double deviation;
59
60 public int getNumber() {
61 return measurements;
62 }
63
64 public double getMin() {
65 return min;
66 }
67
68 public double getMax() {
69 return max;
70 }
71
72 public double getAvg() {
73 return total/measurements;
74 }
75
76 public double getTotal() {
77 return total;
78 }
79
80 public void add(final double value, final long time) {
81 addMeasurement(new Measurement() {
82 public double getValue() {
83 return value;
84 }
85
86 public long getTime() {
87 return time;
88 }
89 });
90 }
91
92 /**
93 * @param value
94 * @param measurement
95 */
96 protected void addMeasurement(Measurement measurement) {
97 if (measurements==0 || measurement.getValue()<min) {
98 min=measurement.getValue();
99 }
100
101 if (measurements==0 || measurement.getValue()>max) {
102 max=measurement.getValue();
103 }
104
105 measurements++;
106 total+=measurement.getValue();
107 deviation+=Math.abs(measurement.getValue()-total/measurements);
108
109 if (keepMeasurements) {
110 measurementsList.add(measurement);
111 }
112 }
113
114 /**
115 * @param value
116 * @param time
117 * @return
118 */
119 protected Measurement newMeasurement(final double value, final long time) {
120 return new Measurement() {
121 public double getValue() {
122 return value;
123 }
124
125 public long getTime() {
126 return time;
127 }
128 };
129 }
130
131 public void add(Metric metric) {
132 if (measurements==0 || metric.getMin()<min) {
133 min=metric.getMin();
134 }
135
136 if (measurements==0 || metric.getMax()>max) {
137 max=metric.getMax();
138 }
139
140 measurements+=metric.getNumber();
141 total+=metric.getTotal();
142 deviation+=metric.getDeviation()*metric.getNumber();
143
144 if (keepMeasurements) {
145 measurementsList.addAll(metric.getMeasurements());
146 }
147 }
148
149 private List measurementsList=new ArrayList();
150
151 public Collection getMeasurements() {
152 return measurementsList;
153 }
154
155 public String getName() {
156 return name;
157 }
158
159 public String toString() {
160 StringBuffer ret=new StringBuffer();
161 ret.append(measurements)
162 .append("/")
163 .append(total)
164 .append(" <= ")
165 .append(name)
166 .append("\n");
167 Iterator it=measurementsList.iterator();
168 while (it.hasNext()) {
169 Measurement m=(Measurement) it.next();
170 ret.append("\t");
171 ret.append(m.getValue());
172 ret.append(" <- ");
173 ret.append("\n");
174 }
175 return ret.toString();
176 }
177
178 public int compareTo(Object o) {
179 if (o instanceof Metric) {
180 return Double.compare(((Metric) o).getTotal(), total);
181 }
182
183 return 1;
184 }
185
186 public double getDeviation() {
187 return deviation/measurements;
188 }
189
190}
191