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 @ 43989

History | View | Annotate | Download (1.46 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
            true
24
        );
25
    }
26

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

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

    
46
}