java小工具util系列4:基础工具代码(Msg、PageResult、Response、常量、枚举)

举报
刘大猫 发表于 2024/11/27 21:09:28 2024/11/27
【摘要】 java小工具util系列4:基础工具代码(Msg、PageResult、Response、常量、枚举)

@[TOC]

说明:基础工具类

image.png

Msg

package com.geespace.microservices.data.computing.model.server.response;

import java.util.HashMap;
import java.util.Map;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * Msg
 * 
 * @author:
 * @date: 2020-06-18
 */
@AllArgsConstructor
@Getter
public enum Msg {

    /**
     * 请求类型错误
     */
    SUCCESS(200, "success"), FAILED(20001, "Operation failed"),
    EXCUTE_ERROR(16001, "Service invocation exception, please try again later"),
    UNKNOW_ERROR(16002, "unknown error"),
    PARAM_ERROR(16003, "request param error"),
    DATA_SAVE_FAIL(16004, "data save fail"),
    ID_IS_NOT_EXIST(16005, "id isn't exist"),
    QUERY_RESULT_IS_EMPTY(16006, "request result is null"),
    NAME_IS_EXIST(16007, "name already exist"),
    ID_IS_NOT_EXIST_DATA(16008, "There is no data for the current ID"),
    DATASOURCE_NOT_EXIST(16009, "datasource not exist"),
    JDBC_EXCEPTION(16010, "jdbc connection or search exception"),
    TASK_HAS_BEEN_STOPPED(16011, "task has been stopped");

    private int code;
    private String msg;

    private static final Map<Integer, Msg> MAP = new HashMap<>();

    static {
        for (Msg msgEnum : Msg.values()) {
            MAP.put(msgEnum.code, msgEnum);
        }
    }

    /**
     * 通过code获取枚举
     *
     * @param code
     *            code
     * @return 枚举
     */
    public static Msg getByCode(int code) {
        return MAP.get(code);
    }
}

PageResult

package com.geespace.microservices.data.computing.model.server.response;

import java.io.Serializable;
import java.util.List;

import lombok.Data;

/**
 *
 * @param <T>
 *            Construct return entity
 * @author liudz
 * @since Oracle JDK1.8
 **/
@Data
public class PageResult<T> implements Serializable {
    private int pageNum;
    private int pageSize;
    private long totalCount;
    private long totalPage;
    private List<T> list;
}

Response

package com.geespace.microservices.data.computing.model.server.response;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * merge fromn iot
 * 
 * @param <T>
 * @Author: Mickey
 * @Date: 2019/9/9 {TIME}
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public final class Response<T> implements Serializable {

    private int code;
    private String msg;
    private T info;

    private Response(Msg msg) {
        this(msg, null);
    }

    private Response(Msg msg, T info) {
        this.code = msg.getCode();
        this.msg = msg.getMsg();
        this.info = info;
    }

    private Response(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * Return to success
     *
     * @param <T>
     *            Return info
     * @return <T> Success
     */
    public static <T> Response<T> success() {
        return success(null);
    }

    /**
     * Return to success
     *
     * @param t
     *            Return info
     * @param <T>
     *            Return generic
     * @return <T> Return entity
     */
    public static <T> Response<T> success(T t) {
        return new Response<>(Msg.SUCCESS, t);
    }

    /**
     * Return failed
     *
     * @param msg
     *            result enumeration
     * @param <T>
     *            Return info
     * @return <T> Return entity
     */
    public static <T> Response<T> error(Msg msg) {
        return new Response<>(msg);
    }

    /**
     * Return failed
     *
     * @param code
     *            Error code
     * @param msg
     *            Error message
     * @param <T>
     *            Return info
     * @return <T> Return entity
     */
    public static <T> Response<T> error(int code, String msg) {
        return new Response<>(code, msg);
    }

    /**
     *
     * @author luke liu
     * @param <T>
     *            t
     * @param msg
     *            error msg
     * @return error result
     **/
    public static <T> Response<T> error(String msg) {
        return new Response(msg);
    }

    /**
     * Determine whether response is successful in returning
     *
     * @return Success
     */
    public boolean responseSuccess() {
        if (this.code == Msg.SUCCESS.getCode()) {
            return true;
        }
        return false;
    }

    private Response(String msg) {
        this.code = Msg.FAILED.getCode();
        this.msg = msg;
        this.info = null;
    }
}

BusinessDomainEnum(枚举)

package com.geespace.microservices.directory.assets.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 业务域
 * @Author: liudz
 * @Date: 2020/6/22
 * @Version 1.0
 */
@AllArgsConstructor
@Getter
public enum BusinessDomainEnum {
    /**
     * 类型1
     */
    ONE(1, "低轨物联"),
    /**
     * 类型2
     */
    TWO(2, "大网");
    private int category;
    private String value;

    /**
     * @author      liudz
     * @param       category category
     * @return      ture or false
     **/
    public static String transfer(int category) {
        BusinessDomainEnum[] values = BusinessDomainEnum.values();
        for (BusinessDomainEnum subEnum : values) {
            if (category == subEnum.getCategory()) {
                return subEnum.getValue();
            }
        }
        return "error";
    }
}

EsDocumentConstants(常量)

package com.geespace.microservices.directory.assets.constants;

/**
 * 资产目录功能相关符号常量
 * @author: liudz
 * @date: 2020/9/15
 */
public final class EsDocumentConstants {
    private EsDocumentConstants(){}

    public static final String FILE_NAME = "file_name";
    public static final String FILE_TYPE = "file_type";
    public static final String DATABASE_NAME = "database_name";
    public static final String TABLE_NAME = "table_name";
    public static final String INCLUDE_FIELDS = "include_fields";
    public static final String BUSINESS_DOMAIN = "business_domain";
    public static final String STORE_TYPE = "store_type";
    public static final String UPDATE_TIME = "update_time";
    public static final String WHETHER_ONLINE = "whether_online";
    public static final String FOREIGN_ID = "foreign_id";
}

本人其他文章链接

1.java小工具util系列1:日期毫秒数转日期字符串
https://blog.csdn.net/a924382407/article/details/121955349

2.java小工具util系列2:获取字符modelStr在字符串str中第count次出现时的下标
https://blog.csdn.net/a924382407/article/details/121955455

3.java小工具util系列3:正则表达式匹配:匹配不包含@特殊字符的字符串
https://blog.csdn.net/a924382407/article/details/121955737

4.java小工具util系列4:String[] 转 List< Integer >
https://blog.csdn.net/a924382407/article/details/121956201

5.java小工具util系列5:基础工具代码(Msg、PageResult、Response、常量、枚举)
https://blog.csdn.net/a924382407/article/details/120952865

6.java小工具util系列6:java执行string返回boolean结果
https://blog.csdn.net/a924382407/article/details/117124536

7.java小工具util系列7:集合中实体对象转换 list中Enrey转Dto
https://blog.csdn.net/a924382407/article/details/121957545

8.java小工具util系列8:JSONObject获取key
https://blog.csdn.net/a924382407/article/details/121957607

9.java小工具util系列9:检测一个字符串是否是时间格式
https://blog.csdn.net/a924382407/article/details/123948881

10.java小工具util系列10:时间毫秒数、时间格式字符串、日期之间相互转化
https://blog.csdn.net/a924382407/article/details/124581851

image.png

重要信息

image.png
image.png
image.png

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。