001package org.javasimon;
002
003import org.javasimon.utils.SimonUtils;
004
005/**
006 * Object holds all relevant data from {@link Stopwatch} Simon. Whenever it is important to get more values
007 * in a synchronous manner, {@link org.javasimon.Stopwatch#sample()} (or {@link Stopwatch#sampleIncrement(Object)}
008 * should be used to obtain this Java Bean object.
009 *
010 * @author <a href="mailto:virgo47@gmail.com">Richard "Virgo" Richter</a>
011 */
012public class StopwatchSample extends Sample {
013
014        private long total;
015        private long counter;
016        private long min;
017        private long max;
018        private long minTimestamp;
019        private long maxTimestamp;
020        private long active;
021        private long maxActive;
022        private long maxActiveTimestamp;
023        private long last;
024        private double mean;
025        private double standardDeviation;
026        private double variance;
027        private double varianceN;
028
029        /**
030         * Returns the total sum of all split times in nanoseconds.
031         *
032         * @return total time of the stopwatch in nanoseconds
033         */
034        public final long getTotal() {
035                return total;
036        }
037
038        /**
039         * Sets the total sum of all split times in nanoseconds.
040         *
041         * @param total total time of the stopwatch in nanoseconds
042         */
043        public final void setTotal(long total) {
044                this.total = total;
045        }
046
047        /**
048         * Returns usage count of the stopwatch. Counter is increased by {@code addTime} and
049         * {@code stop} - that means that it's updated every time the next time split is added.
050         *
051         * @return count of time splits
052         */
053        public final long getCounter() {
054                return counter;
055        }
056
057        /**
058         * Sets the usage count of the stopwatch.
059         *
060         * @param counter count of time splits
061         */
062        public final void setCounter(long counter) {
063                this.counter = counter;
064        }
065
066        /**
067         * Returns minimal time split value in nanoseconds.
068         *
069         * @return minimal time split in nanoseconds
070         */
071        public final long getMin() {
072                return min;
073        }
074
075        /**
076         * Sets the minimal time split value in nanoseconds.
077         *
078         * @param min minimal time split in nanoseconds
079         */
080        public final void setMin(long min) {
081                this.min = min;
082        }
083
084        /**
085         * Returns maximal time split value in nanoseconds.
086         *
087         * @return maximal time split in nanoseconds
088         */
089        public final long getMax() {
090                return max;
091        }
092
093        /**
094         * Sets the maximal time split value in nanoseconds.
095         *
096         * @param max maximal time split in nanoseconds
097         */
098        public final void setMax(long max) {
099                this.max = max;
100        }
101
102        /**
103         * Returns ms timestamp when the min value was measured.
104         *
105         * @return ms timestamp of the min value measurement
106         */
107        public final long getMinTimestamp() {
108                return minTimestamp;
109        }
110
111        /**
112         * Sets the ms timestamp when the min value was measured.
113         *
114         * @param minTimestamp ms timestamp of the min value measurement
115         */
116        public final void setMinTimestamp(long minTimestamp) {
117                this.minTimestamp = minTimestamp;
118        }
119
120        /**
121         * Returns ms timestamp when the max value was measured.
122         *
123         * @return ms timestamp of the max value measurement
124         */
125        public final long getMaxTimestamp() {
126                return maxTimestamp;
127        }
128
129        /**
130         * Sets the ms timestamp when the max value was measured.
131         *
132         * @param maxTimestamp ms timestamp of the max value measurement
133         */
134        public final void setMaxTimestamp(long maxTimestamp) {
135                this.maxTimestamp = maxTimestamp;
136        }
137
138        /**
139         * Returns current number of measured splits (concurrently running).
140         *
141         * @return current number of active splits
142         */
143        public final long getActive() {
144                return active;
145        }
146
147        /**
148         * Sets the current number of measured splits (concurrently running).
149         *
150         * @param active current number of active splits
151         */
152        public final void setActive(long active) {
153                this.active = active;
154        }
155
156        /**
157         * Returns peek value of active concurrent splits.
158         *
159         * @return maximum reached value of active splits
160         */
161        public final long getMaxActive() {
162                return maxActive;
163        }
164
165        /**
166         * Sets the peek value of active concurrent splits.
167         *
168         * @param maxActive maximum reached value of active splits
169         */
170        public final void setMaxActive(long maxActive) {
171                this.maxActive = maxActive;
172        }
173
174        /**
175         * Returns ms timestamp when the last peek of the active split count occurred.
176         *
177         * @return ms timestamp of the last peek of the active split count
178         */
179        public final long getMaxActiveTimestamp() {
180                return maxActiveTimestamp;
181        }
182
183        /**
184         * Sets the ms timestamp when the last peek of the active split count occurred.
185         *
186         * @param maxActiveTimestamp ms timestamp of the last peek of the active split count
187         */
188        public final void setMaxActiveTimestamp(long maxActiveTimestamp) {
189                this.maxActiveTimestamp = maxActiveTimestamp;
190        }
191
192        /**
193         * Returns the value of the last measured split in ns.
194         *
195         * @return last measured split in ns
196         */
197        public final long getLast() {
198                return last;
199        }
200
201        /**
202         * Sets the value of the last measured split in ns.
203         *
204         * @param last last measured split in ns
205         */
206        public final void setLast(long last) {
207                this.last = last;
208        }
209
210        /**
211         * Returns mean value (average) of all measured values.
212         *
213         * @return mean value
214         */
215        public final double getMean() {
216                return mean;
217        }
218
219        /**
220         * Sets the mean value (average) of all measured values.
221         *
222         * @param mean mean value
223         */
224        public final void setMean(double mean) {
225                this.mean = mean;
226        }
227
228        /**
229         * Returns unbiased estimate of standard deviation.
230         *
231         * @return unbiased estimate of standard deviation
232         */
233        public final double getStandardDeviation() {
234                return standardDeviation;
235        }
236
237        /**
238         * Sets unbiased estimate of standard deviation.
239         *
240         * @param standardDeviation unbiased estimate of standard deviation
241         */
242        public final void setStandardDeviation(double standardDeviation) {
243                this.standardDeviation = standardDeviation;
244        }
245
246        /**
247         * Returns unbiased estimate of the population variance.
248         *
249         * @return unbiased estimated variance
250         */
251        public final double getVariance() {
252                return variance;
253        }
254
255        /**
256         * Sets the unbiased estimate of the population variance.
257         *
258         * @param variance unbiased estimated variance
259         */
260        public final void setVariance(double variance) {
261                this.variance = variance;
262        }
263
264        /**
265         * Returns variance value of all measured values (entire population).
266         *
267         * @return entire population variance
268         */
269        public final double getVarianceN() {
270                return varianceN;
271        }
272
273        /**
274         * Sets the variance value of all measured values (entire population).
275         *
276         * @param varianceN entire population variance
277         */
278        public final void setVarianceN(double varianceN) {
279                this.varianceN = varianceN;
280        }
281
282        /**
283         * Returns readable representation of object.
284         *
285         * @return string with readable representation of object
286         */
287        @Override
288        public String toString() {
289                final StringBuilder sb = new StringBuilder();
290                sb.append("StopwatchSample{");
291                if (getName() != null) {
292                        sb.append("name=").append(getName()).append(", ");
293                }
294                sb.append("total=").append(SimonUtils.presentNanoTime(total));
295                sb.append(", counter=").append(counter);
296                sb.append(", max=").append(SimonUtils.presentNanoTime(max));
297                sb.append(", min=").append(SimonUtils.presentNanoTime(min));
298                sb.append(", maxTimestamp=").append(SimonUtils.presentTimestamp(maxTimestamp));
299                sb.append(", minTimestamp=").append(SimonUtils.presentTimestamp(minTimestamp));
300                sb.append(", active=").append(active);
301                sb.append(", maxActive=").append(maxActive);
302                sb.append(", maxActiveTimestamp=").append(SimonUtils.presentTimestamp(maxActiveTimestamp));
303                sb.append(", last=").append(SimonUtils.presentNanoTime(last));
304                sb.append(", mean=").append(SimonUtils.presentNanoTime((long) getMean()));
305                sb.append(", standardDeviation=").append(SimonUtils.presentNanoTime((long) getStandardDeviation()));
306                sb.append(", variance=").append(getVariance());
307                sb.append(", varianceN=").append(getVarianceN());
308                toStringCommon(sb);
309                return sb.toString();
310        }
311
312        /** Equivalent to {@link org.javasimon.StopwatchImpl#toString()} without state. */
313        public String simonToString() {
314                return "Simon Stopwatch: total " + SimonUtils.presentNanoTime(total) +
315                        ", counter " + counter +
316                        ", max " + SimonUtils.presentNanoTime(max) +
317                        ", min " + SimonUtils.presentNanoTime(min) +
318                        ", mean " + SimonUtils.presentNanoTime((long) mean) +
319                        simonToStringCommon();
320        }
321}