Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle UnitUtils.getDimensionName for mirek #4507

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ public class UnitUtils {
* @return The Dimension string or null if the unit can not be found in any of the SystemOfUnits.
*/
public static @Nullable String getDimensionName(Unit<?> unit) {
String compatibleDimension = getDirectDimensionName(unit);
if (compatibleDimension == null) {
compatibleDimension = getDimensionName(unit.inverse());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does unit.inverse() always return a non null value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It may not make immediate logical sense (what's Meter^-1?), but it always has a value.

}
return compatibleDimension;
}

/**
* Returns the name of a compatible dimension for the given unit, without checking for invertedness
*/
private static @Nullable String getDirectDimensionName(Unit<?> unit) {
String compatibleDimension = null;
for (Class<? extends SystemOfUnits> system : ALL_SYSTEM_OF_UNITS) {
for (Field field : system.getDeclaredFields()) {
Expand All @@ -129,7 +140,7 @@ public class UnitUtils {
LOGGER.warn("Unit field points to a null value: {}", field);
} else if (systemUnit.equals(unit)) {
return dimension;
} else if (compatibleDimension == null && systemUnit.isCompatible(unit)) {
} else if (compatibleDimension == null && (systemUnit.isCompatible(unit))) {
compatibleDimension = dimension;
}
} catch (IllegalArgumentException | IllegalAccessException e) {
Expand All @@ -142,7 +153,7 @@ public class UnitUtils {
}
}
}
return compatibleDimension == null ? null : compatibleDimension;
return compatibleDimension;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import javax.measure.Unit;
import javax.measure.quantity.Angle;
import javax.measure.quantity.Dimensionless;
import javax.measure.quantity.ElectricConductance;
import javax.measure.quantity.ElectricResistance;
import javax.measure.quantity.Energy;
import javax.measure.quantity.Length;
import javax.measure.quantity.Power;
Expand Down Expand Up @@ -120,6 +122,12 @@ public void testGetDimensionNameWithDimension() {

unit = Objects.requireNonNull(UnitUtils.parseUnit("m"));
assertThat(UnitUtils.getDimensionName(unit), is(Length.class.getSimpleName()));
assertThat(UnitUtils.getDimensionName(Units.MIRED), is(Temperature.class.getSimpleName()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add the following line to assert that nothing got broken due to the new inversion test..

assertThat(UnitUtils.getDimensionName(Units.KELVIN), is(Temperature.class.getSimpleName()));

assertThat(UnitUtils.getDimensionName(Units.KELVIN), is(Temperature.class.getSimpleName()));
assertThat(UnitUtils.getDimensionName(SIUnits.CELSIUS), is(Temperature.class.getSimpleName()));
assertThat(UnitUtils.getDimensionName(ImperialUnits.FAHRENHEIT), is(Temperature.class.getSimpleName()));
assertThat(UnitUtils.getDimensionName(Units.SIEMENS), is(ElectricConductance.class.getSimpleName()));
assertThat(UnitUtils.getDimensionName(Units.OHM), is(ElectricResistance.class.getSimpleName()));
}

@Test
Expand Down