001package org.javasimon.utils;
002
003import org.javasimon.CounterSample;
004import org.javasimon.Simon;
005import org.javasimon.SimonFilter;
006
007/**
008 * Holds aggregate values for counters in a hierarchy as a result of
009 * {@link SimonUtils#calculateCounterAggregate(Simon)} (optionally with {@link SimonFilter}).
010 *
011 * @author <a href="mailto:ivan.mushketyk@gmail.com">Ivan Mushketyk</a>
012 * @since 3.5
013 */
014public class CounterAggregate {
015
016        private long counter;
017        private long incrementSum;
018        private long decrementSum;
019        private long max = Long.MIN_VALUE;
020        private long min = Long.MAX_VALUE;
021        private long maxTimestamp;
022        private long minTimestamp;
023
024        CounterAggregate() {
025        }
026
027        /**
028         * Returns sum of all counters in a hierarchy of simons.
029         *
030         * @return sum of all counters
031         */
032        public long getCounter() {
033                return counter;
034        }
035
036        /**
037         * Returns sum of all increment sums in a hierarchy of simons.
038         *
039         * @return sum of all increment sums
040         */
041        public long getIncrementSum() {
042                return incrementSum;
043        }
044
045        /**
046         * Returns sum of all decrement sums in a hierarchy of simons.
047         *
048         * @return sum of all decrement sums
049         */
050        public long getDecrementSum() {
051                return decrementSum;
052        }
053
054        /**
055         * Returns max value among max values in a hierarchy of simons.
056         *
057         * @return max value among max values
058         */
059        public long getMax() {
060                return max;
061        }
062
063        /**
064         * Returns timestamp in milliseconds when max value was reached.
065         *
066         * @return timestamp in milliseconds when max value was reached
067         */
068        public long getMaxTimestamp() {
069                return maxTimestamp;
070        }
071
072        /**
073         * Returns min value among min values in a hierarchy of simons.
074         *
075         * @return min value among min values
076         */
077        public long getMin() {
078                return min;
079        }
080
081        /**
082         * Returns timestamp in milliseconds when min value was reached.
083         *
084         * @return timestamp in milliseconds when min value was reached
085         */
086        public long getMinTimestamp() {
087                return minTimestamp;
088        }
089
090        void addSample(CounterSample sample) {
091                counter += sample.getCounter();
092                incrementSum += sample.getIncrementSum();
093                decrementSum += sample.getDecrementSum();
094
095                if (min > sample.getMin()) {
096                        min = sample.getMin();
097                        minTimestamp = sample.getMinTimestamp();
098                }
099
100                if (max < sample.getMax()) {
101                        max = sample.getMax();
102                        maxTimestamp = sample.getMaxTimestamp();
103                }
104        }
105
106        @Override
107        public boolean equals(Object o) {
108                if (this == o) return true;
109                if (o == null || getClass() != o.getClass()) return false;
110
111                CounterAggregate that = (CounterAggregate) o;
112
113                if (counter != that.counter) return false;
114                if (decrementSum != that.decrementSum) return false;
115                if (incrementSum != that.incrementSum) return false;
116                if (max != that.max) return false;
117                if (maxTimestamp != that.maxTimestamp) return false;
118                if (min != that.min) return false;
119                if (minTimestamp != that.minTimestamp) return false;
120
121                return true;
122        }
123
124        @Override
125        public int hashCode() {
126                int result = (int) (counter ^ (counter >>> 32));
127                result = 31 * result + (int) (incrementSum ^ (incrementSum >>> 32));
128                result = 31 * result + (int) (decrementSum ^ (decrementSum >>> 32));
129                result = 31 * result + (int) (max ^ (max >>> 32));
130                result = 31 * result + (int) (min ^ (min >>> 32));
131                result = 31 * result + (int) (maxTimestamp ^ (maxTimestamp >>> 32));
132                result = 31 * result + (int) (minTimestamp ^ (minTimestamp >>> 32));
133                return result;
134        }
135
136        @Override
137        public String toString() {
138                return "CounterAggregate{" +
139                        "counter=" + counter +
140                        ", incrementSum=" + incrementSum +
141                        ", decrementSum=" + decrementSum +
142                        ", max=" + SimonUtils.presentMinMaxCount(max) +
143                        ", min=" + SimonUtils.presentMinMaxCount(min) +
144                        ", maxTimestamp=" + SimonUtils.presentTimestamp(maxTimestamp) +
145                        ", minTimestamp=" + SimonUtils.presentTimestamp(minTimestamp) +
146                        '}';
147        }
148}