Search in sources :

Example 1 with DatePattern

use of com.dexels.navajo.document.types.DatePattern 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 2 with DatePattern

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

the class ASTDatePatternNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    ContextExpression y = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Year (item 0) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression m = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Month (item 1) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression d = jjtGetChild(2).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Day (item 2) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression h = jjtGetChild(3).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Hour (item 3) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression min = jjtGetChild(4).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Minute (item 4) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression s = jjtGetChild(5).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Second (item 5) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    final boolean isLiteral = y.isLiteral() && m.isLiteral() && d.isLiteral() && h.isLiteral() && min.isLiteral() && s.isLiteral();
    return new ContextExpression() {

        @Override
        public boolean isLiteral() {
            return isLiteral;
        }

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            int yearT = ((Integer) y.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int monthT = ((Integer) m.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int dayT = ((Integer) d.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int hourT = ((Integer) h.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int minT = ((Integer) min.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int secT = ((Integer) s.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            return Operand.ofDatePattern(new DatePattern(yearT, monthT, dayT, hourT, minT, secT, true));
        }

        @Override
        public Optional<String> returnType() {
            return Optional.of(Property.DATE_PATTERN_PROPERTY);
        }

        @Override
        public String expression() {
            return expression;
        }
    };
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Optional(java.util.Optional) Selection(com.dexels.navajo.document.Selection) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) TipiLink(com.dexels.navajo.expression.api.TipiLink) DatePattern(com.dexels.navajo.document.types.DatePattern)

Example 3 with DatePattern

use of com.dexels.navajo.document.types.DatePattern 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)

Aggregations

DatePattern (com.dexels.navajo.document.types.DatePattern)3 ClockTime (com.dexels.navajo.document.types.ClockTime)2 Money (com.dexels.navajo.document.types.Money)2 Percentage (com.dexels.navajo.document.types.Percentage)2 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)2 Date (java.util.Date)2 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)1 Message (com.dexels.navajo.document.Message)1 Navajo (com.dexels.navajo.document.Navajo)1 Selection (com.dexels.navajo.document.Selection)1 StopwatchTime (com.dexels.navajo.document.types.StopwatchTime)1 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)1 TipiLink (com.dexels.navajo.expression.api.TipiLink)1 Access (com.dexels.navajo.script.api.Access)1 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)1 Calendar (java.util.Calendar)1 Optional (java.util.Optional)1