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 / numeric / BitGetFunction.java @ 43983

History | View | Annotate | Download (1.44 KB)

1
package org.gvsig.expressionevaluator.impl.function.numeric;
2

    
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.expressionevaluator.Interpreter;
5
import org.gvsig.expressionevaluator.spi.AbstractFunction;
6

    
7
public class BitGetFunction extends AbstractFunction {
8

    
9
    public BitGetFunction() {
10
        super(
11
            "Numeric", "BITGET", Range.is(2),
12
            "Returns true if and only if the first parameter has a bit set "
13
                + "in the position specified by the second parameter. "
14
                + "This method returns a boolean. "
15
                + "The second parameter is zero-indexed; the least significant "
16
                + "bit has position 0",
17
            "BITGET({{bits}}, nbit)",
18
            new String[]{
19
                "bits - a bit set",
20
                "nbit - bit to check"
21
            },
22
            "Boolean"
23
        );
24
    }
25

    
26
    @Override
27
    public Object call(Interpreter interpreter, Object[] args) {
28
        Object value = args[0];
29

    
30
        int bit = getInt(args, 1);
31
        if( value instanceof Integer ) {
32
            int mask = 1<<bit;
33
            boolean r = (((Number) value).intValue() & mask) == mask;
34
            return r;
35
        } 
36
        if( value instanceof Number ) {
37
            int mask = 1<<bit;
38
            boolean r = (((Number) value).longValue() & mask) == mask;
39
            return r;
40
        } 
41
        throw new IllegalArgumentException("Types not allowed in '"+name()+"' function.");
42
    }
43
    
44

    
45
}