001package org.javasimon.source;
002
003import java.lang.reflect.Method;
004
005import org.javasimon.Manager;
006
007/**
008 * Base class for Stopwatch sources working on method locations.
009 *
010 * @author gquintana
011 */
012public abstract class AbstractMethodStopwatchSource<T> extends AbstractStopwatchSource<T> {
013
014        /**
015         * Constructor using specific simon manager.
016         *
017         * @param manager Simon manager
018         */
019        public AbstractMethodStopwatchSource(Manager manager) {
020                super(manager);
021        }
022
023        /**
024         * Get target class from location.
025         *
026         * @param location Location
027         * @return Target class
028         */
029        protected abstract Class<?> getTargetClass(T location);
030
031        /**
032         * Get target method from location.
033         *
034         * @param location Location
035         * @return Target method
036         */
037        protected abstract Method getTargetMethod(T location);
038
039        /**
040         * Wraps this data source in a cache.
041         *
042         * @return Cache monitor source
043         */
044        public CachedStopwatchSource<T, Method> cache() {
045                return newCacheStopwatchSource(this);
046        }
047
048        /**
049         * Wraps given stopwatch source in a cache.
050         *
051         * @param stopwatchSource Stopwatch source
052         * @return Cached stopwatch source
053         */
054        private static <T> CachedStopwatchSource<T, Method> newCacheStopwatchSource(final AbstractMethodStopwatchSource<T> stopwatchSource) {
055                return new CachedStopwatchSource<T, Method>(stopwatchSource) {
056                        @Override
057                        protected Method getLocationKey(T location) {
058                                return stopwatchSource.getTargetMethod(location);
059                        }
060                };
061        }
062}