本文记录的是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. Mybatis-Plus自动生成代码的CodeGenerator代码

    官方地址:Mybatis-Plus:https://mp.baomidou.com/guide/generator.html pom中导入mybatis plus的jar包,因为后面会涉及到代码生成, ...

  2. 利用wiile双层循环打印各种星星---day06

    # 十行十列小星星 j = 0 #定义行数 while j<10: #当行数小于10的时候 i=0 #定义列 while i <10: #当列小于10的时候 print('*',end=' ...

  3. 【Azure事件中心】使用Python SDK(Confluent)相关方法获取offset或lag时提示SSL相关错误

    问题描述 使用Python SDK(Confluent)相关方法获取offset或lag时, 提示SSL相关错误, 是否有更清晰的实例以便参考呢? 问题解决 执行代码,因为一直连接不成功,所以检查 c ...

  4. Linux 系统编程从入门到进阶 学习指南

    引言 大家好,我是小康 ,今天我们来学习一下 Linux 系统编程相关的知识.Linux 系统编程是连接高级语言和硬件的桥梁,它对深入理解计算机系统至关重要.无论你是打算构建高性能服务器还是开发嵌入式 ...

  5. XAF Number(编号)

    前言 编号在各类系统中都会存在,同时它还会根据业务场景的不同,会有不同的生成规则.XAF提供了一个编号生成助手(DistributedIdGeneratorHelper),它能在多并发的情况下,生成一 ...

  6. LLM 推理和应用 开源框架梳理

    之前对LLM 推理和应用了解不多,因此抽时间梳理了一下,我们从模型量化,模型推理,以及开发平台等三个层面来梳理分析. 模型量化 模型训练时为了进度,采用的32位浮点数,因此占用的空间较大,一些大的模型 ...

  7. FIR滤波器的设计和实现

    FIR的作用和价值   FIR(Finite Impulse Response)滤波器:有限长单位冲激响应滤波器,又称为非递归型滤波器,是数字信号处理系统中最基本的元件,它可以在保证任意幅频特性的同时 ...

  8. Django使用本地css/js文件的基本流程

     在网上看了很多说Django如何使用本地css/js的文章, 很多都是说的不是很清楚. 今天终于自己来验证一个能用的了, 记录下 在manager.py同层级下创建static文件夹, 里面放上cs ...

  9. Linux 常见缩写命令英文全称

    linux 命令参考Linux 命令大全: https://man.linuxde.net/ awk = "Aho Weiberger and Kernighan" 三个作者的姓的 ...

  10. 全面解析Android之ANR日志

    不论从事安卓应用开发,还是安卓系统研发,应该都遇到应用无响应(简称ANR)问题,当应用程序一段时间无法及时响应,则会弹出ANR对话框,让用户选择继续等待,还是强制关闭.本文将带你全面解析Android ...