Statistics
| Revision:

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
package org.gvsig.expressionevaluator.impl.function.programming;
2

    
3
import java.util.Iterator;
4
import org.apache.commons.lang3.Range;
5
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_RANGE;
6
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
7
import org.gvsig.expressionevaluator.Function;
8
import org.gvsig.expressionevaluator.Interpreter;
9
import org.gvsig.expressionevaluator.spi.AbstractFunction;
10

    
11
public class RangeFunction extends AbstractFunction {
12

    
13
    public RangeFunction() {
14
        super(
15
            Function.GROUP_PROGRAMMING, 
16
            FUNCTION_RANGE, 
17
            Range.between(1,3),
18
            "",
19
            FUNCTION_RANGE+"({{start}}, stop, step)",
20
            null,
21
            "Iterable",
22
            false
23
        );
24
    }
25
    
26
    @Override
27
    public boolean allowConstantFolding() {
28
        return false;
29
    }    
30
    
31
    @Override
32
    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
    }
61
    
62
    private static class XRangeIterator implements Iterator<Long> {
63

    
64
        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
}