Skip to content

Support interoperability to other unit systems #402

@jpink

Description

@jpink

My target is to create "custom" units for electricity prices: €/MWh and c/kWh. I added following classes:

public class CurrencyDimension implements Dimension, Serializable { // Copied from BaseUnit because its final ?!?
    // The symbol is '¤'
} 
public class Money extends AbstractNumberQuantity<Money> { // Copied from NumberQuantity because its final ?!?
    // Uses BigDecimal as a value.
    @Override
    public String toString() {
        // Workaround for tech.units.indriya.format.SimpleUnitFormat$DefaultFormat.format(SimpleUnitFormat.java:907)
        return getValue() + " " + getUnit();
    }
} 
public class CurrencyUnit extends AbstractUnit<Money> { // Again it would be nice to extend BaseUnit
    public final @NonNull Currency currency; // Internally uses java.util.Currency

    @Override
    public String toString() {
        // Workaround for tech.units.indriya.format.SimpleUnitFormat$DefaultFormat.format(SimpleUnitFormat.java:907)
        return getSymbol();
    }
}
public class CurrencyCentUnit extends CurrencyUnit { // Symbol is 'c' and name is currency display name + " cent".
    private final CurrencyUnit baseUnit;
}
public class Monetary extends AbstractSystemOfUnits { } // This seems to be optional

When i try to print quantity i get error:

java.lang.IllegalArgumentException: Cannot format given Object as a Unit
	at tech.units.indriya.format.SimpleUnitFormat$DefaultFormat.format(SimpleUnitFormat.java:907)

That is because my class isn't AlternateUnit or ProductUnit.

  1. Can you support standard Unit and Quantity interfaces?
  2. Can create another system of units and combine those different systems with this library? I think I should. Or should I fork this and do the modifications.
  3. And what benefits you get by marking all classes final?

I'm not planning to do currency exchanges (although its possible use some service). Just playing on same currency and combining to SI system.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    📋 Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions