Giter Club home page Giter Club logo

kotlin-equals-ignoring-fields's Introduction

kotlin-equals-ignoring-fields

Compares two data cases classes excluding specific field names.

Example

@GenEqualsIgnore(
    [EqualsIgnore(["id", "count"]),
    (EqualsIgnore(["id"], false))]
)
data class Measurement(val id: Long, val date: Long, val count: Int, val test: ByteArray = ByteArray(1))

...
    val m1 = Measurement(1, 1131, 3)
    val m2 = Measurement(2, 1131, 2)
    println(m1 == m2) // false
    println(m1.equalsIgnore_id(m2)) // false
    println(m1.equalsIgnore_id_count(m2)) // true

Usage

Put the @GenEqualsIgnore annotation on the data classes where equals methods excluding certain fields are desired. Each EqualsIgnore inside GenEqualsIgnore will generate one method. The first argument of EqualsIgnore is the array of field names that should be excluded from the equality comparison. The second argument of EqualsIgnore indicates whether the arrays should be compared using the structural equality (Arrays.equals) rather than the referential equality. The second argument is optional (the default value is true). The third argument of EqualsIgnore is an optional method name: if it's not set, the corresponding method names are auto-generated as shown above (equalsIgnore_<fields>).

build.gradle example:

buildscript {
    ext.kotlin_version = '1.2.41'
    ext.ignore_equals_version = '0.3'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}


apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'

kapt {
    generateStubs = true
}

repositories {
    mavenCentral()
    maven {
        url  "https://dl.bintray.com/tomastauber/equals-ignoring-fields"
    }
}

sourceSets {
    main {
        java {
            srcDir "${buildDir.absolutePath}/tmp/kapt/main/kotlinGenerated/"
        }
    }
}

dependencies {
    kapt 'io.cryptoblk:equals-ignoring-fields:$ignore_equals_version'
    compileOnly 'io.cryptoblk:equals-ignoring-fields:$ignore_equals_version'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}


kotlin-equals-ignoring-fields's People

Contributors

tomtau avatar

Stargazers

 avatar

Watchers

 avatar  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.