常用java类

阅读更多

 

 

import com.google.gson.GsonBuilder;

/**
 * @ClassName: Result
 * @author kanpiaoxue
 * @version 1.0
 * @CreateTime: 2019/06/12 10:22:06
 * @Description: 结果通用类
 */
public class Result {
    public static final int SUCCESS_STATUS = 0;

    public static final int ERROR_STATUS = 1;

    private int status;

    private T data;

    private Integer errorCode;

    private String errorMessage;

    /**
     *
     */
    public Result() {
        super();
    }

    /**
     * @param status
     */
    public Result(int status) {
        super();
        this.status = status;
    }

    /**
     * @param status
     * @param errorCode
     * @param errorMessage
     */
    public Result(int status, Integer errorCode, String errorMessage) {
        super();
        this.status = status;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    /**
     * @param status
     * @param errorMessage
     */
    public Result(int status, String errorMessage) {
        super();
        this.status = status;
        this.errorMessage = errorMessage;
    }

    /**
     * @param status
     * @param data
     */
    public Result(int status, T data) {
        super();
        this.status = status;
        this.data = data;
    }

    /**
     * @param status
     * @param data
     * @param errorCode
     * @param errorMessage
     */
    public Result(int status, T data, Integer errorCode, String errorMessage) {
        super();
        this.status = status;
        this.data = data;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public static  Result error(Integer errorCode, String errorMessage) {
        return new Result(ERROR_STATUS, null, errorCode, errorMessage);
    }

    public static  Result error(Integer errorCode, String errorMessage, T data) {
        return new Result(ERROR_STATUS, data, errorCode, errorMessage);
    }

    public static  Result error(String errorMessage) {
        return new Result(ERROR_STATUS, errorMessage);
    }

    public static  Result error(String format, Object... args) {
        String errorMessage = String.format(format, args);
        return new Result(ERROR_STATUS, null, errorMessage);
    }

    public static  Result error(T data) {
        return new Result(ERROR_STATUS, data);
    }

    public static  Result success() {
        return new Result(SUCCESS_STATUS);
    }

    /**
     *
     * @param 
     * @param data
     * @return
     * @author kanpiaoxue
     * @CreateTime: 2019/06/12 10:28:06
     * @Description: 成功状态
     */
    public static  Result success(T data) {
        return new Result(SUCCESS_STATUS, data);
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Result other = (Result) obj;
        if (data == null) {
            if (other.data != null) {
                return false;
            }
        } else if (!data.equals(other.data)) {
            return false;
        }
        if (errorCode == null) {
            if (other.errorCode != null) {
                return false;
            }
        } else if (!errorCode.equals(other.errorCode)) {
            return false;
        }
        if (errorMessage == null) {
            if (other.errorMessage != null) {
                return false;
            }
        } else if (!errorMessage.equals(other.errorMessage)) {
            return false;
        }
        if (status != other.status) {
            return false;
        }
        return true;
    }

    /**
     * @return the data
     */
    public T getData() {
        return data;
    }

    /**
     * @return the errorCode
     */
    public Integer getErrorCode() {
        return errorCode;
    }

    /**
     * @return the errorMessage
     */
    public String getErrorMessage() {
        return errorMessage;
    }

    /**
     * @return the status
     */
    public int getStatus() {
        return status;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((data == null) ? 0 : data.hashCode());
        result = prime * result + ((errorCode == null) ? 0 : errorCode.hashCode());
        result = prime * result + ((errorMessage == null) ? 0 : errorMessage.hashCode());
        result = prime * result + status;
        return result;
    }

    /**
     * @param data
     *            the data to set
     */
    public void setData(T data) {
        this.data = data;
    }

    /**
     * @param errorCode
     *            the errorCode to set
     */
    public void setErrorCode(Integer errorCode) {
        this.errorCode = errorCode;
    }

    /**
     * @param errorMessage
     *            the errorMessage to set
     */
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    /**
     * @param status
     *            the status to set
     */
    public void setStatus(int status) {
        this.status = status;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return new GsonBuilder().create().toJson(this);
    }

}

 

import com.google.gson.GsonBuilder;

/**
 * @ClassName: KeyValue
 * @author kanpiaoxue
 * @version 1.0
 * @CreateTime: 2019/06/12 10:22:23
 * @Description: KeyValue
 */
public class KeyValue {
    private final K key;
    private final V value;

    /**
     * @param key
     * @param value
     */
    public KeyValue(K key, V value) {
        super();
        this.key = key;
        this.value = value;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        KeyValue other = (KeyValue) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        if (value == null) {
            if (other.value != null) {
                return false;
            }
        } else if (!value.equals(other.value)) {
            return false;
        }
        return true;
    }

    /**
     * @return the key
     */
    public K getKey() {
        return key;
    }

    /**
     * @return the value
     */
    public V getValue() {
        return value;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(this);
    }

}

  

 

 

 

 

 

你可能感兴趣的:(常用java类)