Added support for MetricPrefix combined with byte and use byte as default instead of octet (#1838)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
pull/1672/head
Christoph Weitkamp 2020-11-19 19:33:10 +01:00 committed by GitHub
parent 63ec4342f2
commit ddaf0d46fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 7 deletions

View File

@ -180,15 +180,28 @@ public final class SmartHomeUnits extends CustomUnits {
public static final Unit<VolumetricFlowRate> CUBICMETRE_PER_DAY = addUnit(
new ProductUnit<VolumetricFlowRate>(Units.CUBIC_METRE.divide(Units.DAY)));
public static final Unit<DataAmount> BIT = addUnit(new AlternateUnit<>(ONE, "bit"));
public static final Unit<DataAmount> MEGABIT = addUnit(MetricPrefix.MEGA(BIT));
public static final Unit<DataAmount> KILOBIT = addUnit(MetricPrefix.KILO(BIT));
public static final Unit<DataAmount> MEGABIT = addUnit(MetricPrefix.MEGA(BIT));
public static final Unit<DataAmount> GIGABIT = addUnit(MetricPrefix.GIGA(BIT));
public static final Unit<DataAmount> TERABIT = addUnit(MetricPrefix.TERA(BIT));
public static final Unit<DataAmount> PETABIT = addUnit(MetricPrefix.PETA(BIT));
public static final Unit<DataAmount> BYTE = addUnit(BIT.multiply(8));
public static final Unit<DataAmount> OCTET = BYTE;
public static final Unit<DataAmount> OCTET = addUnit(BIT.multiply(8));
public static final Unit<DataAmount> KILOBYTE = addUnit(MetricPrefix.KILO(BYTE));
public static final Unit<DataAmount> MEGABYTE = addUnit(MetricPrefix.MEGA(BYTE));
public static final Unit<DataAmount> GIGABYTE = addUnit(MetricPrefix.GIGA(BYTE));
public static final Unit<DataAmount> TERABYTE = addUnit(MetricPrefix.TERA(BYTE));
public static final Unit<DataAmount> PETABYTE = addUnit(MetricPrefix.PETA(BYTE));
public static final Unit<DataAmount> KIBIBYTE = addUnit(BinaryPrefix.KIBI(BYTE));
public static final Unit<DataAmount> MEBIBYTE = addUnit(BinaryPrefix.MEBI(BYTE));
public static final Unit<DataAmount> GIBIBYTE = addUnit(BinaryPrefix.GIBI(BYTE));
public static final Unit<DataAmount> TEBIBYTE = addUnit(BinaryPrefix.TEBI(BYTE));
public static final Unit<DataAmount> PEBIBYTE = addUnit(BinaryPrefix.PEBI(BYTE));
public static final Unit<DataAmount> KIBIOCTET = addUnit(BinaryPrefix.KIBI(OCTET));
public static final Unit<DataAmount> MEBIOCTET = addUnit(BinaryPrefix.MEBI(OCTET));
public static final Unit<DataAmount> GIBIOCTET = addUnit(BinaryPrefix.GIBI(OCTET));
public static final Unit<DataAmount> TEBIOCTET = addUnit(BinaryPrefix.TEBI(OCTET));
public static final Unit<DataAmount> PEBIOCTET = addUnit(BinaryPrefix.PEBI(OCTET));
public static final Unit<DataTransferRate> BIT_PER_SECOND = addUnit(
new ProductUnit<DataTransferRate>(BIT.divide(Units.SECOND)));
public static final Unit<DataTransferRate> KILOBIT_PER_SECOND = addUnit(MetricPrefix.KILO(BIT_PER_SECOND));
@ -207,6 +220,7 @@ public final class SmartHomeUnits extends CustomUnits {
SimpleUnitFormat.getInstance().label(BIT, BIT.getSymbol());
SimpleUnitFormat.getInstance().label(BIT_PER_SECOND, "bit/s");
SimpleUnitFormat.getInstance().label(BYTE, "B");
SimpleUnitFormat.getInstance().alias(BYTE, "o");
SimpleUnitFormat.getInstance().label(CUBICMETRE_PER_DAY, "m³/d");
SimpleUnitFormat.getInstance().label(CUBICMETRE_PER_HOUR, "m³/h");
SimpleUnitFormat.getInstance().label(CUBICMETRE_PER_MINUTE, "m³/min");
@ -216,11 +230,15 @@ public final class SmartHomeUnits extends CustomUnits {
SimpleUnitFormat.getInstance().label(DEGREE_ANGLE, "°");
SimpleUnitFormat.getInstance().label(DEUTSCHE_HAERTE, "°dH");
SimpleUnitFormat.getInstance().label(DOBSON_UNIT, "DU");
SimpleUnitFormat.getInstance().label(GIBIOCTET, "Gio");
SimpleUnitFormat.getInstance().label(GIGABYTE, "GB");
SimpleUnitFormat.getInstance().label(GIBIBYTE, "GiB");
SimpleUnitFormat.getInstance().alias(GIBIBYTE, "Gio");
SimpleUnitFormat.getInstance().label(GIGABIT, "Gbit");
SimpleUnitFormat.getInstance().label(GIGABIT_PER_SECOND, "Gbit/s");
SimpleUnitFormat.getInstance().label(IRRADIANCE, "W/m²");
SimpleUnitFormat.getInstance().label(KIBIOCTET, "Kio");
SimpleUnitFormat.getInstance().label(KILOBYTE, "KB");
SimpleUnitFormat.getInstance().label(KIBIBYTE, "KiB");
SimpleUnitFormat.getInstance().alias(KIBIBYTE, "Kio");
SimpleUnitFormat.getInstance().label(KILOBIT, "kbit");
SimpleUnitFormat.getInstance().label(KILOBIT_PER_SECOND, "kbit/s");
SimpleUnitFormat.getInstance().label(KILOVAR, "kvar");
@ -228,7 +246,9 @@ public final class SmartHomeUnits extends CustomUnits {
SimpleUnitFormat.getInstance().label(KILOWATT_HOUR, "kWh");
SimpleUnitFormat.getInstance().label(KNOT, KNOT.getSymbol());
SimpleUnitFormat.getInstance().label(LITRE_PER_MINUTE, "l/min");
SimpleUnitFormat.getInstance().label(MEBIOCTET, "Mio");
SimpleUnitFormat.getInstance().label(MEGABYTE, "MB");
SimpleUnitFormat.getInstance().label(MEBIBYTE, "MiB");
SimpleUnitFormat.getInstance().alias(MEBIBYTE, "Mio");
SimpleUnitFormat.getInstance().label(MEGABIT, "Mbit");
SimpleUnitFormat.getInstance().label(MEGABIT_PER_SECOND, "Mbit/s");
SimpleUnitFormat.getInstance().label(MEGAWATT_HOUR, "MWh");
@ -237,11 +257,17 @@ public final class SmartHomeUnits extends CustomUnits {
SimpleUnitFormat.getInstance().label(MILLIAMPERE_HOUR, "mAh");
SimpleUnitFormat.getInstance().label(MILLIBAR, "mbar");
SimpleUnitFormat.getInstance().label(MILLIMETRE_OF_MERCURY, MILLIMETRE_OF_MERCURY.getSymbol());
SimpleUnitFormat.getInstance().label(OCTET, "o");
SimpleUnitFormat.getInstance().label(PARTS_PER_BILLION, "ppb");
SimpleUnitFormat.getInstance().label(PARTS_PER_MILLION, "ppm");
SimpleUnitFormat.getInstance().label(PETABYTE, "PB");
SimpleUnitFormat.getInstance().label(PEBIBYTE, "PiB");
SimpleUnitFormat.getInstance().alias(PEBIBYTE, "Pio");
SimpleUnitFormat.getInstance().label(PETABIT, "Pbit");
SimpleUnitFormat.getInstance().label(STANDARD_GRAVITY, "gₙ");
SimpleUnitFormat.getInstance().label(SIEMENS_PER_METRE, "S/m");
SimpleUnitFormat.getInstance().label(TERABYTE, "TB");
SimpleUnitFormat.getInstance().label(TEBIBYTE, "TiB");
SimpleUnitFormat.getInstance().alias(TEBIBYTE, "Tio");
SimpleUnitFormat.getInstance().label(TERABIT, "Tbit");
SimpleUnitFormat.getInstance().label(TERABIT_PER_SECOND, "Tbit/s");
SimpleUnitFormat.getInstance().label(VAR, "var");

View File

@ -34,6 +34,7 @@ import org.openhab.core.library.dimension.DataAmount;
import org.openhab.core.library.dimension.DataTransferRate;
import org.openhab.core.library.dimension.Density;
import org.openhab.core.library.dimension.Intensity;
import org.openhab.core.library.unit.BinaryPrefix;
import org.openhab.core.library.unit.MetricPrefix;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.SmartHomeUnits;
@ -328,12 +329,24 @@ public class QuantityTypeTest {
QuantityType<DataAmount> amount = new QuantityType<>("8 bit");
QuantityType<DataAmount> octet = amount.toUnit(SmartHomeUnits.BYTE);
assertEquals(1, octet.byteValue());
QuantityType<DataAmount> bytes = new QuantityType<>("1 B");
assertEquals("1 B", bytes.toString());
QuantityType<DataAmount> bits = bytes.toUnit(SmartHomeUnits.BIT);
assertEquals(8, bits.byteValue());
bytes = new QuantityType<>("1 MB");
assertEquals("1 MB", bytes.toString());
bytes = new QuantityType<DataAmount>(1, MetricPrefix.MEGA(SmartHomeUnits.BYTE));
assertEquals("1 MB", bytes.toString());
bytes = new QuantityType<>("1 GiB");
assertEquals("1 GiB", bytes.toString());
bytes = new QuantityType<DataAmount>(1, BinaryPrefix.GIBI(SmartHomeUnits.BYTE));
assertEquals("1 GiB", bytes.toString());
QuantityType<DataAmount> bigAmount = new QuantityType<>("1 Kio");
QuantityType<DataAmount> octets = bigAmount.toUnit(SmartHomeUnits.OCTET);
assertEquals(1024, octets.intValue());
QuantityType<DataAmount> hugeAmount = new QuantityType<>("1024Gio");
QuantityType<DataAmount> lotOfOctets = hugeAmount.toUnit(SmartHomeUnits.OCTET);
assertEquals("1099511627776 o", lotOfOctets.toString());
assertEquals("1099511627776 o", lotOfOctets.format("%d o"));
}
@Test