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

jadex.bridge.sensor.unit.TimeUnit 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.time.Duration;

/**
 *  The time unit.
 */
public enum TimeUnit implements IConvertableUnit, IPrettyPrintUnit
{
	MILLIS, SECS, MINS, HOURS, DAYS;
	
	/**
	 *  Convert to a known unit.
	 */
	public Long convert(Long value)
	{
		if(value!=null)
		{
			long ret = value;
			
			if(TimeUnit.SECS.equals(this))
			{
				ret = Math.round(ret/1000d);
			}
			else if(TimeUnit.MINS.equals(this))
			{
				ret = Math.round(ret/1000d/60);
			}
			else if(TimeUnit.HOURS.equals(this))
			{
				ret = Math.round(ret/1000d/60/60);
			}
			else if(TimeUnit.DAYS.equals(this))
			{
				ret = Math.round(ret/1000d/60/24);
			}
			return Long.valueOf(ret);
		}
		else
		{
			return null;
		}
	}
	
	/**
	 *  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)
	{
		Duration d = Duration.ofMillis(value);
		return d.toString().substring(2).replaceAll("(\\d[HMS])(?!$)", " $1 ").toLowerCase();
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy