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

de.schegge.bitrate.BitrateUnits Maven / Gradle / Ivy

The newest version!
package de.schegge.bitrate;

import tech.units.indriya.AbstractSystemOfUnits;
import tech.units.indriya.AbstractUnit;
import tech.units.indriya.format.SimpleUnitFormat;
import tech.units.indriya.unit.BaseUnit;
import tech.units.indriya.unit.ProductUnit;
import tech.units.indriya.unit.UnitDimension;
import tech.units.indriya.unit.Units;

import javax.measure.MetricPrefix;
import javax.measure.Prefix;
import javax.measure.Unit;

public class BitrateUnits extends AbstractSystemOfUnits {
    final static BitrateUnits INSTANCE = new BitrateUnits();

    private static final Unit BIT = addUnit(new BaseUnit<>("bit", "Bit", UnitDimension.NONE));
    public static final Unit BITS_PER_S = getBandwidthProductUnit(null);
    public static final Unit KILOBITS_PER_S = getBandwidthProductUnit(MetricPrefix.KILO);
    public static final Unit MEGABITS_PER_S = getBandwidthProductUnit(MetricPrefix.MEGA);
    public static final Unit GIGABITS_PER_S = getBandwidthProductUnit(MetricPrefix.GIGA);
    public static final Unit TERABITS_PER_S = getBandwidthProductUnit(MetricPrefix.TERA);

    private static > U addUnit(U unit) {
        INSTANCE.units.add(unit);
        INSTANCE.quantityToUnit.put(Information.class, unit);
        return unit;
    }

    private static ProductUnit getBandwidthProductUnit(Prefix prefix) {
        Unit informationUnit = prefix == null ? BitrateUnits.BIT : BitrateUnits.BIT.prefix(prefix);
        ProductUnit unit = new ProductUnit<>(informationUnit.divide(Units.SECOND));
        SimpleUnitFormat.getInstance().label(unit, prefix == null ? "bit/s" : prefix.getSymbol() + "bit/s");
        return unit;
    }

    @Override
    public String getName() {
        return BitrateUnits.class.getSimpleName();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy