All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.cn9750wang.webtools.error.ErrorCode Maven / Gradle / Ivy

The newest version!
/*
 *    Copyright 2021 wwy
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.gitee.cn9750wang.webtools.error;


import java.io.Serializable;

/**
 * 错误码对象
 * 错误码字符串形式应该由6位组成
 * 字符串格式:PP-T-NNN
 * P:项目或模块名称,占两位十进制
 * T:错误级别或类型,占一位十进制
 * N:具体错误编号,项目内自增,占三位十进制
 *
 * @author wwy
 */
public final class ErrorCode implements Serializable {

    /** 错误代码 */
    private final int code;

    /** 具体错误编号最大值 */
    private static final int MAX_NUM = 999;
    /** 错误代号最大值 */
    private static final int MAX_ERR_CODE = 999999;
    /**
     * 错误代码对象构造函数
     * @param code 错误码数字编号
     */
    public ErrorCode(int code){
        if(code < 0 || code >= MAX_ERR_CODE){
            throw new IllegalArgumentException("错误码数字变号范围不正确");
        }
        this.code = code;
    }

    /**
     * 错误码对象构造函数
     * @param project P:项目或模块名称,占两位十进制
     * @param type T:错误级别或类型,占一位十进制
     * @param num N:具体错误编号,项目内自增,占三位十进制
     */
    public ErrorCode(byte project, ErrorType type, short num) {
        var check = project >= 0 && project <= 99;
        check = check && (num >= 0 && num <= MAX_NUM);
        if(!check){
            throw new IllegalArgumentException("错误码格式参数不正确");
        }
        this.code = (project * 10_000) + (type.getTypeNum() * 1_000) + num;
    }

    /**
     * 错误码对象构造函数
     * @param project P:项目或模块名称,占两位十进制
     * @param type T:错误级别或类型,占一位十进制
     */
    public ErrorCode(byte project, ErrorType type) {
        this(project,type, (short) 0);
    }

    /**
     * 对 N:具体错误编号增长指定值
     * @param num 增长值
     * @return 新生成的错误编码对象
     */
    public ErrorCode increase(int num){
        if(num <= 0){
            throw new IllegalArgumentException("增长步长不能<=0");
        }
        if((code % 1000 + num) < MAX_NUM){
            // 自增一位
            return new ErrorCode(code+1);
        }
        throw new RuntimeException("错误编码溢出,["+this.toString()+"]已是当前类型编码最大值");
    }

    /**
     * 对 N:具体错误编号增长一位
     * @return 新生成的错误编码对象
     */
    public ErrorCode increase(){
        return this.increase(1);
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ErrorCode errorCode = (ErrorCode) o;
        return errorCode.code == this.code;
    }

    @Override
    public int hashCode() {
        return code;
    }

    @Override
    public String toString() {
        return String.valueOf(code);
    }

    public int getCode() {
        return code;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy