用Java根据错误值解析出错误类型和错误码

来源:互联网 发布:开淘宝店铺怎么描述 编辑:程序博客网 时间:2024/05/22 00:37
public class ErrorParser {public static void main(String[] args) {int ret = encodeDepError(Constants.ELEM_POOL, ErrorCode.STATUS| ErrorCode.LACK | ErrorCode.PROBED_FAILED);String retString = decodeDepError(ret);System.out.println(retString);}public static int encodeDepError(int type, int errorCode) {return -(type << 16 | errorCode);}public static String decodeDepError(int error) {int type;int errorCode;int positiveError = -error;type = positiveError >> 16;errorCode = positiveError & 0xFFFF;System.out.println("type = " + type);System.out.println("errorCode = " + errorCode);return "";}}interface ErrorCode {public static final int DUP = 1 << 0;public static final int STATUS = 1 << 1;public static final int LACK = 1 << 2;public static final int PROBED_FAILED = 1 << 3;}interface Constants {public static final int ELEM_CLUSTER = 0;public static final int ELEM_HOST = 1;public static final int ELEM_MON = 2;public static final int ELEM_DISK = 3;public static final int ELEM_OSD = 4;public static final int ELEM_MDS = 5;public static final int ELEM_POOL = 6;public static final int ELEM_ACCS = 7;public static final int ELEM_NTYPES = 8;}

输出为:

type =6

errorCode = 14



0 0