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 |
package org.gvsig.expressionevaluator.impl.function.date; |
---|---|
2 |
|
3 |
import java.text.ParseException; |
4 |
import java.text.SimpleDateFormat; |
5 |
import java.util.Date; |
6 |
import java.util.Objects; |
7 |
import org.apache.commons.lang3.Range; |
8 |
import org.gvsig.expressionevaluator.Interpreter; |
9 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
10 |
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 |
|
19 |
public class RelativeIntervalFunction extends AbstractFunction { |
20 |
|
21 |
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 |
new String[]{ |
28 |
"date1 - An object that represents a moment of time.",
|
29 |
"date2 - An object that represents a moment of time."
|
30 |
}, |
31 |
"RelativeInterval",
|
32 |
false
|
33 |
); |
34 |
this.toInstant = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.INSTANT);
|
35 |
} |
36 |
|
37 |
@Override
|
38 |
public boolean allowConstantFolding() { |
39 |
return true; |
40 |
} |
41 |
|
42 |
@Override
|
43 |
public Object call(Interpreter interpreter, Object[] args) { |
44 |
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 |
} |
59 |
|
60 |
|
61 |
|