Skip to content

Commit

Permalink
Added support for MetricPrefix combined with byte and use byte as def…
Browse files Browse the repository at this point in the history
…ault instead of octet (#1838)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp authored Nov 19, 2020
1 parent 63ec434 commit ddaf0d4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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");
Expand All @@ -216,19 +230,25 @@ 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");
SimpleUnitFormat.getInstance().label(KILOVAR_HOUR, "kvarh");
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");
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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;
Expand Down Expand Up @@ -328,12 +329,24 @@ public void testDataAmount() {
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
Expand Down

0 comments on commit ddaf0d4

Please sign in to comment.