001package org.javasimon.jmx;
002
003import org.javasimon.utils.SimonUtils;
004
005import java.beans.ConstructorProperties;
006import java.util.Date;
007
008/**
009 * Value object for retrieving data from Counter Simon. Basically, it's
010 * {@link org.javasimon.CounterSample} with added JMX capabilities to be return as object via
011 * MXBean method.
012 * <p/>
013 * Example:
014 * <pre>
015 * SimonManagerMXBean simon = JMX.newMXBeanProxy(..., new ObjectName("domain:type=Simon"), SimonManagerMXBean.class);
016 * CounterSample = simon.getCounterSample("simon.counter");
017 * </pre>
018 *
019 * @author Radovan Sninsky
020 * @since 2.0
021 */
022public final class CounterSample extends org.javasimon.CounterSample {
023
024        /**
025         * JMX constructor. Constructor used by JMX client code to initialize all properties of object
026         * from composite data object.
027         *
028         * @param name Simon's name
029         * @param note note (provided optionally)
030         * @param firstUsage first usage ms timestamp
031         * @param lastUsage last usage ms timestamp
032         * @param counter actual counter value
033         * @param min minimal counter value
034         * @param max maximal counter value
035         * @param minTimestamp time when counter reached minimal value
036         * @param maxTimestamp time when counter reached maximal value
037         * @param incSum sum of all increments
038         * @param decSum sum of all decrements
039         */
040        @ConstructorProperties({"name", "note", "firstUsage", "lastUsage", "counter", "min",
041                "max", "minTimestamp", "maxTimestamp", "incrementSum", "decrementSum"})
042        public CounterSample(String name, String note, long firstUsage, long lastUsage, long counter,
043                             long min, long max, long minTimestamp, long maxTimestamp, long incSum, long decSum) {
044                setName(name);
045                setNote(note);
046                setFirstUsage(firstUsage);
047                setLastUsage(lastUsage);
048
049                setCounter(counter);
050                setMin(min);
051                setMax(max);
052                setMinTimestamp(minTimestamp);
053                setMaxTimestamp(maxTimestamp);
054                setIncrementSum(incSum);
055                setDecrementSum(decSum);
056        }
057
058        /**
059         * Internal, framework constructor for Simon MBean implementation to initialize all properties
060         * by sample obtained from Simon.
061         *
062         * @param sample sample object obtained from Counter Simon
063         */
064        CounterSample(org.javasimon.CounterSample sample) {
065                setName(sample.getName());
066                setNote(sample.getNote());
067                setFirstUsage(sample.getFirstUsage());
068                setLastUsage(sample.getLastUsage());
069
070                setCounter(sample.getCounter());
071                setMin(sample.getMin());
072                setMax(sample.getMax());
073                setMinTimestamp(sample.getMinTimestamp());
074                setMaxTimestamp(sample.getMaxTimestamp());
075                setIncrementSum(sample.getIncrementSum());
076                setDecrementSum(sample.getDecrementSum());
077        }
078
079        /**
080         * Timestamp of the first usage from the sampled Simon as a formatted string.
081         *
082         * @return Simon's first usage timestamp as string
083         */
084        public String getFirstUsageAsString() {
085                return SimonUtils.presentTimestamp(getFirstUsage());
086        }
087
088        /**
089         * Timestamp of the first usage from the sampled Simon as a formatted date.
090         *
091         * @return Simon's first usage timestamp as date
092         */
093        public Date getFirstUsageAsDate() {
094                return new Date(getFirstUsage());
095        }
096
097        /**
098         * Timestamp of the last usage from the sampled Simon as a formatted string.
099         *
100         * @return Simon's last usage timestamp as string
101         */
102        public String getLastUsageAsString() {
103                return SimonUtils.presentTimestamp(getLastUsage());
104        }
105
106        /**
107         * Timestamp of the last usage from the sampled Simon as a date.
108         *
109         * @return Simon's last usage timestamp as date
110         */
111        public Date getLastUsageAsDate() {
112                return new Date(getLastUsage());
113        }
114
115        /**
116         * Returns ms timestamp when the min value was measured as a formatted string.
117         *
118         * @return ms timestamp of the min value measurement as string
119         */
120        public final String getMinTimestampAsString() {
121                return SimonUtils.presentTimestamp(getMinTimestamp());
122        }
123
124        /**
125         * Returns ms timestamp when the min value was measured as a formatted date.
126         *
127         * @return ms timestamp of the min value measurement as date
128         */
129        public final Date getMinTimestampAsDate() {
130                return new Date(getMinTimestamp());
131        }
132
133        /**
134         * Returns ms timestamp when the max value was measured as a formatted string.
135         *
136         * @return ms timestamp of the max value measurement as string
137         */
138        public final String getMaxTimestampAsString() {
139                return SimonUtils.presentTimestamp(getMaxTimestamp());
140        }
141
142        /**
143         * Returns ms timestamp when the max value was measured as a formatted date.
144         *
145         * @return ms timestamp of the max value measurement as date
146         */
147        public final Date getMaxTimestampAsDate() {
148                return new Date(getMaxTimestamp());
149        }
150}