Java教程

java对枚举类型进行自动化匹配

本文主要是介绍java对枚举类型进行自动化匹配,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.datasure.movesure.util.exception;

import com.datasure.movesure.base.core.exception.IErrorCode;

/**
 * @ClassName: ShellUtilError
 * @Description:
 * @Author: Datasure008
 * @Date: 2021/9/8 11:21
 */
public enum ShellUtilError implements IErrorCode {
    /**
     *  * 200	找不到paramiko模块,无法执行远程传输
     * 			 * 201	找不到配置文件(json)
     * 			 * 202	配置文件(json)解析失败
     * 			 * 203	参数无效
     * 			 * 204	连接失败
     * 			 * 205	没有找到可用的安装包(linux_client_common_install.zip)
     * 			 * 206	解压客户端安装包失败!
     * 			 * 207	推送客户端安装包失败 (蓝色表示win新增)WINDOWS特有
     * 			 * 209	密码错误
     *
     * 			 * 101	检测到已安装,请先卸载
     * 			 * 102	安装包不完整,缺少必要的文件
     * 			 * 103	安装驱动失败!
     * 			 * 104	添加DS引导项失败!
     * 			 * 105	安装客户端程序失败!
     * 			 * 106	无法获取客户端代理程序包
     * 			 * 110	当前系统不支持安装/卸载
     * 			 * 111	参数解析错误
     * 			 *
     * 			 *
     * 			 * 0	执行成功
     */
    SUCCESS("110000","成功"),
    NOT_FOUND_PARAMIKO_MODULE_ERROR("110200","paramiko模块,无法执行远程传输"),
    NOT_FOUND_CONF_FILE_ERROR("110201","找不到配置文件(json)"),
    JSON_PARSE_ERROR("110202","配置文件(json)解析失败"),
    PARAM_NOTEFFECT_ERROR("110203","参数无效"),
    LINK_FAILED_ERROR("110204","连接失败"),
    NOT_FOUND_INSTALL_PACKAGE_ERROR("110205","没有找到可用的安装包(linux_client_common_install.zip)"),
    UNZIP_ERROR("110206","解压客户端安装包失败!"),
    PASSWORD_ERROR("110209","推送客户端安装包失败 !"),
    HAVE_INSTALLED_ERROR("110101","检测到已安装,请先卸载"),
    FILE_NOT_FILL_ERROR("110102","安装包不完整,缺少必要的文件"),
    INSTALL_DERVER_FAIL_ERROR("110103","安装驱动失败!"),
    DS_FILL_FAIL_ERROR("110104","添加DS引导项失败!"),
    INSTALL_CLIENT_FAIL_ERROR("110105","安装客户端程序失败!"),
    PROXY_PACKAGE_FAIL_ERROR("110106","法获取客户端代理程序包"),
    LINUX_PROXY_PACKAGE_FAIL_ERROR("110107","执行远程命令失败"),
    NOT_SUPPORT_INSTALL_OR_UNINSTALL_ERROR("110110","当前系统不支持安装/卸载"),
    PARAM_PARSE_ERROR("110111","参数解析错误"),
    WINDOWS_PUSH_PACKAGE_FAIL_ERROR("110207","下载安装包失败"),
    ;

    private String errorCode;
    private String errorMessage;

    ShellUtilError(String errorCode, String errorMessage) {
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    @Override
    public String getErrorCode() {
        return this.errorCode;
    }

    @Override
    public String getErrorMessage() {
        return this.errorMessage;
    }


}

  /**
                 * auth:jiaozongguan
                 * 处理各种异常
                 */
                ShellUtilError[] shellUtilErrors = ShellUtilError.values();
                List<ShellUtilError> shellUtilErrorList = new ArrayList<>();
                shellUtilErrorList = Arrays.asList(shellUtilErrors);

                int finalExitCode = exitCode;
                ShellUtilError shellUtilError = shellUtilErrorList.stream().filter(
                        m -> m.getErrorCode().substring(3, 6).equals(String.valueOf(finalExitCode))).findFirst().get();
                // complete failed.
                if (shellUtilError != null) {

                    throw new WebsocketException(shellUtilError);
                }
package com.datasure.movesure.base.core.exception;

/**
 * @ClassName: WebsocketException
 * @Description:websocket消息异常,统一处理
 * @Author: jiaozongguan
 * @Date: 2021/8/10 14:04
 */
public class WebsocketException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    protected String errorCode;
    /**
     * 错误信息
     */
    protected String errorMessage;

    public WebsocketException() {
        super();
    }

    public WebsocketException(IErrorCode errorCode) {
        super(errorCode.getErrorMessage());
        this.errorCode = errorCode.getErrorCode();
        this.errorMessage = errorCode.getErrorMessage();
    }

    public WebsocketException(IErrorCode errorCode, Throwable cause) {
        super(errorCode.getErrorMessage(), cause);
        this.errorCode = errorCode.getErrorCode();
        this.errorMessage = errorCode.getErrorMessage();
    }

    public WebsocketException(String errorMsg) {
        super(errorMsg);
        this.errorMessage = errorMsg;
    }

    public WebsocketException(String errorCode, String errorMsg) {
        super(errorMsg);
        this.errorCode = errorCode;
        this.errorMessage = errorMsg;
    }

    public WebsocketException(String errorCode, String errorMsg, Throwable cause) {
        super(errorMsg, cause);
        this.errorCode = errorCode;
        this.errorMessage = errorMsg;
    }


    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getMessage() {
        return errorMessage;
    }

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }
}

这篇关于java对枚举类型进行自动化匹配的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!