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

se.l4.vibe.probes.SamplerProbes Maven / Gradle / Ivy

There is a newer version: 0.4.0
Show newest version
package se.l4.vibe.probes;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import se.l4.vibe.probes.Sampler.Entry;

/**
 * General probes that work with {@link Sampler sampler}.
 * 
 * @author Andreas Holstenson
 *
 */
public class SamplerProbes
{
	
	private SamplerProbes()
	{
	}
	
	/**
	 * Create a probe for the specified sampler that will keep track of values
	 * over the specified time.
	 * 
	 * @param series
	 * @param duration
	 * @param unit
	 * @param operation
	 * @return
	 */
	public static  Probe forSampler(
		Sampler series,
		long duration, TimeUnit unit,
		SampleOperation operation
	)
	{
		return new TimeLimitedProbe(duration, unit, series, operation);
	}
	
	/**
	 * Create a probe for the specified series that will keep track of values
	 * over the specified time.
	 * 
	 * @param series
	 * @param duration
	 * @param unit
	 * @return
	 */
	public static > Probe forSampler(
		Sampler series,
		long duration, TimeUnit unit
	)
	{
		return forSampler(series, duration, unit, new ModifiableDataOperation());
	}
	
	/**
	 * Create a probe for the specified series that keep track of values
	 * ever sampled.
	 * 
	 * 

* The given operation will not have access to the entire range of * {@link Sampler.Entry entries} in the * {@link SampleOperation#add(Object, java.util.Collection) add method}. * * @param series * @param operation * @return */ public static Probe forSampler( Sampler series, SampleOperation operation ) { return new EternityProbe(series, operation); } /** * Create a probe for the specified series that keep track of values * ever sampled. * *

* This is can be used if the data stored in the series implements * {@link ModifiableData}. * * @param series * @return */ public static > Probe forSampler(Sampler series) { return forSampler(series, new ModifiableDataOperation()); } private static class EternityProbe implements Probe { private final SampleOperation operation; public EternityProbe(Sampler series, SampleOperation operation) { this.operation = operation; series.addListener(new SampleListener() { @Override public void sampleAcquired(SampledProbe probe, Sampler.Entry value) { handleSampleAcquired(value); } }); } protected void handleSampleAcquired(Entry value) { operation.add(value.getValue(), null); } @Override public Output read() { return operation.get(); } } /** * Probe that is time limited based on the input of a * {@link Sampler}. * * @author Andreas Holstenson * */ private static class TimeLimitedProbe implements Probe { private final SampleOperation operation; private final long maxAge; private final List> entries; /** * Create a new time limited probe that will be limited to the specified * time. The operation is used to calculate the value of the probe. * * @param time * @param unit * @param operation */ public TimeLimitedProbe( long time, TimeUnit unit, Sampler series, SampleOperation operation) { maxAge = unit.toMillis(time); this.operation = operation; entries = new LinkedList>(); series.addListener(new SampleListener() { @Override public void sampleAcquired(SampledProbe probe, Sampler.Entry value) { handleSampleAcquired(value); } }); } private void handleSampleAcquired(Sampler.Entry entry) { if(! entries.isEmpty()) { /* * If we have entries check if the first one should be * removed or kept. */ Sampler.Entry firstEntry = entries.get(0); if(firstEntry.getTime() < System.currentTimeMillis() - maxAge) { entries.remove(0); operation.remove(firstEntry.getValue(), entries); } } entries.add(entry); operation.add(entry.getValue(), entries); } @Override public Output read() { return operation.get(); } } private static class ModifiableDataOperation> implements SampleOperation { private T data; @Override public void add(T value, Collection> entries) { if(data == null) { data = value; return; } data = data.add(value); } @Override public void remove(T value, Collection> entries) { if(data == null) return; data = data.remove(value); } @Override public T get() { return data; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy