Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.time / org.gvsig.expressionevaluator.time.lib / org.gvsig.expressionevaluator.time.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / date / RelativeIntervalFunction.java @ 44644

History | View | Annotate | Download (2.21 KB)

1 43987 jjdelcerro
package org.gvsig.expressionevaluator.impl.function.date;
2 43512 jjdelcerro
3 43987 jjdelcerro
import java.text.ParseException;
4
import java.text.SimpleDateFormat;
5
import java.util.Date;
6 44080 jjdelcerro
import java.util.Objects;
7 43512 jjdelcerro
import org.apache.commons.lang3.Range;
8 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
9 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
10 44080 jjdelcerro
import org.gvsig.timesupport.DataTypes;
11
import org.gvsig.timesupport.Instant;
12
import org.gvsig.timesupport.RelativeInstant;
13
import org.gvsig.timesupport.RelativeInterval;
14
import org.gvsig.timesupport.TimeSupportLocator;
15
import org.gvsig.tools.ToolsLocator;
16
import org.gvsig.tools.dataTypes.CoercionException;
17
import org.gvsig.tools.dataTypes.DataTypesManager;
18 43512 jjdelcerro
19 44080 jjdelcerro
public class RelativeIntervalFunction extends AbstractFunction {
20 43512 jjdelcerro
21 44080 jjdelcerro
    private final DataTypesManager.Coercion toInstant;
22
23
    public RelativeIntervalFunction() {
24
        super("Date", "RelativeInterval",Range.is(2),
25
            "Returns a relative interval from the arguments",
26
            "RelativeInterval({{date1}}, data2)",
27 43983 jjdelcerro
            new String[]{
28 44080 jjdelcerro
                "date1 - An object that represents a moment of time.",
29
                "date2 - An object that represents a moment of time."
30 43983 jjdelcerro
            },
31 44080 jjdelcerro
            "RelativeInterval",
32
            false
33 43983 jjdelcerro
        );
34 44080 jjdelcerro
        this.toInstant = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.INSTANT);
35 43512 jjdelcerro
    }
36
37
    @Override
38 44009 jjdelcerro
    public boolean allowConstantFolding() {
39
        return true;
40
    }
41
42
    @Override
43 43521 jjdelcerro
    public Object call(Interpreter interpreter, Object[] args) {
44 44080 jjdelcerro
            Object arg0 = getObject(args, 0);
45
            Object arg1 = getObject(args, 1);
46
            try {
47
                RelativeInstant instant0 = (RelativeInstant) this.toInstant.coerce(arg0);
48
                RelativeInstant instant1 = (RelativeInstant) this.toInstant.coerce(arg1);
49
                RelativeInterval interval = TimeSupportLocator.getManager().createRelativeInterval(
50
                    instant0,
51
                    instant1
52
                );
53
                return interval;
54
            } catch (CoercionException ex) {
55
                throw new RuntimeException("Can't create an interval from '"+Objects.toString(arg0, "")+":"+Objects.toString(arg1, "")+"'.", ex);
56
            }
57
        }
58 43512 jjdelcerro
    }
59
60