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 @ 43521
History | View | Annotate | Download (1.29 KB)
1 | 43512 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.numeric; |
---|---|---|---|
2 | |||
3 | import org.apache.commons.lang3.Range; |
||
4 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
5 | 43512 | jjdelcerro | 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 | null
|
||
18 | ); |
||
19 | } |
||
20 | |||
21 | @Override
|
||
22 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) { |
23 | 43512 | jjdelcerro | Object value = args[0]; |
24 | |||
25 | int bit = getInt(args, 1); |
||
26 | if( value instanceof Integer ) { |
||
27 | int mask = 1<<bit; |
||
28 | boolean r = (((Number) value).intValue() & mask) == mask; |
||
29 | return r;
|
||
30 | } |
||
31 | if( value instanceof Number ) { |
||
32 | int mask = 1<<bit; |
||
33 | boolean r = (((Number) value).longValue() & mask) == mask; |
||
34 | return r;
|
||
35 | } |
||
36 | throw new IllegalArgumentException("Types not allowed in '"+name()+"' function."); |
||
37 | } |
||
38 | |||
39 | |||
40 | } |