All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javasimon.source.AbstractMethodStopwatchSource Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
package org.javasimon.source;

import java.lang.reflect.Method;

import org.javasimon.Manager;

/**
 * Base class for Stopwatch sources working on method locations.
 *
 * @author gquintana
 */
public abstract class AbstractMethodStopwatchSource extends AbstractStopwatchSource {

	/**
	 * Constructor using specific simon manager.
	 *
	 * @param manager Simon manager
	 */
	public AbstractMethodStopwatchSource(Manager manager) {
		super(manager);
	}

	/**
	 * Get target class from location.
	 *
	 * @param location Location
	 * @return Target class
	 */
	protected abstract Class getTargetClass(T location);

	/**
	 * Get target method from location.
	 *
	 * @param location Location
	 * @return Target method
	 */
	protected abstract Method getTargetMethod(T location);

	/**
	 * Wraps this data source in a cache.
	 *
	 * @return Cache monitor source
	 */
	public CachedStopwatchSource cache() {
		return newCacheStopwatchSource(this);
	}

	/**
	 * Wraps given stopwatch source in a cache.
	 *
	 * @param stopwatchSource Stopwatch source
	 * @return Cached stopwatch source
	 */
	private static  CachedStopwatchSource newCacheStopwatchSource(final AbstractMethodStopwatchSource stopwatchSource) {
		return new CachedStopwatchSource(stopwatchSource) {
			@Override
			protected Method getLocationKey(T location) {
				return stopwatchSource.getTargetMethod(location);
			}
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy