1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 package org.jaxen.function;
50
51 import java.util.List;
52
53 import org.jaxen.Context;
54 import org.jaxen.Function;
55 import org.jaxen.FunctionCallException;
56 import org.jaxen.Navigator;
57
58 /***
59 * <p><b>4.4</b> <code><i>number</i> floor(<i>number</i>)</code></p>
60 *
61 *
62 * <blockquote src="http://www.w3.org/TR/xpath">
63 * The floor function returns the largest (closest to positive infinity)
64 * number that is not greater than the argument and that is an integer....
65 * If the argument is NaN, then NaN is returned.
66 * If the argument is positive infinity, then positive infinity is returned.
67 * If the argument is negative infinity, then negative infinity is returned.
68 * If the argument is positive zero, then positive zero is returned.
69 * If the argument is negative zero, then negative zero is returned.
70 * If the argument is greater than zero, but less than 1, then positive zero is returned.
71 * </blockquote>
72 *
73 * @author bob mcwhirter (bob @ werken.com)
74 *
75 * @see <a href="http://www.w3.org/TR/xpath#function-floor" target="_top">Section 4.4 of the XPath Specification</a>
76 * @see <a href="http://www.w3.org/1999/11/REC-xpath-19991116-errata/" target="_top">XPath Errata</a>
77 */
78 public class FloorFunction implements Function
79 {
80
81 /***
82 * Create a new <code>FloorFunction</code> object.
83 */
84 public FloorFunction() {}
85
86 /*** Returns the largest integer less than or equal to a number.
87 *
88 * @param context the context at the point in the
89 * expression when the function is called
90 * @param args a list with exactly one item which will be converted to a
91 * <code>Double</code> as if by the XPath <code>number()</code> function
92 *
93 * @return a <code>Double</code> containing the largest integer less than or equal
94 * to <code>args.get(0)</code>
95 *
96 * @throws FunctionCallException if <code>args</code> has more or less than one item
97 */
98 public Object call(Context context,
99 List args) throws FunctionCallException
100 {
101 if (args.size() == 1)
102 {
103 return evaluate( args.get(0),
104 context.getNavigator() );
105 }
106
107 throw new FunctionCallException( "floor() requires one argument." );
108 }
109
110 /*** Returns the largest integer less than or equal to the argument.
111 * If necessary, the argument is first converted to a <code>Double</code>
112 * as if by the XPath <code>number()</code> function.
113 *
114 * @param obj the object whose floor is returned
115 * @param nav ignored
116 *
117 * @return a <code>Double</code> containing the largest integer less
118 * than or equal to <code>obj</code>
119 */
120 public static Double evaluate(Object obj,
121 Navigator nav)
122 {
123 Double value = NumberFunction.evaluate( obj,
124 nav );
125
126 return new Double( Math.floor( value.doubleValue() ) );
127 }
128 }
129