svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / programming / RangeFunction.java @ 44207
History | View | Annotate | Download (2.79 KB)
1 | 44138 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|---|---|
2 | 43512 | jjdelcerro | |
3 | 44138 | jjdelcerro | import java.util.Iterator; |
4 | 43512 | jjdelcerro | import org.apache.commons.lang3.Range; |
5 | 44207 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_RANGE; |
6 | 44138 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
7 | import org.gvsig.expressionevaluator.Function; |
||
8 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
9 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
10 | |||
11 | 44138 | jjdelcerro | public class RangeFunction extends AbstractFunction { |
12 | 43512 | jjdelcerro | |
13 | 44138 | jjdelcerro | public RangeFunction() {
|
14 | 43512 | jjdelcerro | super(
|
15 | 44138 | jjdelcerro | Function.GROUP_PROGRAMMING, |
16 | 44207 | jjdelcerro | FUNCTION_RANGE, |
17 | 44138 | jjdelcerro | Range.between(1,3), |
18 | "",
|
||
19 | 44207 | jjdelcerro | FUNCTION_RANGE+"({{start}}, stop, step)",
|
20 | 44138 | jjdelcerro | null,
|
21 | "Iterable",
|
||
22 | 44207 | jjdelcerro | false
|
23 | 43512 | jjdelcerro | ); |
24 | } |
||
25 | 44138 | jjdelcerro | |
26 | 43512 | jjdelcerro | @Override
|
27 | 44009 | jjdelcerro | public boolean allowConstantFolding() { |
28 | 44138 | jjdelcerro | return false; |
29 | } |
||
30 | 44009 | jjdelcerro | |
31 | @Override
|
||
32 | 44138 | jjdelcerro | public Object call(Interpreter interpreter, final Object[] args) throws Exception { |
33 | switch(args.length) {
|
||
34 | case 1: |
||
35 | return new Iterable<Long>() { |
||
36 | @Override
|
||
37 | public Iterator<Long> iterator() { |
||
38 | return new XRangeIterator(0, getLong(args, 0), 1); |
||
39 | } |
||
40 | }; |
||
41 | case 2: |
||
42 | return new Iterable<Long>() { |
||
43 | @Override
|
||
44 | public Iterator<Long> iterator() { |
||
45 | return new XRangeIterator(getLong(args, 0), getLong(args, 1), 1); |
||
46 | } |
||
47 | }; |
||
48 | case 3: |
||
49 | return new Iterable<Long>() { |
||
50 | @Override
|
||
51 | public Iterator<Long> iterator() { |
||
52 | return new XRangeIterator(getLong(args, 0), getLong(args, 1), getLong(args, 2)); |
||
53 | } |
||
54 | }; |
||
55 | default:
|
||
56 | // Aqui no deberia llegar, el interprete deberia producir un error
|
||
57 | // antes, al comprobar el numero de argumentos de esta funcion.
|
||
58 | throw new ExpressionRuntimeException("Incorrect number of arguments in function range."); |
||
59 | } |
||
60 | 43512 | jjdelcerro | } |
61 | 44138 | jjdelcerro | |
62 | private static class XRangeIterator implements Iterator<Long> { |
||
63 | 43512 | jjdelcerro | |
64 | 44138 | jjdelcerro | private final long start; |
65 | private final long stop; |
||
66 | private final long step; |
||
67 | private long current; |
||
68 | |||
69 | public XRangeIterator(long start, long stop, long step) { |
||
70 | this.start = start;
|
||
71 | this.stop = stop;
|
||
72 | this.step = step;
|
||
73 | this.current = start;
|
||
74 | } |
||
75 | |||
76 | @Override
|
||
77 | public boolean hasNext() { |
||
78 | return this.current < this.stop; |
||
79 | } |
||
80 | |||
81 | @Override
|
||
82 | public Long next() { |
||
83 | long n = this.current; |
||
84 | this.current += this.step; |
||
85 | return n;
|
||
86 | } |
||
87 | |||
88 | } |
||
89 | |||
90 | 43512 | jjdelcerro | } |