我正在寻找使用Jscience进行计算的更好方法,但通用模式更难以获得清晰的解决方案。如果定义了单位数量的单价,我需要计算n个单位的价格。等,
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());
对于上面的代码,我收到此错误:
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>)
我在Java Tutorials阅读了关于泛型的内容并尝试了以下内容,但仍然没有明确的解决方案:
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());
}
现在收到此错误:
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>)
答案 0 :(得分:2)
在某些情况下,Unit.valueOf("kg")
工厂方法可能很方便。但在这种情况下,它没有保留足够的信息。它只返回&#34; some&#34;的单位。数量 - 因此返回类型中为? extends Quantity
。但是,您不知道这是否是数量,例如Mass
或Power
。只要您不知道这一点,就无法说出对quantity.to(unitQuantity.getUnit())
的通话是否有效。
在这种情况下,您可以使用new BaseUnit<Mass>("kg")
代替Unit.valueOf("kg")
解决此问题:
import static javax.measure.unit.SI.MILLI;
import javax.measure.Measure;
import javax.measure.quantity.Mass;
import javax.measure.unit.BaseUnit;
import org.jscience.economics.money.Currency;
import org.jscience.economics.money.Money;
import org.jscience.physics.amount.Amount;
public class JScienceUnits
{
public static void main(String[] args)
{
BaseUnit<Mass> kg = new BaseUnit<Mass>("kg");
Measure<Double, Mass> unitQuantity = Measure.valueOf(1.0, kg);
Amount<Money> unitPrice = Amount.valueOf(150.0, Currency.USD);
Measure<Double, Mass> quantity = Measure.valueOf(500.0, MILLI(kg));
Amount<Money> amount = unitPrice.times(
quantity.to(unitQuantity.getUnit()).getValue() /
unitQuantity.getValue());
System.out.println(
"Money for "+quantity+
" with unit price "+unitPrice+
" is "+amount);
}
}
答案 1 :(得分:0)
找到两种方法。转换失败时抛出ConversionException。
@SuppressWarnings("unchecked")
public static <U extends Quantity, V extends Quantity> Amount<Money> computeAmount(
Measure<Double, U> unitQuantity, Amount<Money> unitRate,
Measure<Double, V> netQuantity) {
return unitRate.times(netQuantity.to((Unit<V>) unitQuantity.getUnit())
.getValue() / unitQuantity.getValue());
}
-
public static <U extends Quantity, V extends Quantity> Amount<Money> computeAmount(
Measure<Double, U> unitQuantity, Amount<Money> unitRate,
Measure<Double, V> netQuantity) {
UnitConverter toUnitQuantityUnit = netQuantity.getUnit()
.getConverterTo(unitQuantity.getUnit());
return unitRate
.times(toUnitQuantityUnit.convert(netQuantity.getValue())
/ unitQuantity.getValue());
}