I am looking for a better way to compute using Jscience but the generic pattern is much tougher to get a clear solution. I need to compute price for n units provided the unit price for a unit quantity is defined. like,
Measure<Double, ? extends Quantity> unitQuantity = Measure.valueOf(1.0,
Unit.valueOf("kg"));
Amount<Money> unitPrice = Amount.valueOf(150.0, USD);
Measure<Double, ? extends Quantity> quantity = Measure.valueOf(500.0,
MILLI(Unit.valueOf("kg")));
Amount<Money> amount = unitPrice.times(quantity.to(unitQuantity.getUnit())
.getValue() / unitQuantity.getValue());
For the above code I am getting this error:
The method to(Unit<capture#7-of ? extends Quantity>) in the type
Measure<Double,capture#7-of ? extends Quantity> is not applicable for
the arguments (Unit<capture#8-of ? extends Quantity>)
I read about generics at Java Tutorials and tried the following, but still no clear solution:
Amount<Money> amount = compute(unitPrice,unitQuantity,quantity)
private <T extends Quantity> Amount<Money> compute(Amount<Money> unitPrice,
Measure<Double, T> unitQuantity, Measure<Double, T> quantity) {
return unitPrice.times(quantity.to(unitQuantity.getUnit()).getValue()
/ unitQuantity.getValue());
}
Now getting this error:
The method compute(Amount<Money>, Measure<Double,T>, Measure<Double,T>)
in the type JscienceEx is not applicable for the arguments (Amount<Money>,
Measure<Double,capture#7-of ? extends Quantity>, Measure<Double,capture#8-of ?
extends Quantity>)