001package io.prometheus.metrics.model.snapshots;
002
003/**
004 * Some pre-defined units for convenience. You can create your own units with
005 * <pre>
006 *     new Unit("myUnit");
007 * </pre>
008 * Note that in Prometheus, units are largely based on SI base units
009 * (seconds, bytes, joules, grams, meters, ratio, volts, amperes, and celsius).
010 */
011public class Unit {
012
013    private final String name;
014
015    public static final Unit RATIO = new Unit("ratio");
016    public static final Unit SECONDS = new Unit("seconds");
017    public static final Unit BYTES = new Unit("bytes");
018    public static final Unit CELSIUS = new Unit("celsius");
019    public static final Unit JOULES = new Unit("joules");
020    public static final Unit GRAMS = new Unit("grams");
021    public static final Unit METERS = new Unit("meters");
022    public static final Unit VOLTS = new Unit("volts");
023    public static final Unit AMPERES = new Unit("amperes");
024
025    public Unit(String name) {
026        this.name = name;
027        if (name == null) {
028            throw new NullPointerException("Unit name cannot be null.");
029        }
030        if (name.isEmpty()) {
031            throw new IllegalArgumentException("Unit name cannot be empty.");
032        }
033    }
034
035    @Override
036    public String toString() {
037        return name;
038    }
039
040    public static double nanosToSeconds(long nanos) {
041        return nanos / 1E9;
042    }
043
044    public static double millisToSeconds(long millis) {
045        return millis / 1E3;
046    }
047
048    public static double secondsToMillis(double seconds) {
049        return seconds * 1E3;
050    }
051
052    public static double kiloBytesToBytes(double kilobytes) {
053        return kilobytes * 1024;
054    }
055}