Code Monkey home page Code Monkey logo

deepequals's Introduction

deepequals

Deep Java object comparison (without the need to implement equals)

  • compares two objects by recursively comparing their properties (public no-arg methods with non-void return type. The method name does not matter). It (currently) ignores equals methods except for some types: primitives and corresponding wrappers, String, LocalDate and similar, enums, and Object.

  • since it compares objects by calling methods, it does not require the objects to be of the same runtime type and thus the user must indicate the top type used for comparison:

    Foo x = ...;
    Foo y = ...;
    assertTrue(deepEquals(Foo.class, x, y));
  • Generics are fully supported by leveraging Guava's TypeToken:

    assertTrue(deepEquals(
            new TypeToken<Supplier<String>>() {},
            () -> "hello",
            new Supplier<String>() {
              @Override public String get() { return "hello"; }
            }));
  • it also natively supports (including all subinterfaces and -classes):

    • Optional: deep compares the contained objects recursively when appropriate.
    • Map: maps must contain the same keys. Values are deep compared.
    • Set: must contain the same elements as indicated by equals and hashCode. No deep comparison takes place.
    • arrays and Iterable: deep compares the elements. Choice of strict or lenient ordering.
  • supports overriding the way types/properties are compared.

  • by default is expects all types involved to be data-carrying objects exclusively i.e. all methods on the objects in the tree both take no args and return something. This restriction can be relaxed, though:

    class Foo {
        public void thisMethodIsNotAProperty(/* whatever */) { ... }
        public Bar thisOneIsntEither(Baz x /*, whatever */) { ... }
        public Qux thisIsAProperty() { ... }
    }
    assertTrue(withOptions()
            .typeLenient()
            .deepEquals(Foo.class, x, y));
  • other goodies ... Check out the tests for more details.

deepequals's People

Contributors

beluchin avatar grossvater avatar tomahi 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.