GithubHelp home page GithubHelp logo

kkiro-coder / equator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dadiyang/equator

0.0 0.0 0.0 123 KB

一个用于比较两个对象的所有属性是否全部相等,并且可以获取所有不相等的属性的比对器

License: Apache License 2.0

Java 100.00%

equator's Introduction

属性比对器

一个用于比较两个对象的属性是否相等,并且可以获取所有不相等的属性的比对器

原理

使用反射得到两个对象的属性(field或者getter方法),比对该属性的值

注意事项

  1. 如果有一个对象为空,则认为该对象的所有属性都为空
  2. 基于getter方法的对比器,会忽略 getClass() 方法
  3. 属性比对默认使用 Objects.deepEquals 方法进行比对,如果是集合类,会转换为数组进行对比
  4. 支持比对两个不同类型的对象,默认只比对两个类字段的交集,即两个类都有的字段才比对,可以设置 bothExistFieldOnly 配置,字段不存在和 null 视为相等

使用

加入依赖

加入 maven 依赖

<dependency>
    <groupId>com.github.dadiyang</groupId>
    <artifactId>equator</artifactId>
    <version>1.0.4</version>
</dependency>

或者下载 equator-1.0.4.jar ,并添加到 classpath 下。

调用方法

这里提供了GetterBaseEquator和FieldBaseEquator两个实现类,分别对应基于getter方法和基于属性的比对器,可以根据自己的需要进行选择

Equator equator = new GetterBaseEquator();
// 支持比对两个不同类型的对象,默认只比对两个类字段的交集,即两个类都有的字段才比对
User user1 = new User(...);
UserDTO user2 = new UserDTO(...);

// 判断属性是否完全相等
equator.isEquals(user1, user2);

// 获取不同的属性
List<FieldInfo> diff = equator.getDiffFields(user1, user2);

扩展

AbstractEquator中定义了 isFieldEquals 方法,如果你需要对某些特殊属性进行特殊的比对,则可以覆盖此方法

结构图

结构图

equator's People

Contributors

dadiyang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.