Why You Should Use Auto Value in Java? | Mincong's Blog
Auto Value is a Java library which helps you to generate value types correctly. A value type is class without identity: two instances are considered interchangeable as long as they have equal field values. Examples: DateTime
, Money
, Uri
… but you also tend to create a great many of these yourself. You know the kind: they're the ones where you have to implement equals()
, hashCode()
, and usually toString()
.
Before Auto Value
Before Auto Value, creating a value type is not easy. In order to implement it correctly, you need to declare all the fields manually, mark them as private
, and only expose the getters; you need to implement hashCode()
and equals()
(often handled by IDE), and keep them up to date when attributes changed; you also need to mark the class as final
to prevent subclassing, which guarantees the equality.
Read full article from Why You Should Use Auto Value in Java? | Mincong's Blog
No comments:
Post a Comment