001package org.javasimon;
002
003import org.javasimon.utils.SimonUtils;
004
005/**
006 * Object holds all relevant data from {@link Counter} Simon. Whenever it is important to get more values
007 * in a synchronous manner, {@link org.javasimon.Counter#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 CounterSample extends Sample {
013
014        private long counter;
015        private long min;
016        private long max;
017        private long minTimestamp;
018        private long maxTimestamp;
019        private long incrementSum;
020        private long decrementSum;
021
022        /**
023         * Returns the value of the counter.
024         *
025         * @return counter value
026         */
027        public final long getCounter() {
028                return counter;
029        }
030
031        /**
032         * Sets the value of the counter.
033         *
034         * @param counter value of the counter
035         */
036        public final void setCounter(long counter) {
037                this.counter = counter;
038        }
039
040        /**
041         * Returns minimal value of counter.
042         *
043         * @return maximal reached value
044         */
045        public final long getMin() {
046                return min;
047        }
048
049        /**
050         * Sets the minimal value of the counter.
051         *
052         * @param min the minimal value of the counter.
053         */
054        public final void setMin(long min) {
055                this.min = min;
056        }
057
058        /**
059         * Returns maximal value of counter.
060         *
061         * @return maximal reached value
062         */
063        public final long getMax() {
064                return max;
065        }
066
067        /**
068         * Sets the maximal value of the counter.
069         *
070         * @param max the maximal value of the counter.
071         */
072        public final void setMax(long max) {
073                this.max = max;
074        }
075
076        /**
077         * Returns ms timestamp when the min value was reached.
078         *
079         * @return ms timestamp of the min value decremented
080         */
081        public final long getMinTimestamp() {
082                return minTimestamp;
083        }
084
085        /**
086         * Sets ms timestamp when the min value was reached.
087         *
088         * @param minTimestamp ms timestamp when the min value was reached
089         */
090        public final void setMinTimestamp(long minTimestamp) {
091                this.minTimestamp = minTimestamp;
092        }
093
094        /**
095         * Returns ms timestamp when the max value was reached.
096         *
097         * @return ms timestamp of the max value incremented
098         */
099        public final long getMaxTimestamp() {
100                return maxTimestamp;
101        }
102
103        /**
104         * Sets ms timestamp when the max value was reached.
105         *
106         * @param maxTimestamp ms timestamp when the max value was reached
107         */
108        public final void setMaxTimestamp(long maxTimestamp) {
109                this.maxTimestamp = maxTimestamp;
110        }
111
112        /**
113         * Returns the sum of all incremented values. If incremented value was negative, sum
114         * is lowered by this value.
115         *
116         * @return sum of all incremented values
117         */
118        public final long getIncrementSum() {
119                return incrementSum;
120        }
121
122        /**
123         * Sets the sum of all incremented values.
124         *
125         * @param incrementSum sum of all incremented values
126         */
127        public final void setIncrementSum(long incrementSum) {
128                this.incrementSum = incrementSum;
129        }
130
131        /**
132         * Returns the sum of all decremented values (as a positive number). If decremented value was negative, sum
133         * is lowered by this value.
134         *
135         * @return sum of all decremented values
136         */
137        public final long getDecrementSum() {
138                return decrementSum;
139        }
140
141        /**
142         * Sets the sum of all decremented values.
143         *
144         * @param decrementSum sum of all decremented values
145         */
146        public final void setDecrementSum(long decrementSum) {
147                this.decrementSum = decrementSum;
148        }
149
150        /**
151         * Returns the total sum of increments and decrements as a formatted string (+inc/-dec).
152         *
153         * @return total sum of increments and decrements as string
154         */
155        public final String getTotalAsString() {
156                return "+" + SimonUtils.presentMinMaxCount(incrementSum) + "/-" + SimonUtils.presentMinMaxCount(decrementSum);
157        }
158
159        /**
160         * Returns readable representation of object.
161         *
162         * @return string with readable representation of object
163         */
164        @Override
165        public String toString() {
166                final StringBuilder sb = new StringBuilder();
167                sb.append("CounterSample{");
168                if (getName() != null) {
169                        sb.append("name=").append(getName()).append(", ");
170                }
171                sb.append("counter=").append(counter);
172                sb.append(", max=").append(SimonUtils.presentMinMaxCount(max));
173                sb.append(", min=").append(SimonUtils.presentMinMaxCount(min));
174                sb.append(", maxTimestamp=").append(SimonUtils.presentTimestamp(maxTimestamp));
175                sb.append(", minTimestamp=").append(SimonUtils.presentTimestamp(minTimestamp));
176                sb.append(", incrementSum=").append(incrementSum);
177                sb.append(", decrementSum=").append(decrementSum);
178                toStringCommon(sb);
179                return sb.toString();
180        }
181
182        /** Equivalent to {@link org.javasimon.CounterImpl#toString()} without state. */
183        public synchronized String simonToString() {
184                return "Simon Counter: counter=" + counter +
185                        ", max=" + SimonUtils.presentMinMaxCount(max) +
186                        ", min=" + SimonUtils.presentMinMaxCount(min) +
187                        simonToStringCommon();
188        }
189}