@Test void generateUnitConversionAlgorithm() { String generateUnitConversionAlgorithm = numericAlgorithmGenerator.generateUnitConversionAlgorithm(targetAttribute, sourceAttribute); assertEquals( "$('sourceHeight').unit('cm').toUnit('m').value();", generateUnitConversionAlgorithm); }
String generateUnitConversionAlgorithm(Attribute targetAttribute, Attribute sourceAttribute) { String algorithm = null; Unit<? extends Quantity> targetUnit = unitResolver.resolveUnit(targetAttribute); Unit<? extends Quantity> sourceUnit = unitResolver.resolveUnit(sourceAttribute); if (sourceUnit != null) { if (targetUnit != null && !sourceUnit.equals(targetUnit)) { UnitConverter unitConverter; try { unitConverter = sourceUnit.getConverterTo(targetUnit); } catch (ConversionException e) { unitConverter = null; algorithm = String.format( "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString()); } if (unitConverter != null) { algorithm = String.format( "$('%s').unit('%s').toUnit('%s').value();", sourceAttribute.getName(), sourceUnit.toString(), targetUnit.toString()); } } else { algorithm = String.format( "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString()); } } if (algorithm == null) { algorithm = String.format("$('%s').value();", sourceAttribute.getName()); } return algorithm; }
NumericAlgorithmGenerator implements AlgorithmGenerator { String generateUnitConversionAlgorithm(Attribute targetAttribute, Attribute sourceAttribute) { String algorithm = null; Unit<? extends Quantity> targetUnit = unitResolver.resolveUnit(targetAttribute); Unit<? extends Quantity> sourceUnit = unitResolver.resolveUnit(sourceAttribute); if (sourceUnit != null) { if (targetUnit != null && !sourceUnit.equals(targetUnit)) { UnitConverter unitConverter; try { unitConverter = sourceUnit.getConverterTo(targetUnit); } catch (ConversionException e) { unitConverter = null; algorithm = String.format( "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString()); } if (unitConverter != null) { algorithm = String.format( "$('%s').unit('%s').toUnit('%s').value();", sourceAttribute.getName(), sourceUnit.toString(), targetUnit.toString()); } } else { algorithm = String.format( "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString()); } } if (algorithm == null) { algorithm = String.format("$('%s').value();", sourceAttribute.getName()); } return algorithm; } }
NumericAlgorithmGenerator implements AlgorithmGenerator { String generateUnitConversionAlgorithm(Attribute targetAttribute, Attribute sourceAttribute) { String algorithm = null; Unit<? extends Quantity> targetUnit = unitResolver.resolveUnit(targetAttribute); Unit<? extends Quantity> sourceUnit = unitResolver.resolveUnit(sourceAttribute); if (sourceUnit != null) { if (targetUnit != null && !sourceUnit.equals(targetUnit)) { UnitConverter unitConverter; try { unitConverter = sourceUnit.getConverterTo(targetUnit); } catch (ConversionException e) { unitConverter = null; algorithm = String.format( "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString()); } if (unitConverter != null) { algorithm = String.format( "$('%s').unit('%s').toUnit('%s').value();", sourceAttribute.getName(), sourceUnit.toString(), targetUnit.toString()); } } else { algorithm = String.format( "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString()); } } if (algorithm == null) { algorithm = String.format("$('%s').value();", sourceAttribute.getName()); } return algorithm; } NumericAlgorithmGenerator(UnitResolver unitResolver); }
NumericAlgorithmGenerator implements AlgorithmGenerator { String generateUnitConversionAlgorithm(Attribute targetAttribute, Attribute sourceAttribute) { String algorithm = null; Unit<? extends Quantity> targetUnit = unitResolver.resolveUnit(targetAttribute); Unit<? extends Quantity> sourceUnit = unitResolver.resolveUnit(sourceAttribute); if (sourceUnit != null) { if (targetUnit != null && !sourceUnit.equals(targetUnit)) { UnitConverter unitConverter; try { unitConverter = sourceUnit.getConverterTo(targetUnit); } catch (ConversionException e) { unitConverter = null; algorithm = String.format( "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString()); } if (unitConverter != null) { algorithm = String.format( "$('%s').unit('%s').toUnit('%s').value();", sourceAttribute.getName(), sourceUnit.toString(), targetUnit.toString()); } } else { algorithm = String.format( "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString()); } } if (algorithm == null) { algorithm = String.format("$('%s').value();", sourceAttribute.getName()); } return algorithm; } NumericAlgorithmGenerator(UnitResolver unitResolver); String generate(
      Attribute targetAttribute,
      List<Attribute> sourceAttributes,
      EntityType targetEntityType,
      EntityType sourceEntityType); boolean isSuitable(Attribute targetAttribute, List<Attribute> sourceAttributes); }
NumericAlgorithmGenerator implements AlgorithmGenerator { String generateUnitConversionAlgorithm(Attribute targetAttribute, Attribute sourceAttribute) { String algorithm = null; Unit<? extends Quantity> targetUnit = unitResolver.resolveUnit(targetAttribute); Unit<? extends Quantity> sourceUnit = unitResolver.resolveUnit(sourceAttribute); if (sourceUnit != null) { if (targetUnit != null && !sourceUnit.equals(targetUnit)) { UnitConverter unitConverter; try { unitConverter = sourceUnit.getConverterTo(targetUnit); } catch (ConversionException e) { unitConverter = null; algorithm = String.format( "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString()); } if (unitConverter != null) { algorithm = String.format( "$('%s').unit('%s').toUnit('%s').value();", sourceAttribute.getName(), sourceUnit.toString(), targetUnit.toString()); } } else { algorithm = String.format( "$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString()); } } if (algorithm == null) { algorithm = String.format("$('%s').value();", sourceAttribute.getName()); } return algorithm; } NumericAlgorithmGenerator(UnitResolver unitResolver); String generate(
      Attribute targetAttribute,
      List<Attribute> sourceAttributes,
      EntityType targetEntityType,
      EntityType sourceEntityType); boolean isSuitable(Attribute targetAttribute, List<Attribute> sourceAttributes);  }