Search in sources :

Example 1 with ConvertDistanceUnit

use of io.atlasmap.v2.ConvertDistanceUnit in project atlasmap by atlasmap.

the class NumberFieldActions method convertDistanceUnit.

@AtlasFieldActionInfo(name = "ConvertDistanceUnit", sourceType = FieldType.NUMBER, targetType = FieldType.NUMBER, sourceCollectionType = CollectionType.NONE, targetCollectionType = CollectionType.NONE)
public static Number convertDistanceUnit(Action action, Number input) {
    if (input == null) {
        return 0;
    }
    if (action == null || !(action instanceof ConvertDistanceUnit) || ((ConvertDistanceUnit) action).getFromUnit() == null || ((ConvertDistanceUnit) action).getToUnit() == null) {
        throw new IllegalArgumentException("ConvertDistanceUnit must be specfied  with fromUnit and toUnit");
    }
    DistanceUnitType fromUnit = ((ConvertDistanceUnit) action).getFromUnit();
    DistanceUnitType toUnit = ((ConvertDistanceUnit) action).getToUnit();
    double rate = distanceConvertionTable.get(fromUnit).get(toUnit);
    return doMultiply(input, rate);
}
Also used : DistanceUnitType(io.atlasmap.v2.DistanceUnitType) ConvertDistanceUnit(io.atlasmap.v2.ConvertDistanceUnit) AtlasFieldActionInfo(io.atlasmap.spi.AtlasFieldActionInfo)

Example 2 with ConvertDistanceUnit

use of io.atlasmap.v2.ConvertDistanceUnit in project atlasmap by atlasmap.

the class NumberFieldActionsTest method testConvertDistanceUnit.

@Test
public void testConvertDistanceUnit() {
    ConvertDistanceUnit action = new ConvertDistanceUnit();
    action.setFromUnit(DistanceUnitType.METER);
    action.setToUnit(DistanceUnitType.METER);
    assertEquals(1.0, NumberFieldActions.convertDistanceUnit(action, 1.0));
    action.setToUnit(DistanceUnitType.FOOT);
    assertEquals(6.561679790026247, NumberFieldActions.convertDistanceUnit(action, 2.0));
    action.setToUnit(DistanceUnitType.YARD);
    assertEquals(3.2808398950131235, NumberFieldActions.convertDistanceUnit(action, 3.0));
    action.setToUnit(DistanceUnitType.MILE);
    assertEquals(2.4854847689493362, NumberFieldActions.convertDistanceUnit(action, 4000.0));
    action.setToUnit(DistanceUnitType.INCH);
    assertEquals(196.8503937007874, NumberFieldActions.convertDistanceUnit(action, 5.0));
    action.setFromUnit(DistanceUnitType.FOOT);
    action.setToUnit(DistanceUnitType.METER);
    assertEquals(1.8287999999999998, NumberFieldActions.convertDistanceUnit(action, 6.0));
    action.setToUnit(DistanceUnitType.FOOT);
    assertEquals(7.0, NumberFieldActions.convertDistanceUnit(action, 7.0));
    action.setToUnit(DistanceUnitType.YARD);
    assertEquals(27.0, NumberFieldActions.convertDistanceUnit(action, 81.0));
    action.setToUnit(DistanceUnitType.MILE);
    assertEquals(1.7045454545454546, NumberFieldActions.convertDistanceUnit(action, 9000.0));
    action.setToUnit(DistanceUnitType.INCH);
    assertEquals(12.0, NumberFieldActions.convertDistanceUnit(action, 1.0));
    action.setFromUnit(DistanceUnitType.YARD);
    action.setToUnit(DistanceUnitType.METER);
    assertEquals(22.86, NumberFieldActions.convertDistanceUnit(action, 25.0));
    action.setToUnit(DistanceUnitType.FOOT);
    assertEquals(9.0, NumberFieldActions.convertDistanceUnit(action, 3.0));
    action.setToUnit(DistanceUnitType.YARD);
    assertEquals(4.0, NumberFieldActions.convertDistanceUnit(action, 4.0));
    action.setToUnit(DistanceUnitType.MILE);
    assertEquals(2.840909090909091, NumberFieldActions.convertDistanceUnit(action, 5000.0));
    action.setToUnit(DistanceUnitType.INCH);
    assertEquals(216.0, NumberFieldActions.convertDistanceUnit(action, 6.0));
    action.setFromUnit(DistanceUnitType.MILE);
    action.setToUnit(DistanceUnitType.METER);
    assertEquals(11265.408, NumberFieldActions.convertDistanceUnit(action, 7.0));
    action.setToUnit(DistanceUnitType.FOOT);
    assertEquals(42240.0, NumberFieldActions.convertDistanceUnit(action, 8.0));
    action.setToUnit(DistanceUnitType.YARD);
    assertEquals(15840.0, NumberFieldActions.convertDistanceUnit(action, 9.0));
    action.setToUnit(DistanceUnitType.MILE);
    assertEquals(1.0, NumberFieldActions.convertDistanceUnit(action, 1.0));
    action.setToUnit(DistanceUnitType.INCH);
    assertEquals(126720.0, NumberFieldActions.convertDistanceUnit(action, 2.0));
    action.setFromUnit(DistanceUnitType.INCH);
    action.setToUnit(DistanceUnitType.METER);
    assertEquals(7.62, NumberFieldActions.convertDistanceUnit(action, 300.0));
    action.setToUnit(DistanceUnitType.FOOT);
    assertEquals(3.5, NumberFieldActions.convertDistanceUnit(action, 42.0));
    action.setToUnit(DistanceUnitType.YARD);
    assertEquals(1.5, NumberFieldActions.convertDistanceUnit(action, 54.0));
    action.setToUnit(DistanceUnitType.MILE);
    assertEquals(9.469696969696969, NumberFieldActions.convertDistanceUnit(action, 600000.0));
    action.setToUnit(DistanceUnitType.INCH);
    assertEquals(6.0, NumberFieldActions.convertDistanceUnit(action, 6.0));
}
Also used : ConvertDistanceUnit(io.atlasmap.v2.ConvertDistanceUnit) Test(org.junit.Test)

Aggregations

ConvertDistanceUnit (io.atlasmap.v2.ConvertDistanceUnit)2 AtlasFieldActionInfo (io.atlasmap.spi.AtlasFieldActionInfo)1 DistanceUnitType (io.atlasmap.v2.DistanceUnitType)1 Test (org.junit.Test)1