关于实现一个自定义类的equals方法和hashCode() 方法

今天看公司代码的时候,看到一个自己之前遗忘的知识点。
1. java里面虽然不提供go-to语句,但是使用label: 配合break可以实现这个功能。break主要用于控制跳出指定label代码块。
2. 可以直接使用Date自带方法来进行时间比较,状态实时计算出来。
3. @JsonFormat 格式化 json返回格式。
下面是这个前辈的代码:


import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable;
import java.util.Date;

public class ActivityEntity implements Serializable {
    private static final long serialVersionUID = 549127479215301040L;
    private String aid;
    private String title;
    private Integer actType;
    private Integer userDayPlayCount;
    private Integer userTotalPlayCount;
    private Integer userDayAwardCount;
    private Integer userTotalAwardCount;
    private String verificationPass;
    @JsonFormat(
        pattern = "yyyy-MM-dd HH:mm:ss",
        timezone = "GMT+8"
    )
    private Date startTime;
    @JsonFormat(
        pattern = "yyyy-MM-dd HH:mm:ss",
        timezone = "GMT+8"
    )
    private Date endTime;
    @JsonFormat(
        pattern = "yyyy-MM-dd HH:mm:ss",
        timezone = "GMT+8"
    )
    private Date createTime;
    private String remark;

    public Integer getActivityStatus() {
        Date now = new Date();
        // 0 活动未开始  1 活动已开始 2 活动已经结束
        return this.startTime.after(now)?Integer.valueOf(0):(this.endTime.before(now)?Integer.valueOf(2):Integer.valueOf(1));
    }

    public boolean isInProgress() {
        return 1 == this.getActivityStatus().intValue();
    }

    public boolean isEnd() {
        return 2 == this.getActivityStatus().intValue();
    }

    public boolean isNotStart() {
        return 0 == this.getActivityStatus().intValue();
    }

    public ActivityEntity() {
    }

    public String getAid() {
        return this.aid;
    }

    public String getTitle() {
        return this.title;
    }

    public Integer getActType() {
        return this.actType;
    }

    public Integer getUserDayPlayCount() {
        return this.userDayPlayCount;
    }

    public Integer getUserTotalPlayCount() {
        return this.userTotalPlayCount;
    }

    public Integer getUserDayAwardCount() {
        return this.userDayAwardCount;
    }

    public Integer getUserTotalAwardCount() {
        return this.userTotalAwardCount;
    }

    public String getVerificationPass() {
        return this.verificationPass;
    }

    public Date getStartTime() {
        return this.startTime;
    }

    public Date getEndTime() {
        return this.endTime;
    }

    public Date getCreateTime() {
        return this.createTime;
    }

    public String getRemark() {
        return this.remark;
    }

    public void setAid(String aid) {
        this.aid = aid;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setActType(Integer actType) {
        this.actType = actType;
    }

    public void setUserDayPlayCount(Integer userDayPlayCount) {
        this.userDayPlayCount = userDayPlayCount;
    }

    public void setUserTotalPlayCount(Integer userTotalPlayCount) {
        this.userTotalPlayCount = userTotalPlayCount;
    }

    public void setUserDayAwardCount(Integer userDayAwardCount) {
        this.userDayAwardCount = userDayAwardCount;
    }

    public void setUserTotalAwardCount(Integer userTotalAwardCount) {
        this.userTotalAwardCount = userTotalAwardCount;
    }

    public void setVerificationPass(String verificationPass) {
        this.verificationPass = verificationPass;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof ActivityEntity)) {
            return false;
        } else {
            ActivityEntity other = (ActivityEntity)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                label155: {
                    Object this$aid = this.getAid();
                    Object other$aid = other.getAid();
                    if(this$aid == null) {
                        if(other$aid == null) {
                            break label155;
                        }
                    } else if(this$aid.equals(other$aid)) {
                        break label155;
                    }

                    return false;
                }

                Object this$title = this.getTitle();
                Object other$title = other.getTitle();
                if(this$title == null) {
                    if(other$title != null) {
                        return false;
                    }
                } else if(!this$title.equals(other$title)) {
                    return false;
                }

                Object this$actType = this.getActType();
                Object other$actType = other.getActType();
                if(this$actType == null) {
                    if(other$actType != null) {
                        return false;
                    }
                } else if(!this$actType.equals(other$actType)) {
                    return false;
                }

                label134: {
                    Object this$userDayPlayCount = this.getUserDayPlayCount();
                    Object other$userDayPlayCount = other.getUserDayPlayCount();
                    if(this$userDayPlayCount == null) {
                        if(other$userDayPlayCount == null) {
                            break label134;
                        }
                    } else if(this$userDayPlayCount.equals(other$userDayPlayCount)) {
                        break label134;
                    }

                    return false;
                }

                label127: {
                    Object this$userTotalPlayCount = this.getUserTotalPlayCount();
                    Object other$userTotalPlayCount = other.getUserTotalPlayCount();
                    if(this$userTotalPlayCount == null) {
                        if(other$userTotalPlayCount == null) {
                            break label127;
                        }
                    } else if(this$userTotalPlayCount.equals(other$userTotalPlayCount)) {
                        break label127;
                    }

                    return false;
                }

                label120: {
                    Object this$userDayAwardCount = this.getUserDayAwardCount();
                    Object other$userDayAwardCount = other.getUserDayAwardCount();
                    if(this$userDayAwardCount == null) {
                        if(other$userDayAwardCount == null) {
                            break label120;
                        }
                    } else if(this$userDayAwardCount.equals(other$userDayAwardCount)) {
                        break label120;
                    }

                    return false;
                }

                Object this$userTotalAwardCount = this.getUserTotalAwardCount();
                Object other$userTotalAwardCount = other.getUserTotalAwardCount();
                if(this$userTotalAwardCount == null) {
                    if(other$userTotalAwardCount != null) {
                        return false;
                    }
                } else if(!this$userTotalAwardCount.equals(other$userTotalAwardCount)) {
                    return false;
                }

                label106: {
                    Object this$verificationPass = this.getVerificationPass();
                    Object other$verificationPass = other.getVerificationPass();
                    if(this$verificationPass == null) {
                        if(other$verificationPass == null) {
                            break label106;
                        }
                    } else if(this$verificationPass.equals(other$verificationPass)) {
                        break label106;
                    }

                    return false;
                }

                Object this$startTime = this.getStartTime();
                Object other$startTime = other.getStartTime();
                if(this$startTime == null) {
                    if(other$startTime != null) {
                        return false;
                    }
                } else if(!this$startTime.equals(other$startTime)) {
                    return false;
                }

                label92: {
                    Object this$endTime = this.getEndTime();
                    Object other$endTime = other.getEndTime();
                    if(this$endTime == null) {
                        if(other$endTime == null) {
                            break label92;
                        }
                    } else if(this$endTime.equals(other$endTime)) {
                        break label92;
                    }

                    return false;
                }

                Object this$createTime = this.getCreateTime();
                Object other$createTime = other.getCreateTime();
                if(this$createTime == null) {
                    if(other$createTime != null) {
                        return false;
                    }
                } else if(!this$createTime.equals(other$createTime)) {
                    return false;
                }

                Object this$remark = this.getRemark();
                Object other$remark = other.getRemark();
                if(this$remark == null) {
                    if(other$remark != null) {
                        return false;
                    }
                } else if(!this$remark.equals(other$remark)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof ActivityEntity;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $aid = this.getAid();
        int result = result * 59 + ($aid == null?0:$aid.hashCode());
        Object $title = this.getTitle();
        result = result * 59 + ($title == null?0:$title.hashCode());
        Object $actType = this.getActType();
        result = result * 59 + ($actType == null?0:$actType.hashCode());
        Object $userDayPlayCount = this.getUserDayPlayCount();
        result = result * 59 + ($userDayPlayCount == null?0:$userDayPlayCount.hashCode());
        Object $userTotalPlayCount = this.getUserTotalPlayCount();
        result = result * 59 + ($userTotalPlayCount == null?0:$userTotalPlayCount.hashCode());
        Object $userDayAwardCount = this.getUserDayAwardCount();
        result = result * 59 + ($userDayAwardCount == null?0:$userDayAwardCount.hashCode());
        Object $userTotalAwardCount = this.getUserTotalAwardCount();
        result = result * 59 + ($userTotalAwardCount == null?0:$userTotalAwardCount.hashCode());
        Object $verificationPass = this.getVerificationPass();
        result = result * 59 + ($verificationPass == null?0:$verificationPass.hashCode());
        Object $startTime = this.getStartTime();
        result = result * 59 + ($startTime == null?0:$startTime.hashCode());
        Object $endTime = this.getEndTime();
        result = result * 59 + ($endTime == null?0:$endTime.hashCode());
        Object $createTime = this.getCreateTime();
        result = result * 59 + ($createTime == null?0:$createTime.hashCode());
        Object $remark = this.getRemark();
        result = result * 59 + ($remark == null?0:$remark.hashCode());
        return result;
    }

    public String toString() {
        return "ActivityEntity(aid=" + this.getAid() + ", title=" + this.getTitle() + ", actType=" + this.getActType() + ", userDayPlayCount=" + this.getUserDayPlayCount() + ", userTotalPlayCount=" + this.getUserTotalPlayCount() + ", userDayAwardCount=" + this.getUserDayAwardCount() + ", userTotalAwardCount=" + this.getUserTotalAwardCount() + ", verificationPass=" + this.getVerificationPass() + ", startTime=" + this.getStartTime() + ", endTime=" + this.getEndTime() + ", createTime=" + this.getCreateTime() + ", remark=" + this.getRemark() + ")";
    }
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,132评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,802评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,566评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,858评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,867评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,695评论 1 282
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,064评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,705评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,915评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,677评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,796评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,432评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,041评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,992评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,223评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,185评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,535评论 2 343

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,598评论 18 139
  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,601评论 1 180
  • 无论你们是朋友还是情人,无论你们一起多长时间了,只要一起策划去旅行,就完全暴露了自己隐藏的性格。 从选择旅行地开始...
    whenwho阅读 185评论 0 0
  • 【别贪嘴】新年第一天,当需改变!删除了新浪腾讯:兢兢业业为你服务,到头来给我一个屁,两好“咖”一好,世间最简单的道...
    茧破阅读 211评论 0 4
  • 170818 生活中总是会发生和人起冲突的时候,有时明明是小事,结果话赶话会让矛盾升级,进而激化。怎...
    闲看花开花落_阿文阅读 208评论 0 0