unitsofmeasurement / indriya

JSR 385 - Reference Implementation
Other
118 stars 40 forks source link

ProductUnit not thread-safe #282

Closed andi-huber closed 4 years ago

andi-huber commented 4 years ago

same as with https://github.com/unitsofmeasurement/uom-se/issues/210

andi-huber commented 4 years ago

supposed fix: PR 284 https://github.com/unitsofmeasurement/indriya/pull/284/files

andi-huber commented 4 years ago

ProductUnit#hashCode() had a side-effect, such that when called it would sort its unit dimension elements. Which is semantically incorrect. According to spec the initial order must be preserved.

Also this sorting had the potential of interfering with other threads, that would access these unit dimension elements concurrently.

We fix both here by using a defensive copy of the elements before sorting, while preserving the initial order of the unit dimension elements. As an optimization bonus, we do cash the calculated hashCode, so the unit element sorting is only required once per instance.

andi-huber commented 4 years ago

merged