unitsofmeasurement / indriya

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

Add ability to remove label from `SimpleUnitFormat` #394

Closed J-N-K closed 1 year ago

J-N-K commented 1 year ago

In openHAB (https://github.com/openhab) we use SimpleUnitFormat.getInstance() for handling labels of units. Until now this worked fine, but for a new feature we need dynamic handling of available units. Unfortunately it is not possible to remove a label from the DefaultFormat and it is also not possible to extend the DefaultFormat because it has protected access only.

I can provide a PR for the addition of DefaultFormat.removeLabels(Unit<?>). WDYT?

keilw commented 1 year ago

While this is for the inner class DefaultFormat, it should ideally be on UnitFormat along the lines of ServletContext and methods like setAttribute and removeAttribute, but we could leave such removeLabel (and for consistency also removeAlias) method available on SimpleUnitFormat for now.

As DefaultFormat should be made private or package-local in a future version, this doesn't make sense on its level.

Could be fixed by #396

keilw commented 1 year ago

I made a small adjustment, because unlike the label (which can only be set once, if set multiple times overwrites its "label" purpose and leaves the old one basically as an alias) aliases can be assigned multiple times. Therefore I redefined removeAlias() to remove a named alias (if present) while removeAliases() removes all aliases for a particular unit.

I added more tests to SimpleUnitFormatTest, the only thing I didn't call was removing the label/aliases from the "shared" instance, because it would break other tests, but if that's what you need in openHAB or elsewhere, you can use these methods accordingly.