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

jadex.bridge.sensor.unit.MemoryUnit Maven / Gradle / Ivy

Go to download

Jadex bridge is a base package for kernels and platforms, i.e., it is used by both and provides commonly used interfaces and classes for active components and their management.

There is a newer version: 4.0.267
Show newest version
package jadex.bridge.sensor.unit;

import java.text.DecimalFormat;

/**
 *  Memory unit.
 */
public enum MemoryUnit implements IConvertableUnit, IPrettyPrintUnit
{
	B, KB, MB, GB, TB;
	
	protected static final String[] units = new String[]{"Bi", "kiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
	
	protected static final DecimalFormat format = new DecimalFormat("#,##0.#");
		
	/**
	 *  Convert to a known unit.
	 */
	public Long convert(Long value)
	{
		long ret = value;
		
		if(MemoryUnit.KB.equals(this))
		{
			ret = Math.round(ret/1024d);
		}
		else if(MemoryUnit.MB.equals(this))
		{
			ret = Math.round(ret/1024d/1024);
		}
		else if(MemoryUnit.GB.equals(this))
		{
			ret = Math.round(ret/1024d/1024/1024);
		}
		else if(MemoryUnit.TB.equals(this))
		{
			ret = Math.round(ret/1024d/1024/1024/1024); //1024*1024*1024*1024; -> 0 :-(
		}
		return ret;
	}
	
	/**
	 *  Pretty print a value according to the underlying unit to a string.
	 *  @param value The value.
	 *  @return The pretty printed string.
	 */
	public String prettyPrint(Long value)
	{
		if(value <= 0) return "0";
		int dg = (int)(Math.log10(value)/Math.log10(1024));
		return format.format(value/Math.pow(1024, dg)) + " " + units[dg];
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy