mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Fix DataAmount dimension units (#3208)
Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
@@ -166,6 +166,8 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Measurement -->
|
<!-- Measurement -->
|
||||||
|
<!-- Attention: before upgrading these dependencies check that the dimension X is still not used, otherwise change the
|
||||||
|
dimension label for org.openhab.core.library.unit.Units.BIT -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.measure</groupId>
|
<groupId>javax.measure</groupId>
|
||||||
<artifactId>unit-api</artifactId>
|
<artifactId>unit-api</artifactId>
|
||||||
|
|||||||
@@ -64,8 +64,10 @@ import tech.units.indriya.function.ExpConverter;
|
|||||||
import tech.units.indriya.function.LogConverter;
|
import tech.units.indriya.function.LogConverter;
|
||||||
import tech.units.indriya.function.MultiplyConverter;
|
import tech.units.indriya.function.MultiplyConverter;
|
||||||
import tech.units.indriya.unit.AlternateUnit;
|
import tech.units.indriya.unit.AlternateUnit;
|
||||||
|
import tech.units.indriya.unit.BaseUnit;
|
||||||
import tech.units.indriya.unit.ProductUnit;
|
import tech.units.indriya.unit.ProductUnit;
|
||||||
import tech.units.indriya.unit.TransformedUnit;
|
import tech.units.indriya.unit.TransformedUnit;
|
||||||
|
import tech.units.indriya.unit.UnitDimension;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegate common units to {@link Units} to hide this dependency from the rest of openHAB.
|
* Delegate common units to {@link Units} to hide this dependency from the rest of openHAB.
|
||||||
@@ -188,7 +190,7 @@ public final class Units extends CustomUnits {
|
|||||||
tech.units.indriya.unit.Units.CUBIC_METRE.divide(tech.units.indriya.unit.Units.HOUR)));
|
tech.units.indriya.unit.Units.CUBIC_METRE.divide(tech.units.indriya.unit.Units.HOUR)));
|
||||||
public static final Unit<VolumetricFlowRate> CUBICMETRE_PER_DAY = addUnit(new ProductUnit<VolumetricFlowRate>(
|
public static final Unit<VolumetricFlowRate> CUBICMETRE_PER_DAY = addUnit(new ProductUnit<VolumetricFlowRate>(
|
||||||
tech.units.indriya.unit.Units.CUBIC_METRE.divide(tech.units.indriya.unit.Units.DAY)));
|
tech.units.indriya.unit.Units.CUBIC_METRE.divide(tech.units.indriya.unit.Units.DAY)));
|
||||||
public static final Unit<DataAmount> BIT = addUnit(new AlternateUnit<>(ONE, "bit"));
|
public static final Unit<DataAmount> BIT = addUnit(new BaseUnit<>("bit", UnitDimension.parse('X')));
|
||||||
public static final Unit<DataAmount> KILOBIT = addUnit(MetricPrefix.KILO(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> MEGABIT = addUnit(MetricPrefix.MEGA(BIT));
|
||||||
public static final Unit<DataAmount> GIGABIT = addUnit(MetricPrefix.GIGA(BIT));
|
public static final Unit<DataAmount> GIGABIT = addUnit(MetricPrefix.GIGA(BIT));
|
||||||
@@ -245,8 +247,14 @@ public final class Units extends CustomUnits {
|
|||||||
SimpleUnitFormat.getInstance().label(GIGABIT, "Gbit");
|
SimpleUnitFormat.getInstance().label(GIGABIT, "Gbit");
|
||||||
SimpleUnitFormat.getInstance().label(GIGABIT_PER_SECOND, "Gbit/s");
|
SimpleUnitFormat.getInstance().label(GIGABIT_PER_SECOND, "Gbit/s");
|
||||||
SimpleUnitFormat.getInstance().label(IRRADIANCE, "W/m²");
|
SimpleUnitFormat.getInstance().label(IRRADIANCE, "W/m²");
|
||||||
SimpleUnitFormat.getInstance().label(KILOBYTE, "KB");
|
SimpleUnitFormat.getInstance().label(KILOBYTE, "kB");
|
||||||
SimpleUnitFormat.getInstance().label(KIBIBYTE, "KiB");
|
// "KB" is wrong, but we keep it for backward compatibility
|
||||||
|
SimpleUnitFormat.getInstance().alias(KILOBYTE, "KB");
|
||||||
|
SimpleUnitFormat.getInstance().label(KIBIBYTE, "kiB");
|
||||||
|
// "KiB" is wrong, but we keep it for backward compatibility
|
||||||
|
SimpleUnitFormat.getInstance().alias(KIBIBYTE, "KiB");
|
||||||
|
SimpleUnitFormat.getInstance().alias(KIBIBYTE, "kio");
|
||||||
|
// "Kio" is wrong, but we keep it for backward compatibility
|
||||||
SimpleUnitFormat.getInstance().alias(KIBIBYTE, "Kio");
|
SimpleUnitFormat.getInstance().alias(KIBIBYTE, "Kio");
|
||||||
SimpleUnitFormat.getInstance().label(KILOBIT, "kbit");
|
SimpleUnitFormat.getInstance().label(KILOBIT, "kbit");
|
||||||
SimpleUnitFormat.getInstance().label(KILOBIT_PER_SECOND, "kbit/s");
|
SimpleUnitFormat.getInstance().label(KILOBIT_PER_SECOND, "kbit/s");
|
||||||
|
|||||||
+55
@@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2010-2022 Contributors to the openHAB project
|
||||||
|
*
|
||||||
|
* See the NOTICE file(s) distributed with this work for additional
|
||||||
|
* information.
|
||||||
|
*
|
||||||
|
* This program and the accompanying materials are made available under the
|
||||||
|
* terms of the Eclipse Public License 2.0 which is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-2.0
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: EPL-2.0
|
||||||
|
*/
|
||||||
|
package org.openhab.core.library.dimension;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.openhab.core.library.types.QuantityType;
|
||||||
|
import org.openhab.core.library.unit.Units;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link DataAmountTest} holds tests for {@link DataAmount}
|
||||||
|
*
|
||||||
|
* @author Jan N. Klug - Initial contribution
|
||||||
|
*/
|
||||||
|
@NonNullByDefault
|
||||||
|
public class DataAmountTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBToMB() {
|
||||||
|
QuantityType<DataAmount> quantityType = new QuantityType("1000 B");
|
||||||
|
QuantityType<DataAmount> converted = quantityType.toUnit("MB");
|
||||||
|
|
||||||
|
assertThat(converted.toString(), is(equalTo("0.001 MB")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testKBToMB() {
|
||||||
|
QuantityType<DataAmount> quantityType = new QuantityType("1000 kB");
|
||||||
|
QuantityType<DataAmount> converted = quantityType.toUnit("MB");
|
||||||
|
|
||||||
|
assertThat(converted.toString(), is(equalTo("1 MB")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInvertibleUnit() {
|
||||||
|
QuantityType<DataAmount> quantityType = new QuantityType("1000 KB");
|
||||||
|
QuantityType<?> inverted = quantityType.toInvertibleUnit(Units.MEGABYTE);
|
||||||
|
|
||||||
|
assertThat(quantityType, is(equalTo(inverted)));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user