@Canonical annotation

27 / Sep / 2012 by Mohit Garg 2 comments

@Canonical: It’s very useful annotation. It provides the combination of features of @ToString (default implementation of toString() method based upon the fields in the class), @EqualsAndHashCode(default implementation of equals() and hashCode() method of the class based upon the fields in the class) and @TupleConstructor (provides the classical constructor with default properties).

import groovy.transform.Canonical

@Canonical
class Person {
String name
String address
String city

}

Person person = new Person(name:"Mohit Garg",address:"Address",city: "city")
Person person1 = new Person("Mohit Garg","Address","city")

println("Check two objects are equal:::"+(person.equals(person1)))  // Both objects will be equal because of same hashCode. (Output:true)

println("person object:::"+person) //Output:Person(Mohit Garg, Address, city)

Hope this will help you :)

FOUND THIS USEFUL? SHARE IT

comments (2)

  1. Deepak Mittal

    Hi Jason,

    I regret our failure in maintaining the quality of our posts, which has made us earn a lot of loyal audience. We were trying to promote the habit of blogging amongst our peers through an internal initiative, but never realized the consequences of merely improving the numbers.

    It has been an eye opener for us. We are really sorry for letting you down by posting sub standard content and assure you that such instances would not be repeated.

    I appreciate it that you brought it to our notice and made us realize the gravity of the situation. Sorry once again and we thank you for the continued support of our blog.

    Reply
  2. Jason John

    I was a proud subscriber of intelligrape’s Blogs, but now it seems that you people are posting documentation of twitter/linkedin/spring, i have a huge spam on these topics in my feed reader, thanks to you guys.
    Is it a marketing or SEO initiative? because if it is, i must tell we readers don’t need documentation, we need application of concept.

    Reply

Leave a comment -