Search in sources :

Example 21 with Money

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

the class Utils method subtract.

/**
 * Generic method to subtract two objects.
 */
public static final Object subtract(Object a, Object b, String expression) {
    if (a == null || b == null) {
        return null;
    }
    if ((a instanceof String) || (b instanceof String)) {
        throw new TMLExpressionException("Subtraction not defined for Strings");
    } 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 Date && b instanceof Date) {
        // Correct dates for daylight savings time.
        Calendar ca = Calendar.getInstance();
        ca.setTime((Date) a);
        ca.add(Calendar.MILLISECOND, ca.get(Calendar.DST_OFFSET));
        Calendar cb = Calendar.getInstance();
        cb.setTime((Date) b);
        cb.add(Calendar.MILLISECOND, cb.get(Calendar.DST_OFFSET));
        return Integer.valueOf((int) ((ca.getTimeInMillis() - cb.getTimeInMillis()) / (double) MILLIS_IN_DAY));
    } 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.subtract(dp2);
        return dp1.getDate();
    } else if ((a instanceof ClockTime || a instanceof Date || a instanceof StopwatchTime) && (b instanceof ClockTime || b instanceof Date || b instanceof StopwatchTime)) {
        long myMillis = (a instanceof ClockTime ? ((ClockTime) a).dateValue().getTime() : (a instanceof Date ? ((Date) a).getTime() : ((StopwatchTime) a).getMillis()));
        long otherMillis = (b instanceof ClockTime ? ((ClockTime) b).dateValue().getTime() : (b instanceof Date ? ((Date) b).getTime() : ((StopwatchTime) b).getMillis()));
        return new StopwatchTime((int) (myMillis - otherMillis));
    } 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("Subtraction: Unknown type. " + " expression: " + expression);
}
Also used : Percentage(com.dexels.navajo.document.types.Percentage) Calendar(java.util.Calendar) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) ClockTime(com.dexels.navajo.document.types.ClockTime) Date(java.util.Date) StopwatchTime(com.dexels.navajo.document.types.StopwatchTime) Money(com.dexels.navajo.document.types.Money) DatePattern(com.dexels.navajo.document.types.DatePattern)

Example 22 with Money

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

the class TestCompiledMoneyExpression method testSubtractWithMoney.

@Test
public void testSubtractWithMoney() {
    Operand result;
    result = Expression.evaluate("ToMoney(15) - ToMoney(10)", null, null, null);
    Assert.assertEquals(new Money(5), result.value);
    result = Expression.evaluate("15 - ToMoney(10)", null, null, null);
    Assert.assertEquals(new Money(5), result.value);
    result = Expression.evaluate("ToMoney(15) - 10", null, null, null);
    Assert.assertEquals(new Money(5), result.value);
    result = Expression.evaluate("ToLong(15) - ToMoney(10)", null, null, null);
    Assert.assertEquals(new Money(5), result.value);
    result = Expression.evaluate("ToMoney(15) - ToLong(10)", null, null, null);
    Assert.assertEquals(new Money(5), result.value);
    result = Expression.evaluate("15.0 - ToMoney(10)", null, null, null);
    Assert.assertEquals(new Money(5), result.value);
    result = Expression.evaluate("ToMoney(15) - 10.0", null, null, null);
    Assert.assertEquals(new Money(5), result.value);
}
Also used : Money(com.dexels.navajo.document.types.Money) Operand(com.dexels.navajo.document.Operand) Test(org.junit.Test)

Example 23 with Money

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

the class UtilsMoneyTest method testAddMoneyAndInteger.

@Test
public void testAddMoneyAndInteger() {
    Money a = new Money(15);
    Integer b = 10;
    Money result = (Money) Utils.add(a, b, "ToMoney(15) + 10");
    assertEquals(new Money(25), result);
}
Also used : Money(com.dexels.navajo.document.types.Money) Test(org.junit.Test)

Example 24 with Money

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

the class UtilsMoneyTest method testSubtractLongAndMoney.

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

Example 25 with Money

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

the class UtilsMoneyTest method testAddTwoMoneys.

// ADD
@Test
public void testAddTwoMoneys() {
    Money a = new Money(15);
    Money b = new Money(10);
    Money result = (Money) Utils.add(a, b, "ToMoney(15) + ToMoney(10)");
    assertEquals(new Money(25), result);
}
Also used : Money(com.dexels.navajo.document.types.Money) Test(org.junit.Test)

Aggregations

Money (com.dexels.navajo.document.types.Money)39 Test (org.junit.Test)25 Percentage (com.dexels.navajo.document.types.Percentage)10 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)8 ClockTime (com.dexels.navajo.document.types.ClockTime)6 Navajo (com.dexels.navajo.document.Navajo)5 Property (com.dexels.navajo.document.Property)5 Date (java.util.Date)5 Operand (com.dexels.navajo.document.Operand)4 Message (com.dexels.navajo.document.Message)3 NavajoException (com.dexels.navajo.document.NavajoException)3 Binary (com.dexels.navajo.document.types.Binary)3 StopwatchTime (com.dexels.navajo.document.types.StopwatchTime)3 StringWriter (java.io.StringWriter)3 DatePattern (com.dexels.navajo.document.types.DatePattern)2 SystemException (com.dexels.navajo.script.api.SystemException)2 StringReader (java.io.StringReader)2 ExpressionChangedException (com.dexels.navajo.document.ExpressionChangedException)1 PropertyTypeException (com.dexels.navajo.document.PropertyTypeException)1 BaseMessageImpl (com.dexels.navajo.document.base.BaseMessageImpl)1