@Test void convertUnit() { assertEquals(".div(1000.0)", unitConverter.convertUnit(valueOf("kg"), valueOf("g"))); assertEquals(".div(100.0)", unitConverter.convertUnit(valueOf("m"), valueOf("cm"))); assertEquals(".div(1000.0)", unitConverter.convertUnit(valueOf("kg/m²"), valueOf("g"))); assertEquals(".div(100.0)", unitConverter.convertUnit(valueOf("kg/m²"), valueOf("cm"))); }
public String convertUnit(Unit<? extends Quantity> standardUnit, Unit<? extends Quantity> unit) { if (standardUnit != null && unit != null) { StringBuilder conversionScript = new StringBuilder(); for (String standardUnitName : findCompositeUnitNames(standardUnit.toString())) { for (String customeUnitName : findCompositeUnitNames(unit.toString())) { Unit<?> unit1 = Unit.valueOf(standardUnitName); Unit<?> unit2 = Unit.valueOf(customeUnitName); if (unit1 != null && unit2 != null && unit1.isCompatible(unit2) && !unit1.equals(unit2)) { Amount<?> value2 = Amount.valueOf(1, unit2); Amount<?> value1 = value2.to(unit1); double estimatedValue1 = value1.getEstimatedValue(); double estimatedValue2 = value2.getEstimatedValue(); if (estimatedValue1 > estimatedValue2) { conversionScript .append(".times(") .append(value1.divide(value2).getEstimatedValue()) .append(")"); } else { conversionScript .append(".div(") .append(value2.divide(value1).getEstimatedValue()) .append(")"); } } if (conversionScript.length() > 0) { return conversionScript.toString(); } } } } return StringUtils.EMPTY; }
MagmaUnitConverter { public String convertUnit(Unit<? extends Quantity> standardUnit, Unit<? extends Quantity> unit) { if (standardUnit != null && unit != null) { StringBuilder conversionScript = new StringBuilder(); for (String standardUnitName : findCompositeUnitNames(standardUnit.toString())) { for (String customeUnitName : findCompositeUnitNames(unit.toString())) { Unit<?> unit1 = Unit.valueOf(standardUnitName); Unit<?> unit2 = Unit.valueOf(customeUnitName); if (unit1 != null && unit2 != null && unit1.isCompatible(unit2) && !unit1.equals(unit2)) { Amount<?> value2 = Amount.valueOf(1, unit2); Amount<?> value1 = value2.to(unit1); double estimatedValue1 = value1.getEstimatedValue(); double estimatedValue2 = value2.getEstimatedValue(); if (estimatedValue1 > estimatedValue2) { conversionScript .append(".times(") .append(value1.divide(value2).getEstimatedValue()) .append(")"); } else { conversionScript .append(".div(") .append(value2.divide(value1).getEstimatedValue()) .append(")"); } } if (conversionScript.length() > 0) { return conversionScript.toString(); } } } } return StringUtils.EMPTY; } }
MagmaUnitConverter { public String convertUnit(Unit<? extends Quantity> standardUnit, Unit<? extends Quantity> unit) { if (standardUnit != null && unit != null) { StringBuilder conversionScript = new StringBuilder(); for (String standardUnitName : findCompositeUnitNames(standardUnit.toString())) { for (String customeUnitName : findCompositeUnitNames(unit.toString())) { Unit<?> unit1 = Unit.valueOf(standardUnitName); Unit<?> unit2 = Unit.valueOf(customeUnitName); if (unit1 != null && unit2 != null && unit1.isCompatible(unit2) && !unit1.equals(unit2)) { Amount<?> value2 = Amount.valueOf(1, unit2); Amount<?> value1 = value2.to(unit1); double estimatedValue1 = value1.getEstimatedValue(); double estimatedValue2 = value2.getEstimatedValue(); if (estimatedValue1 > estimatedValue2) { conversionScript .append(".times(") .append(value1.divide(value2).getEstimatedValue()) .append(")"); } else { conversionScript .append(".div(") .append(value2.divide(value1).getEstimatedValue()) .append(")"); } } if (conversionScript.length() > 0) { return conversionScript.toString(); } } } } return StringUtils.EMPTY; }  }
MagmaUnitConverter { public String convertUnit(Unit<? extends Quantity> standardUnit, Unit<? extends Quantity> unit) { if (standardUnit != null && unit != null) { StringBuilder conversionScript = new StringBuilder(); for (String standardUnitName : findCompositeUnitNames(standardUnit.toString())) { for (String customeUnitName : findCompositeUnitNames(unit.toString())) { Unit<?> unit1 = Unit.valueOf(standardUnitName); Unit<?> unit2 = Unit.valueOf(customeUnitName); if (unit1 != null && unit2 != null && unit1.isCompatible(unit2) && !unit1.equals(unit2)) { Amount<?> value2 = Amount.valueOf(1, unit2); Amount<?> value1 = value2.to(unit1); double estimatedValue1 = value1.getEstimatedValue(); double estimatedValue2 = value2.getEstimatedValue(); if (estimatedValue1 > estimatedValue2) { conversionScript .append(".times(") .append(value1.divide(value2).getEstimatedValue()) .append(")"); } else { conversionScript .append(".div(") .append(value2.divide(value1).getEstimatedValue()) .append(")"); } } if (conversionScript.length() > 0) { return conversionScript.toString(); } } } } return StringUtils.EMPTY; }  String convertUnit(Unit<? extends Quantity> standardUnit, Unit<? extends Quantity> unit); }
MagmaUnitConverter { public String convertUnit(Unit<? extends Quantity> standardUnit, Unit<? extends Quantity> unit) { if (standardUnit != null && unit != null) { StringBuilder conversionScript = new StringBuilder(); for (String standardUnitName : findCompositeUnitNames(standardUnit.toString())) { for (String customeUnitName : findCompositeUnitNames(unit.toString())) { Unit<?> unit1 = Unit.valueOf(standardUnitName); Unit<?> unit2 = Unit.valueOf(customeUnitName); if (unit1 != null && unit2 != null && unit1.isCompatible(unit2) && !unit1.equals(unit2)) { Amount<?> value2 = Amount.valueOf(1, unit2); Amount<?> value1 = value2.to(unit1); double estimatedValue1 = value1.getEstimatedValue(); double estimatedValue2 = value2.getEstimatedValue(); if (estimatedValue1 > estimatedValue2) { conversionScript .append(".times(") .append(value1.divide(value2).getEstimatedValue()) .append(")"); } else { conversionScript .append(".div(") .append(value2.divide(value1).getEstimatedValue()) .append(")"); } } if (conversionScript.length() > 0) { return conversionScript.toString(); } } } } return StringUtils.EMPTY; }  String convertUnit(Unit<? extends Quantity> standardUnit, Unit<? extends Quantity> unit);  }