jadex.bridge.sensor.unit.TimeUnit Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-platform-bridge Show documentation
Show all versions of jadex-platform-bridge Show documentation
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.
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();
}
}