Search in sources :

Example 1 with StopwatchTime

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

the class ToMilliseconds method main.

public static void main(String[] args) {
    ToMilliseconds ts = new ToMilliseconds();
    ts.reset();
    System.err.println("Time: " + Calendar.getInstance().getTime());
    ts.insertStopwatchOperand(new StopwatchTime("01:22:45:234"));
    try {
        Object o = ts.evaluate();
        System.err.println("Millis: " + ((Integer) o).intValue());
    } catch (Exception e) {
        e.printStackTrace();
    }
    ts.reset();
    System.err.println("Time: " + Calendar.getInstance().getTime());
    ts.insertClockTimeOperand(new ClockTime("01:22"));
    try {
        Object o = ts.evaluate();
        System.err.println("Millis: " + ((Long) o).longValue());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : StopwatchTime(com.dexels.navajo.document.types.StopwatchTime) ClockTime(com.dexels.navajo.document.types.ClockTime)

Example 2 with StopwatchTime

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

the class TestProperty method tesSetAnyValue.

@Test
public void tesSetAnyValue() {
    Navajo n = NavajoFactory.getInstance().createNavajo();
    Property p1 = NavajoFactory.getInstance().createProperty(n, "Aap", "", "", "");
    // String
    p1.setAnyValue("Apenoot");
    assertEquals("string", p1.getType());
    assertEquals("Apenoot", p1.getValue());
    assertTrue(p1.getTypedValue().equals("Apenoot"));
    // Integer
    p1.setAnyValue(Integer.valueOf(50));
    assertEquals("integer", p1.getType());
    assertEquals("50", p1.getValue());
    assertTrue(p1.getTypedValue().equals(Integer.valueOf(50)));
    // Double
    p1.setAnyValue(Double.valueOf(50));
    assertEquals("float", p1.getType());
    assertEquals("50.0", p1.getValue());
    assertTrue(p1.getTypedValue().equals(Double.valueOf(50)));
    // Float
    p1.setAnyValue(Float.valueOf(50));
    assertEquals("float", p1.getType());
    assertEquals("50.0", p1.getValue());
    assertTrue(p1.getTypedValue().equals(Double.valueOf(50)));
    // Date
    Date d = new java.util.Date();
    p1.setAnyValue(d);
    String expectedFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS").format(d);
    assertEquals("date", p1.getType());
    assertEquals(expectedFormat, p1.getValue());
    assertTrue(p1.getTypedValue().equals(d));
    // Long
    p1.setAnyValue(Long.valueOf(10));
    assertEquals("long", p1.getType());
    assertEquals("10", p1.getValue());
    assertTrue(p1.getTypedValue().equals(Long.valueOf(10)));
    // Boolean
    p1.setAnyValue(Boolean.TRUE);
    assertEquals("boolean", p1.getType());
    assertEquals("true", p1.getValue());
    assertTrue(p1.getTypedValue().equals(Boolean.TRUE));
    // Binary
    Binary b = new Binary("Mooie array".getBytes());
    p1.setAnyValue(b);
    assertEquals("binary", p1.getType());
    Binary b1 = (Binary) p1.getTypedValue();
    String expected = new String(b1.getData());
    assertEquals("Mooie array", expected);
    // Money
    p1.setAnyValue(new Money(5000));
    assertEquals("money", p1.getType());
    assertEquals("5000.00", p1.getValue());
    assertTrue(p1.getTypedValue().equals(new Money(5000)));
    // ClockTime
    Date d1 = new java.util.Date();
    ClockTime ct = new ClockTime(d1);
    String expectedFormat2 = ct.toString();
    p1.setAnyValue(ct);
    assertEquals("clocktime", p1.getType());
    assertEquals(expectedFormat2, p1.getValue());
    assertTrue(p1.getTypedValue().equals(new ClockTime(d1)));
    // StopwatchTime
    Date d2 = new java.util.Date();
    String format = new SimpleDateFormat("HH:mm:ss:SSS").format(d2);
    StopwatchTime swt = new StopwatchTime(format);
    p1.setAnyValue(swt);
    assertEquals("stopwatchtime", p1.getType());
    logger.info("FORM: {} val: {}", format, p1.getValue());
    assertEquals(format, p1.getValue());
    assertTrue(p1.getTypedValue().equals(new StopwatchTime(format)));
    // Percentage
    Percentage p = new Percentage(50);
    p1.setAnyValue(p);
    assertEquals("percentage", p1.getType());
    assertEquals("50.0", p1.getValue());
    assertTrue(p1.getTypedValue().equals(new Percentage(50)));
}
Also used : StopwatchTime(com.dexels.navajo.document.types.StopwatchTime) Money(com.dexels.navajo.document.types.Money) Percentage(com.dexels.navajo.document.types.Percentage) Navajo(com.dexels.navajo.document.Navajo) Binary(com.dexels.navajo.document.types.Binary) ClockTime(com.dexels.navajo.document.types.ClockTime) Property(com.dexels.navajo.document.Property) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 3 with StopwatchTime

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

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

the class BasePropertyImpl method getTypedValue.

/**
 * Get the value of a property as a Java object.
 *
 * @return
 */
@Override
public final Object getTypedValue() {
    if (getType().equals(Property.STRING_PROPERTY)) {
        return getValue();
    }
    if (getType().equals(Property.BOOLEAN_PROPERTY)) {
        if (getValue() != null && !getValue().equals("")) {
            return Boolean.valueOf(getValue().equalsIgnoreCase("true"));
        } else {
            return null;
        }
    }
    if (getType().equals(EXPRESSION_LITERAL_PROPERTY)) {
        if (getValue() != null && !getValue().equals("")) {
            return new NavajoExpression(getValue());
        } else {
            return null;
        }
    }
    if (getType().equals(EXPRESSION_PROPERTY)) {
        if (evaluatedValue == null) {
            evaluatedValue = getEvaluatedValue();
            return evaluatedValue;
        } else {
            return evaluatedValue;
        }
    }
    if (getType().equals(Property.PERCENTAGE_PROPERTY)) {
        if (getValue() != null) {
            return new Percentage(getValue());
        } else {
            return null;
        }
    }
    if (getType().equals(Property.MONEY_PROPERTY)) {
        if (getValue() == null || "".equals(getValue())) {
            return new Money((Double) null, getSubType());
        }
        String val = getValue();
        NumberFormat fn = NumberFormat.getNumberInstance(Locale.US);
        Number parse;
        try {
            parse = fn.parse(val);
        } catch (ParseException e) {
            return null;
        }
        return new Money(parse.doubleValue(), getSubType());
    } else if (getType().equals(Property.CLOCKTIME_PROPERTY)) {
        if (getValue() == null || getValue().equals("")) {
            return null;
        }
        try {
            return new ClockTime(getValue(), getSubType());
        } catch (Exception e) {
            logger.error("Error: ", e);
        }
    } else if (getType().equals(Property.STOPWATCHTIME_PROPERTY)) {
        try {
            return new StopwatchTime(getValue(), getSubType());
        } catch (Exception e) {
            logger.error("Error: ", e);
        }
    } else if (getType().equals(Property.DATE_PROPERTY) || getType().equals(Property.TIMESTAMP_PROPERTY)) {
        if (getValue() == null || getValue().equals("") || getValue().equals("null")) {
            return null;
        }
        if (myDate != null) {
            return myDate;
        }
        // Try in order from most specific to least specific
        try {
            return timestampFormat.get().parse(getValue());
        } catch (Exception ex) {
            try {
                return dateFormat4.get().parse(getValue());
            } catch (Exception ex2) {
                try {
                    return dateFormat1.get().parse(getValue());
                } catch (Exception ex3) {
                    try {
                        return dateFormat2.get().parse(getValue());
                    } catch (Exception ex4) {
                        try {
                            Long l = Long.parseLong(getValue());
                            Date d = new java.util.Date();
                            d.setTime(l);
                            return d;
                        } catch (Exception e5) {
                            logger.info("Sorry I really can't parse that date: {}", getValue());
                            return null;
                        }
                    }
                }
            }
        }
    } else if (getType().equals(Property.INTEGER_PROPERTY)) {
        if (getValue() == null || getValue().equals("") || getValue().trim().equals("null")) {
            return null;
        }
        try {
            return Integer.valueOf(Integer.parseInt(getValue().trim()));
        } catch (NumberFormatException ex3) {
            logger.info("Numberformat exception...: {}", getValue().trim());
            return null;
        }
    } else if (getType().equals(Property.LONG_PROPERTY)) {
        if (getValue() == null || getValue().equals("")) {
            return null;
        }
        try {
            // Added a trim. Frank.
            return Long.valueOf(Long.parseLong(getValue().trim()));
        } catch (NumberFormatException ex3) {
            logger.info("Numberformat exception...");
            return null;
        }
    } else if (getType().equals(Property.FLOAT_PROPERTY)) {
        if (getValue() == null || getValue().equals("")) {
            return null;
        }
        String v = getValue();
        String w = v;
        // Sometimes the number formatting creates
        if (v.indexOf(',') != -1) {
            w = v.replaceAll(",", "");
        }
        Double d;
        try {
            d = Double.valueOf(Double.parseDouble(w));
        } catch (NumberFormatException ex) {
            logger.info("Can not format double with: {}", w);
            return null;
        }
        return d;
    } else if (getType().equals(Property.BINARY_PROPERTY)) {
        try {
            return myBinary;
        } catch (Exception e) {
            logger.error("Error: ", e);
        }
    } else if (getType().equals(Property.SELECTION_PROPERTY)) {
        return getAllSelectedSelections();
    } else if (getType().equals(Property.TIPI_PROPERTY)) {
        return tipiProperty;
    } else if (getType().equals(Property.LIST_PROPERTY)) {
        if (tipiProperty != null || myValue == null) {
            return tipiProperty;
        }
        try {
            if (myValue.indexOf('[') == 0) {
                // Parse back into a list
                String stripped = myValue.substring(1, myValue.length() - 1);
                tipiProperty = Arrays.asList(stripped.split(", "));
                return tipiProperty;
            } else if (myValue.length() > 0) {
                logger.info("Failed to parse {} as a list!", myValue);
            }
        } catch (Exception e) {
            logger.warn("Exception on parsing {} as a list!", myValue, e);
        }
        return null;
    } else if (getType().equals(Property.BINARY_DIGEST_PROPERTY)) {
        return new BinaryDigest(getValue());
    } else if (getType().equals(Property.COORDINATE_PROPERTY)) {
        try {
            return new Coordinate(myValue);
        } catch (Exception e) {
            logger.error("Cannot create Coordinate Property: ", e);
        }
    }
    return getValue();
}
Also used : NavajoExpression(com.dexels.navajo.document.types.NavajoExpression) Percentage(com.dexels.navajo.document.types.Percentage) Date(java.util.Date) ClockTime(com.dexels.navajo.document.types.ClockTime) PropertyTypeException(com.dexels.navajo.document.PropertyTypeException) NavajoException(com.dexels.navajo.document.NavajoException) ParseException(java.text.ParseException) ExpressionChangedException(com.dexels.navajo.document.ExpressionChangedException) IOException(java.io.IOException) Date(java.util.Date) StopwatchTime(com.dexels.navajo.document.types.StopwatchTime) Money(com.dexels.navajo.document.types.Money) BinaryDigest(com.dexels.navajo.document.types.BinaryDigest) Coordinate(com.dexels.navajo.document.types.Coordinate) ParseException(java.text.ParseException) NumberFormat(java.text.NumberFormat)

Example 5 with StopwatchTime

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

the class TestProperty method testStopwatch.

@Test
public void testStopwatch() {
    // Tests.
    StopwatchTime ck = new StopwatchTime(60000);
    logger.info("Hoea: {}", ck);
    StopwatchTime ck2 = new StopwatchTime("01");
    logger.info("ck3 = {}", ck2);
    StopwatchTime ck3 = new StopwatchTime("01:999");
    logger.info("ck3 = {}", ck3);
    StopwatchTime ck4 = new StopwatchTime("01:100");
    logger.info("ck4 = {}", ck4);
    StopwatchTime ck5 = new StopwatchTime("00:67:345", "format=SS.MM");
    logger.info("ck5 = {}", ck5);
    StopwatchTime ck6 = new StopwatchTime("02:01:10");
    logger.info("ck6 = {}", ck6);
    StopwatchTime ck7 = new StopwatchTime(1100);
    logger.info("ck7 = {}", ck7);
    StopwatchTime sw1 = new StopwatchTime("12:30:00:000");
    StopwatchTime sw2 = new StopwatchTime("04:45:12:976");
    StopwatchTime result = sw1.subtract(sw2);
    logger.info("result = {}", result);
    Assert.assertTrue(true);
}
Also used : StopwatchTime(com.dexels.navajo.document.types.StopwatchTime) Test(org.junit.Test)

Aggregations

StopwatchTime (com.dexels.navajo.document.types.StopwatchTime)6 ClockTime (com.dexels.navajo.document.types.ClockTime)5 Date (java.util.Date)4 Money (com.dexels.navajo.document.types.Money)3 Percentage (com.dexels.navajo.document.types.Percentage)3 Test (org.junit.Test)3 ExpressionChangedException (com.dexels.navajo.document.ExpressionChangedException)1 Navajo (com.dexels.navajo.document.Navajo)1 NavajoException (com.dexels.navajo.document.NavajoException)1 Property (com.dexels.navajo.document.Property)1 PropertyTypeException (com.dexels.navajo.document.PropertyTypeException)1 Binary (com.dexels.navajo.document.types.Binary)1 BinaryDigest (com.dexels.navajo.document.types.BinaryDigest)1 Coordinate (com.dexels.navajo.document.types.Coordinate)1 DatePattern (com.dexels.navajo.document.types.DatePattern)1 NavajoExpression (com.dexels.navajo.document.types.NavajoExpression)1 FunctionInterface (com.dexels.navajo.expression.api.FunctionInterface)1 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)1 IOException (java.io.IOException)1 NumberFormat (java.text.NumberFormat)1