OperatorBit.java 3.5 KB
Newer Older
T
tianqiao 已提交
1 2 3
package com.ql.util.express.instruction.op;

import com.ql.util.express.Operator;
4
import com.ql.util.express.exception.QLException;
T
tianqiao 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

import java.util.Arrays;

/**
 * Created by tianqiao on 16/12/15.
 */
public class OperatorBit extends Operator {
    
    public OperatorBit(String name)
    {
        this.name = name;
    }
    @Override
    public Object executeInner(Object[] list) throws Exception {
        if( this.name.equals("~")){
            if (list.length == 1 && list[0] instanceof Number) {
                if(list[0]instanceof Integer) {
                    return ~(((Number) list[0]).intValue());
                }else{
                    return ~(((Number) list[0]).longValue());
                }
            }else{
27
                throw new QLException("取反操作符 ~ 参数不合法:"+ Arrays.toString(list));
T
tianqiao 已提交
28 29 30 31 32 33 34 35 36
            }
        }
        if( this.name.equals("&")){
            if (list.length == 2 && list[0] instanceof Number && list[1] instanceof Number) {
                if(list[0]instanceof Integer && list[1] instanceof Integer){
                    return (Integer)list[0]&(Integer)list[1];
                }
                return (((Number)list[0]).longValue())&(((Number)list[1]).longValue());
            }else{
37
                throw new QLException("按位与操作符 & 两边的参数不合法:"+ Arrays.toString(list));
T
tianqiao 已提交
38 39 40 41 42 43 44 45 46
            }
        }
        if( this.name.equals("|")){
            if (list.length == 2 && list[0] instanceof Number && list[1] instanceof Number) {
                if(list[0]instanceof Integer && list[1] instanceof Integer){
                    return (Integer)list[0]|(Integer)list[1];
                }
                return (((Number)list[0]).longValue())|(((Number)list[1]).longValue());
            }else{
47
                throw new QLException("按位或操作符 | 两边的参数不合法:"+ Arrays.toString(list));
T
tianqiao 已提交
48 49 50 51 52 53 54 55 56
            }
        }
        if( this.name.equals("^")){
            if (list.length == 2 && list[0] instanceof Number && list[1] instanceof Number) {
                if(list[0]instanceof Integer && list[1] instanceof Integer){
                    return (Integer)list[0]^(Integer)list[1];
                }
                return (((Number)list[0]).longValue())^(((Number)list[1]).longValue());
            }else{
57
                throw new QLException("按位异或操作符 ^ 两边的参数不合法:"+ Arrays.toString(list));
T
tianqiao 已提交
58 59 60 61 62 63 64 65 66
            }
        }
        if( this.name.equals("<<")){
            if (list.length == 2 && list[0] instanceof Number && list[1] instanceof Number) {
                if(list[0]instanceof Integer && list[1] instanceof Integer){
                    return (Integer)list[0]<<(Integer)list[1];
                }
                return (((Number)list[0]).longValue())<<(((Number)list[1]).longValue());
            }else{
67
                throw new QLException("左移操作符 << 两边的参数不合法:"+ Arrays.toString(list));
T
tianqiao 已提交
68 69 70 71 72 73 74 75 76
            }
        }
        if( this.name.equals(">>")){
            if (list.length == 2 && list[0] instanceof Number && list[1] instanceof Number) {
                if(list[0]instanceof Integer && list[1] instanceof Integer){
                    return (Integer)list[0]>>(Integer)list[1];
                }
                return (((Number)list[0]).longValue())>>(((Number)list[1]).longValue());
            }else{
77
                throw new QLException("右移操作符 >> 两边的参数不合法:"+ Arrays.toString(list));
T
tianqiao 已提交
78 79
            }
        }
80
        throw new QLException("不支持的位运算操作符:"+ Arrays.toString(list));
T
tianqiao 已提交
81 82
    }
}