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

se.l4.vibe.probes.Sum 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.concurrent.TimeUnit;

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

/**
 * Probes that will calculate the sum of other {@link Probe probes} and
 * {@link Sampler time series}.
 * 
 * @author Andreas Holstenson
 *
 */
public class Sum
{
	private Sum()
	{
	}
	
	/**
	 * Create a probe that will calculate the sum of the entire series as a
	 * {@link Long}.
	 * 
	 * @param series
	 * @return
	 */
	public static  Probe forSamplerAsLong(Sampler series)
	{
		return SamplerProbes.forSampler(series, new SumLongOperation(ValueReaders.same()));
	}
	
	/**
	 * Create a probe that will calculate the sum of the entire series as a
	 * {@link Long}.
	 * 
	 * @param series
	 * @return
	 */
	public static  Probe forSamplerAsLong(Sampler series, ValueReader reader)
	{
		return SamplerProbes.forSampler(series, new SumLongOperation(reader));
	}
	
	/**
	 * Create a probe that will calculate the sum of the entire series as a
	 * {@link Double}.
	 * 
	 * @param series
	 * @return
	 */
	public static  Probe forSamplerAsDouble(Sampler series)
	{
		return SamplerProbes.forSampler(series, new SumDoubleOperation(ValueReaders.same()));
	}
	
	/**
	 * Create a probe that will calculate the sum of the entire series as a
	 * {@link Double}.
	 * 
	 * @param series
	 * @return
	 */
	public static  Probe forSamplerAsDouble(Sampler series, ValueReader reader)
	{
		return SamplerProbes.forSampler(series, new SumDoubleOperation(reader));
	}
	
	/**
	 * Create a probe that will calculate the sum as a sliding window with
	 * the specified duration.
	 * 
	 * @param series
	 * @param duration
	 * @param unit
	 * @return
	 */
	public static  Probe forSamplerAsLong(
		Sampler series,
		long duration, 
		TimeUnit unit
	)
	{
		return SamplerProbes.forSampler(series, duration, unit, new SumLongOperation(ValueReaders.same()));
	}
	
	/**
	 * Create a probe that will calculate the sum as a sliding window with
	 * the specified duration.
	 * 
	 * @param series
	 * @param duration
	 * @param unit
	 * @return
	 */
	public static  Probe forSamplerAsLong(
		Sampler series,
		ValueReader reader,
		long duration, 
		TimeUnit unit
	)
	{
		return SamplerProbes.forSampler(series, duration, unit, new SumLongOperation(reader));
	}
	
	/**
	 * Create a probe that will calculate the sum as a sliding window with
	 * the specified duration.
	 * 
	 * @param series
	 * @param duration
	 * @param unit
	 * @return
	 */
	public static  Probe forSamplerAsDouble(
		Sampler series,
		long duration, 
		TimeUnit unit
	)
	{
		return SamplerProbes.forSampler(series, duration, unit, new SumDoubleOperation(ValueReaders.same()));
	}
	
	/**
	 * Create a probe that will calculate the sum as a sliding window with
	 * the specified duration.
	 * 
	 * @param series
	 * @param duration
	 * @param unit
	 * @return
	 */
	public static  Probe forSamplerAsDouble(
		Sampler series,
		ValueReader reader,
		long duration, 
		TimeUnit unit
	)
	{
		return SamplerProbes.forSampler(series, duration, unit, new SumDoubleOperation(reader));
	}
	
	private static class SumDoubleOperation
		implements SampleOperation
	{
		private final ValueReader reader;
		
		private double sum;
		
		public SumDoubleOperation(ValueReader reader)
		{
			this.reader = reader;
		}

		@Override
		public void remove(T value, Collection> entries)
		{
			sum -= reader.read(value).doubleValue();
		}

		@Override
		public void add(T value, Collection> entries)
		{
			sum += reader.read(value).doubleValue();
		}

		@Override
		public Double get()
		{
			return sum;
		}
	}
	
	private static class SumLongOperation
		implements SampleOperation
	{
		private final ValueReader reader;
		
		private long sum;
		
		public SumLongOperation(ValueReader reader)
		{
			this.reader = reader;
		}
	
		@Override
		public void remove(T value, Collection> entries)
		{
			sum -= reader.read(value).longValue();
		}
	
		@Override
		public void add(T value, Collection> entries)
		{
			sum += reader.read(value).longValue();
		}
	
		@Override
		public Long get()
		{
			return sum;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy