本文记录的是2016年4月初发生的事情。


前几天,标准CI的静态检查页面发现一个项目组同事引入的FindBugs问题,EQ_COMPARETO_USE_OBJECT_EQUALS,CI对这个问题给出的介绍如下

Class defines compareTo(...) and uses Object.equals()

同事没见过这个问题,不了解如何修改,于是在中午回基地吃饭的路上一起讨论这个问题。

其实这个问题并不复杂,也不困难。FindBugs工具是在抱怨一个Java Bean类实现了Comparable接口,但却使用了父类(java.lang.Object类)的equals方法,没有覆盖父类的实现,违反了接口的使用规则。

因此消除EQ_COMPARETO_USE_OBJECT_EQUALS的方法:

  • 在这个Java Bean类中覆盖父类的equals方法;

  • 对于Java Bean类的两个对象x和y,equals方法的实现保证如下等式成立

      x.compareTo(y) == 0时,x.equals(y)为true

修改方法很简单,本项目使用了Lombok,因此为Java Bean类实现equals方法并不需要写代码。同时Lombok还很贴心的生成了hashCode方法,这又避免了另外一个FindBugs问题,即实现equals方法时,需要同步实现hashCode方法。

问题按时解决了,成功避免项目组被部门通报、晾晒。那么接下来需要复习一下功课,避免后续掉到相同的坑里。如下是相关的资料,作为对上述修改方法的补充说明,蛮好理解的,所以就不费神转换了。

EQ_COMPARETO_USE_OBJECT_EQUALS的官方定义

如下截取自FindBugs官方文档

This class defines a compareTo(...) method but inherits its equals() method from java.lang.Object.

Generally, the value of compareTo should return zero if and only if equals returns true.

If this is violated, weird and unpredictable failures will occur in classes such as PriorityQueue.

In Java 5 the PriorityQueue.remove method uses the compareTo method, while in Java 6 it uses the equals method.

From the JavaDoc for the compareTo method in the Comparable interface:

It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)).

Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact.

The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."

Comparable的官方文档

如下截取自Java官方文档

public interface Comparable

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.

Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.

The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.

It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method.

For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective.

Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals. One exception is java.math.BigDecimal, whose natural ordering equates BigDecimal objects with equal values and different precisions (such as 4.0 and 4.00).

For the mathematically inclined, the relation that defines the natural ordering on a given class C is:

{(x, y) such that x.compareTo(y) <= 0}.

The quotient for this total order is:

{(x, y) such that x.compareTo(y) == 0}.

It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering is consistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method:

{(x, y) such that x.equals(y)}.

This interface is a member of the Java Collections Framework.

高等代数 or 离散数学

看过上面截取的材料,突然发现接口ComparablecompareTo方法满足如下要求:

  1. 自反,即x.compareTo(x) == 0
  2. 对称,即x.compareTo(y) == 0,同时y.compareTo(x) == 0
  3. 传递,如果x.compareTo(y) == 0并且y.compareTo(z) == 0,那么一定有x.compareTo(z) == 0

不由得赞叹老外写文档时的确是相当的用心,值得学习。另外更加觉得当年读数学系的信息与计算科学专业是正确的选择。

资料

FindBugs问题EQ_COMPARETO_USE_OBJECT_EQUALS的解决方法的更多相关文章

  1. JAVA各种OOM代码样例及解决方法

    周末了,觉得我还有很多作业没有写,针对目前大家对OOM的类型不太熟悉,那么我们来总结一下各种OOM出现的情况以及解决方法. 我们把各种OOM的情况列出来,然后逐一进行代码编写复现和提供解决方法. 1. ...

  2. IE6、7下html标签间存在空白符,导致渲染后占用多余空白位置的原因及解决方法

    直接上图:原因:该div包含的内容是靠后台进行print操作,输出的.如果没有输出任何内容,浏览器会默认给该空白区域添加空白符.在IE6.7下,浏览器解析渲染时,会认为空白符也是占位置的,默认其具有字 ...

  3. MVVM框架从WPF移植到UWP遇到的问题和解决方法

    MVVM框架从WPF移植到UWP遇到的问题和解决方法 0x00 起因 这几天开始学习UWP了,之前有WPF经验,所以总体感觉还可以,看了一些基础概念和主题,写了几个测试程序,突然想起来了前一段时间在W ...

  4. iPhone Anywehre虚拟定位提示“后台服务未启动,请重新安装应用后使用”的解决方法

    问题描述: iPhone越狱了,之后在Cydia中安装Anywhere虚拟定位,但是打开app提示:后台服务未启动,请重新安装应用后使用. 程序无法正常使用... 解决方法: 打开Cydia-已安装, ...

  5. python中IndentationError: expected an indented block错误的解决方法

    IndentationError: expected an indented block 翻译为IndentationError:预期的缩进块 解决方法:有冒号的下一行要缩进,该缩进就缩进

  6. js闭包for循环总是只执行最后一个值得解决方法

    <style> li{ list-style: none;width:40px;height: 40px;text-align:center;line-height: 40px;curso ...

  7. mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法!

    mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法! 修改mysql5.7的配置文件即可解决,方法如下: linux版:找到mysql的安装路径进入默认的为/usr/shar ...

  8. maven常见异常以及解决方法

    本文写的是我在整合ssm框架时遇到的一些问题,我估计很多人也会遇到,所以,这里作为一个总结,希望能够帮助大家解决问题 一,加入shiro组件时抛出的异常 加入步骤(略) 问题 1,保存后,无法导入sh ...

  9. WebEssentials 在vs2013 update5安装报错的解决方法.

    WebEssentials 最高支持到update4 如果更新到了update5 RC, 则无法直接安装. 解决方法是 1,下载WebEssentials2013.vsix 文件. 2, 安装7zip ...

  10. [异常解决] windows用SSH和linux同步文件&linux开启SSH&ssh client 报 algorithm negotiation failed的解决方法之一

    1.安装.配置与启动 SSH分客户端openssh-client和openssh-server 如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有 ...

随机推荐

  1. Qt实用技巧:QCustomPlot做北斗GPS显示绝对位置运动轨迹和相对位置运动轨迹图的时,使图按照输入点顺序连曲线

    需求   使用QCustomPlot绘制多个目标的北斗运行轨迹图,包括累计绝对位置图和记录时刻的相对位置图.  当前绘制存在问题:    交付客户前,公司内部自测流程发现的问题.  实际预期效果为:  ...

  2. 正则函数及面向对象开发初识---day19

    1.正则函数 # ### 正则函数 import re #search 通过正则匹配出第一个对象返回,通过group取出对象中的值 strvar = "1+2 3*4" obj = ...

  3. 尝试通过uniapp仿微信页面

    最近一直想弄一个app,然后刚好看到Uniapp这个技术,然后最近就用几个晚上琢磨了下: 先看下成果: 1.通讯页面,这个是通过插件uni-indexed-list 索引列表 进行改造过后:改造过程还 ...

  4. 机器学习策略篇:详解单一数字评估指标(Single number evaluation metric)

    单一数字评估指标 无论是调整超参数,或者是尝试不同的学习算法,或者在搭建机器学习系统时尝试不同手段,会发现,如果有一个单实数评估指标,进展会快得多,它可以快速告诉,新尝试的手段比之前的手段好还是差.所 ...

  5. 安装MySql失败( Microsoft Visual C++ 2013 Runtime 64bit)

    参考资料:下载之家 提示你缺少什么版本就安装什么版本.64位或者32位. 文件下载地址:下载之家 不知道有没有失效,如果失效的话大家直接去下载之家搜索下载.

  6. Elasticsearch-Mapping(映射)

    Elasticsearch-Mapping(映射) Mapping是用来定义一个文档(document),以及它所包含的属性(field)是如何存储和 索引的. 哪些字符串属性应该被看做全文本属性(f ...

  7. 如何在矩池云上安装语音识别模型 Whisper

    如何在矩池云上安装语音识别模型 Whisper Whisper 是 OpenAI 近期开源的一个语音识别的模型,研究人员基于 680,000 小时的标记音频数据进行训练,它同时也是一个多任务模型,可以 ...

  8. 【Azure Redis 缓存】Azure Cache for Redis 如何迁移

    Azure Cache for Redis 如何迁移 [Azure Redis 缓存]Azure Cache for Redis有默认备份可以用于恢复么?一文中,介绍了使用RDB文件的方式来迁移Red ...

  9. 【Azure 应用服务】App Service For Linux 怎么安装Composer,怎么安装PHP扩展,怎么来修改站点根路径启动程序?

    问题一:App Service 的默认启动路径为wwwroot,如何修改到到PHP代码运行目录呢? 如Laravel的启动目录为public/?那如何修改呢? App Service 的默认 PHP ...

  10. 使用秘籍|如何实现图数据库 NebulaGraph 的高效建模、快速导入、性能优化

    本文整理自 NebulaGraph PD 方扬在「NebulaGraph x KubeBlocks」meetup 上的演讲,主要包括以下内容: NebulaGraph 3.x 发展历程 NebulaG ...