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

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

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

/**
 * Probe that provides a ratio between two values.
 * 
 * @author Andreas Holstenson
 *
 */
public class Ratio
{
	private Ratio()
	{
	}
	
	/**
	 * Create a new ratio between the two probes. 
	 * 
	 * 

* Note that the ratio will call {@link SampledProbe#sample()}, which means * that the probes used should not be used as part of a {@link Sampler}. * * @param probe1 * @param probe2 * @return */ public static SampledProbe between( SampledProbe a, SampledProbe b) { return new SampledRatio(a, b); } /** * Create a ratio between a probe and a static value. * *

* Note that the ratio will call {@link SampledProbe#sample()}, which means * that the probes used should not be used as part of a {@link Sampler}. * * @param probe1 * @param number * @return */ public static SampledProbe between( SampledProbe a, double b) { return new SampledRatio(a, new ConstantProbe(b)); } /** * Create a ratio between a static value and a probe. * *

* Note that the ratio will call {@link SampledProbe#sample()}, which means * that the probes used should not be used as part of a {@link Sampler}. * * @param number * @param probe2 * @return */ public static SampledProbe between( double a, SampledProbe b) { return new SampledRatio(new ConstantProbe(a), b); } /** * Create a new ratio between the two probes. * * @param probe1 * @param probe2 * @return */ public static Probe between( Probe a, Probe b) { return new NormalRatio(a, b); } /** * Create a ratio between a probe and a static value. * * @param probe1 * @param number * @return */ public static Probe between( Probe a, double b) { return new NormalRatio(a, new ConstantProbe(b)); } /** * Create a ratio between a static value and a probe. * * @param number * @param probe2 * @return */ public static Probe between( double a, Probe b) { return new NormalRatio(new ConstantProbe(a), b); } private static class SampledRatio extends AbstractSampledProbe { private final SampledProbe probe1; private final SampledProbe probe2; public SampledRatio(SampledProbe probe1, SampledProbe probe2) { this.probe1 = probe1; this.probe2 = probe2; } @Override public Double peek() { return probe1.peek().doubleValue() / probe2.peek().doubleValue(); } @Override protected Double sample0() { return probe1.sample().doubleValue() / probe2.sample().doubleValue(); } } private static class NormalRatio implements Probe { private final Probe probe1; private final Probe probe2; public NormalRatio(Probe probe1, Probe probe2) { this.probe1 = probe1; this.probe2 = probe2; } @Override public Double read() { return probe1.read().doubleValue() / probe2.read().doubleValue(); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy