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

se.l4.vibe.percentile.CombinedProbes Maven / Gradle / Ivy

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

import java.util.HashMap;
import java.util.Map;

import se.l4.vibe.mapping.KeyValueMappable;
import se.l4.vibe.mapping.KeyValueReceiver;
import se.l4.vibe.probes.AbstractSampledProbe;
import se.l4.vibe.probes.SampledProbe;

/**
 * Utility for combining several probes into a single one.
 * 
 * @author Andreas Holstenson
 *
 */
public class CombinedProbes
{
	private CombinedProbes()
	{
	}
	
	public static  Builder builder()
	{
		return new Builder<>();
	}
	
	/**
	 * Builder for probes.
	 * 
	 * @author Andreas Holstenson
	 *
	 * @param 
	 */
	public static class Builder
	{
		private final Map> probes;
		
		private Builder()
		{
			probes = new HashMap<>();
		}
		
		public Builder add(String name, SampledProbe probe)
		{
			probes.put(name, probe);
			return this;
		}
		
		public SampledProbe> create()
		{
			return new CombinedProbe<>(probes);
		}
	}
	
	/**
	 * Probe that takes values from other probes and returns a {@link CombinedData}.
	 * 
	 * @author Andreas Holstenson
	 *
	 * @param 
	 */
	private static class CombinedProbe
		extends AbstractSampledProbe>
	{
		private final Map> probes;
		
		public CombinedProbe(Map> probes)
		{
			this.probes = probes;
		}
		
		@Override
		public CombinedData peek()
		{
			Map values = new HashMap<>();
			for(Map.Entry> p : probes.entrySet())
			{
				values.put(p.getKey(), p.getValue().peek());
			}
			return new CombinedData<>(values);
		}
		
		@Override
		protected CombinedData sample0()
		{
			Map values = new HashMap<>();
			for(Map.Entry> p : probes.entrySet())
			{
				values.put(p.getKey(), p.getValue().sample());
			}
			return new CombinedData<>(values);
		}
	}
	
	/**
	 * Combination of data from several probes.
	 * 
	 * @author Andreas Holstenson
	 *
	 * @param 
	 */
	public static class CombinedData
		implements KeyValueMappable
	{
		private final Map values;

		public CombinedData(Map values)
		{
			this.values = values;
		}
		
		public T get(String id)
		{
			return values.get(id);
		}

		@Override
		public void mapToKeyValues(KeyValueReceiver receiver)
		{
			for(Map.Entry e : values.entrySet())
			{
				receiver.add(e.getKey(), e.getValue());
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy