Search in sources :

Example 1 with Percentage

use of com.dexels.navajo.document.types.Percentage in project navajo by Dexels.

the class ASTMulNode method interpret.

private Operand interpret(Operand ao, Operand bo) {
    Object a = ao.value;
    Object b = bo.value;
    if (a instanceof Integer) {
        int inta = (Integer) a;
        if (b instanceof Integer) {
            return Operand.ofInteger(inta * (Integer) b);
        } else if (b instanceof Long) {
            return Operand.ofLong(inta * (Long) b);
        } else if (b instanceof Double) {
            return Operand.ofFloat(inta * (Double) b);
        }
    } else if (a instanceof Long) {
        long longa = (Long) a;
        if (b instanceof Integer) {
            return Operand.ofLong(longa * (Integer) b);
        } else if (b instanceof Long) {
            return Operand.ofLong(longa * (Long) b);
        } else if (b instanceof Double) {
            return Operand.ofFloat(longa * (Double) b);
        }
    } else if (a instanceof Double) {
        double doublea = (Double) a;
        if (b instanceof Integer) {
            return Operand.ofFloat(doublea * (Integer) b);
        } else if (b instanceof Long) {
            return Operand.ofFloat(doublea * (Long) b);
        } else if (b instanceof Double) {
            return Operand.ofFloat(doublea * (Double) b);
        }
    } else if (a instanceof Money || b instanceof Money) {
        return Operand.ofMoney(new Money(Utils.getDoubleValue(a) * Utils.getDoubleValue(b)));
    } else if (a instanceof Percentage || b instanceof Percentage) {
        return Operand.ofPercentage(new Percentage(Utils.getDoubleValue(a) * Utils.getDoubleValue(b)));
    } else if ((a instanceof String) || (b instanceof String)) {
        throw new TMLExpressionException("Multiplication not defined for String values");
    }
    return Operand.NULL;
}
Also used : Money(com.dexels.navajo.document.types.Money) Percentage(com.dexels.navajo.document.types.Percentage) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 2 with Percentage

use of com.dexels.navajo.document.types.Percentage in project navajo by Dexels.

the class Utils method add.

/**
 * Generic method to add two objects.
 */
public static final Object add(Object a, Object b, String expression) {
    if ((a == null) && (b == null)) {
        return null;
    } else if (a == null) {
        return b;
    } else if (b == null) {
        return a;
    }
    if ((a instanceof String) || (b instanceof String)) {
        String sA = Utils.getStringValue(a);
        String sB = Utils.getStringValue(b);
        return sA + sB;
    } else if ((a instanceof DatePattern || a instanceof Date) && (b instanceof DatePattern || b instanceof Date)) {
        DatePattern dp1 = null;
        DatePattern dp2 = null;
        if (a instanceof Date) {
            dp1 = DatePattern.parseDatePattern((Date) a);
        } else {
            dp1 = (DatePattern) a;
        }
        if (b instanceof Date) {
            dp2 = DatePattern.parseDatePattern((Date) b);
        } else {
            dp2 = (DatePattern) b;
        }
        dp1.add(dp2);
        return dp1.getDate();
    } else if ((a instanceof Money) || (b instanceof Money)) {
        if (!(a instanceof Money || a instanceof Integer || a instanceof Long || a instanceof Double))
            throw new TMLExpressionException("Invalid argument for operation: " + a.getClass() + ", expression: " + expression);
        if (!(b instanceof Money || b instanceof Integer || b instanceof Long || b instanceof Double))
            throw new TMLExpressionException("Invalid argument for operation: " + b.getClass() + ", expression: " + expression);
        Money arg1 = (a instanceof Money ? (Money) a : new Money(a));
        Money arg2 = (b instanceof Money ? (Money) b : new Money(b));
        return new Money(arg1.doubleValue() + arg2.doubleValue());
    } else if ((a instanceof Percentage) || (b instanceof Percentage)) {
        if (!(a instanceof Percentage || a instanceof Integer || a instanceof Long || a instanceof Double))
            throw new TMLExpressionException("Invalid argument for operation: " + a.getClass() + ", expression: " + expression);
        if (!(b instanceof Percentage || b instanceof Integer || b instanceof Long || b instanceof Double))
            throw new TMLExpressionException("Invalid argument for operation: " + b.getClass() + ", expression: " + expression);
        Percentage arg1 = (a instanceof Percentage ? (Percentage) a : new Percentage(a));
        Percentage arg2 = (b instanceof Percentage ? (Percentage) b : new Percentage(b));
        return new Percentage(arg1.doubleValue() + arg2.doubleValue());
    } else if ((a instanceof ClockTime && b instanceof DatePattern)) {
        DatePattern dp1 = DatePattern.parseDatePattern(((ClockTime) a).dateValue());
        DatePattern dp2 = (DatePattern) b;
        dp1.add(dp2);
        return new ClockTime(dp1.getDate());
    } else if ((b instanceof ClockTime && a instanceof DatePattern)) {
        DatePattern dp1 = DatePattern.parseDatePattern(((ClockTime) b).dateValue());
        DatePattern dp2 = (DatePattern) a;
        dp1.add(dp2);
        return new ClockTime(dp1.getDate());
    } else if ((a instanceof ClockTime && b instanceof ClockTime)) {
        DatePattern dp1 = DatePattern.parseDatePattern(((ClockTime) a).dateValue());
        DatePattern dp2 = DatePattern.parseDatePattern(((ClockTime) b).dateValue());
        dp1.add(dp2);
        return new ClockTime(dp1.getDate());
    } else if ((a instanceof Boolean && b instanceof Boolean)) {
        Boolean ba = (Boolean) a;
        Boolean bb = (Boolean) b;
        return Integer.valueOf((ba.booleanValue() ? 1 : 0) + (bb.booleanValue() ? 1 : 0));
    } else if (a instanceof Integer) {
        int inta = (Integer) a;
        if (b instanceof Integer) {
            return inta + (Integer) b;
        } else if (b instanceof Long) {
            return inta + (Long) b;
        } else if (b instanceof Double) {
            return inta + (Double) b;
        }
    } else if (a instanceof Long) {
        long longa = (Long) a;
        if (b instanceof Integer) {
            return longa + (Integer) b;
        } else if (b instanceof Long) {
            return longa + (Long) b;
        } else if (b instanceof Double) {
            return longa + (Double) b;
        }
    } else if (a instanceof Double) {
        double doublea = (Double) a;
        if (b instanceof Integer) {
            return doublea + (Integer) b;
        } else if (b instanceof Long) {
            return doublea + (Long) b;
        } else if (b instanceof Double) {
            return doublea + (Double) b;
        }
    }
    throw new TMLExpressionException("Addition: Unknown type. " + " expression: " + expression);
}
Also used : Percentage(com.dexels.navajo.document.types.Percentage) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) ClockTime(com.dexels.navajo.document.types.ClockTime) Date(java.util.Date) Money(com.dexels.navajo.document.types.Money) DatePattern(com.dexels.navajo.document.types.DatePattern)

Example 3 with Percentage

use of com.dexels.navajo.document.types.Percentage in project navajo by Dexels.

the class UtilsPercentageTest method testSubtractPercentageAndLong.

@Test
public void testSubtractPercentageAndLong() {
    Percentage a = new Percentage(15);
    Long b = 10L;
    Percentage result = (Percentage) Utils.subtract(a, b, "ToPercentage(15) - ToLong(10)");
    assertEquals(new Percentage(5), result);
}
Also used : Percentage(com.dexels.navajo.document.types.Percentage) Test(org.junit.Test)

Example 4 with Percentage

use of com.dexels.navajo.document.types.Percentage in project navajo by Dexels.

the class UtilsPercentageTest method testSubtractIntegerAndPercentage.

@Test
public void testSubtractIntegerAndPercentage() {
    Integer a = 15;
    Percentage b = new Percentage(10);
    Percentage result = (Percentage) Utils.subtract(a, b, "15 - ToPercentage(10)");
    assertEquals(new Percentage(5), result);
}
Also used : Percentage(com.dexels.navajo.document.types.Percentage) Test(org.junit.Test)

Example 5 with Percentage

use of com.dexels.navajo.document.types.Percentage in project navajo by Dexels.

the class UtilsPercentageTest method testSubtractDoubleAndPercentage.

@Test
public void testSubtractDoubleAndPercentage() {
    Double a = 15.0;
    Percentage b = new Percentage(10);
    Percentage result = (Percentage) Utils.subtract(a, b, "15.0 - ToPercentage(10)");
    assertEquals(new Percentage(5), result);
}
Also used : Percentage(com.dexels.navajo.document.types.Percentage) Test(org.junit.Test)

Aggregations

Percentage (com.dexels.navajo.document.types.Percentage)28 Test (org.junit.Test)18 Money (com.dexels.navajo.document.types.Money)10 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)7 ClockTime (com.dexels.navajo.document.types.ClockTime)6 Date (java.util.Date)5 Navajo (com.dexels.navajo.document.Navajo)4 Operand (com.dexels.navajo.document.Operand)4 Property (com.dexels.navajo.document.Property)4 Message (com.dexels.navajo.document.Message)3 NavajoException (com.dexels.navajo.document.NavajoException)3 StopwatchTime (com.dexels.navajo.document.types.StopwatchTime)3 Binary (com.dexels.navajo.document.types.Binary)2 DatePattern (com.dexels.navajo.document.types.DatePattern)2 SystemException (com.dexels.navajo.script.api.SystemException)2 ExpressionChangedException (com.dexels.navajo.document.ExpressionChangedException)1 PropertyTypeException (com.dexels.navajo.document.PropertyTypeException)1 JSONTML (com.dexels.navajo.document.json.JSONTML)1 BinaryDigest (com.dexels.navajo.document.types.BinaryDigest)1 Coordinate (com.dexels.navajo.document.types.Coordinate)1