001package org.javasimon.source;
002
003import org.javasimon.Manager;
004import org.javasimon.Split;
005import org.javasimon.Stopwatch;
006
007/**
008 * Base implementation for {@link MonitorSource} producings stopwatches.
009 *
010 * @author gquintana
011 */
012public abstract class AbstractStopwatchSource<T> implements StopwatchSource<T> {
013        /**
014         * Simon manager used for producing Stopwatches.
015         */
016        private final Manager manager;
017
018        /**
019         * Constructor with {@link Manager}.
020         *
021         * @param manager Simon manager used for producing Stopwatches
022         */
023        public AbstractStopwatchSource(Manager manager) {
024                this.manager = manager;
025        }
026
027        @Override
028        public Manager getManager() {
029                return manager;
030        }
031
032        /**
033         * Default implementation returns always true.
034         *
035         * @return always true
036         */
037        @Override
038        public boolean isMonitored(T location) {
039                return true;
040        }
041
042        /**
043         * Get monitor name for the given location.
044         */
045        protected abstract String getMonitorName(T location);
046
047        /**
048         * Provide a Stopwatch for given location.
049         *
050         * @param location Location
051         * @return Stopwatch
052         */
053        @Override
054        public Stopwatch getMonitor(T location) {
055                return manager.getStopwatch(getMonitorName(location));
056        }
057
058        @Override
059        public Split start(T location) {
060                if (isMonitored(location)) {
061                        return getMonitor(location).start();
062                }
063                return Split.DISABLED;
064        }
065}