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

fix: proper factor for deci prefix #176

Merged
merged 1 commit into from
Dec 24, 2024
Merged
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 @@ -10,19 +10,27 @@

public class SensorUnit {

/**
* Note that for simplicity's sake, we're not supporting deca prefix since it would make the prefix identification logic
* more complex and it's not a very used prefix. Might revisit as needed.
*/
private final static Map<String, Double> prefixesToFactors = Map.of(
"n", 1e-9,
"µ", 1e-6,
"m", 1e-3,
"c", 1e-2,
"d", 10.0,
"d", 1e-1,
"h", 100.0,
"k", 1e3,
"M", 1e6,
"G", 1e9);
private final static Map<String, SensorUnit> baseUnits = new HashMap<>();
public static final SensorUnit W = baseUnitFor("W");
public static final SensorUnit J = baseUnitFor("J");
public static final SensorUnit decimalPercentage = baseUnitFor("decimal percentage");
private final static Map<String, SensorUnit> knownUnits = new HashMap<>();

public static final SensorUnit mW = SensorUnit.of("mW");
public static final SensorUnit µJ = SensorUnit.of("µJ");
private final String symbol;
private final SensorUnit base;
private final double factor;
Expand Down Expand Up @@ -121,10 +129,4 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hashCode(symbol);
}

public static final SensorUnit W = baseUnitFor("W");
public static final SensorUnit mW = SensorUnit.of("mW");
public static final SensorUnit J = baseUnitFor("J");
public static final SensorUnit µJ = SensorUnit.of("µJ");
public static final SensorUnit decimalPercentage = baseUnitFor("decimal percentage");
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ void ofShouldWork() {
assertThat(su).isEqualTo(SensorUnit.decimalPercentage);
assertThat(su.factor()).isEqualTo(1);
assertThat(su.base()).isEqualTo(SensorUnit.decimalPercentage);

su = SensorUnit.of("dW");
assertThat(su.base()).isEqualTo(SensorUnit.W);
assertThat(su.factor()).isEqualTo(0.1);
}

@Test
Expand Down Expand Up @@ -47,9 +51,9 @@ void conversionFactorShouldWork() {
assertEquals(1, SensorUnit.J.conversionFactorTo(SensorUnit.J));
assertEquals(1, SensorUnit.decimalPercentage.conversionFactorTo(SensorUnit.decimalPercentage));
assertEquals(1, SensorUnit.of("mW").conversionFactorTo(SensorUnit.of("mW")));
assertEquals(1e-3, SensorUnit.of("mW").conversionFactorTo(SensorUnit.W), 0.0001);
assertEquals(1e3, SensorUnit.of("W").conversionFactorTo(SensorUnit.of("mW")), 0.0001);
assertEquals(1e3, SensorUnit.of("mW").conversionFactorTo(SensorUnit.of("µW")), 0.0001);
assertEquals(0.001, SensorUnit.of("mW").conversionFactorTo(SensorUnit.W), 0.0001);
assertEquals(1000, SensorUnit.of("W").conversionFactorTo(SensorUnit.of("mW")), 0.0001);
assertEquals(1000, SensorUnit.of("mW").conversionFactorTo(SensorUnit.of("µW")), 0.0001);
assertEquals(1e-3, SensorUnit.of("µW").conversionFactorTo(SensorUnit.of("mW")), 0.0001);
assertEquals(0.1, SensorUnit.of("mW").conversionFactorTo(SensorUnit.of("cW")), 0.0001);
assertEquals(10, SensorUnit.of("cW").conversionFactorTo(SensorUnit.of("mW")), 0.0001);
Expand Down
Loading