Skip to content

Commit

Permalink
Fixed a caching issue with UOM conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
point85 committed Aug 28, 2021
1 parent fcf9d2a commit 95f56ae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.point85</groupId>
<artifactId>uom</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
<packaging>jar</packaging>

<name>uom</name>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/point85/uom/UnitOfMeasure.java
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,11 @@ public void setConversion(double scalingFactor, UnitOfMeasure abscissaUnit, doub

// re-cache
MeasurementSystem.getSystem().registerUnit(this);

// remove from conversion registry
if (conversionRegistry.containsKey(abscissaUnit)) {
conversionRegistry.remove(abscissaUnit);
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/point85/uom/test/library/TestQuantity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ of this software and associated documentation files (the "Software"), to deal

import org.junit.Test;
import org.point85.uom.Constant;
import org.point85.uom.MeasurementSystem;
import org.point85.uom.Prefix;
import org.point85.uom.Quantity;
import org.point85.uom.Unit;
Expand Down Expand Up @@ -1250,4 +1251,23 @@ public void testMultiConversion() throws Exception {
assertTrue(isCloseTo(converted.get(1).getAmount(), 1128.0, DELTA6));
assertTrue(isCloseTo(converted.get(2).getAmount(), 4.787, DELTA3));
}

@Test
public void testCurrencyConversion() throws Exception {
MeasurementSystem sys = MeasurementSystem.getSystem();
UnitOfMeasure usd_uom = sys.createScalarUOM(UnitType.CURRENCY, "US-Dollar", "USD", "US 'paper' dollar");
UnitOfMeasure usdt_uom = sys.createScalarUOM(UnitType.CURRENCY, "Tether", "USDT", "USD 'stable' coin");

// Initial conversion rate
usdt_uom.setConversion(0.9, usd_uom);

Quantity portfolio = new Quantity(200, usdt_uom);
Quantity portfolio_usd = portfolio.convert(usd_uom);
assertTrue(isCloseTo(portfolio_usd.getAmount(), 180.0, DELTA6));

// change conversion rate
usdt_uom.setConversion(1.0, usd_uom);
portfolio_usd = portfolio.convert(usd_uom);
assertTrue(isCloseTo(portfolio_usd.getAmount(), 200.0, DELTA6));
}
}

0 comments on commit 95f56ae

Please sign in to comment.