Photo by on Matthew Szlichta Unsplash Auto Value is a library to help you easily construct value class. It automatically generate code at build time to reduce lots of boilerplate in your source code. The Old Days Let’s start from a simple POJO to see what we can improve. public class Point { private double x; private double y; public double getX() { return x; } public double getY() { return y; } public void setX(double x) { x = x; } public void setY(double y) { y = y; } @Override public int hashCode() {...} @Override public boolean equals(Object other) {...} @Override public String toString() {...} } What the matter of this simple code? Getter and setter seems boilerplate but we still have to add to comply with the encapsulate rule. Manually override and to distinguish different objects. hasCode() equals() Manually override for better logging purpose. toString() AutoValue is here to rescue. Get Start How Auto Value can help us? Here is the sample code after we use AutoValue. import com.google.auto.value.AutoValue; @AutoValuepublic abstract class Point { public abstract double x(); public abstract double y(); public static Point create(double x, double y) {return new AutoValue_Point(x, y);}} By add the annotation, Auto Value auto generate the class for us. We access the real class by the abstract interface . So what problem we solved? @AutoValue AutoValue_Point Point No getter anymore No setter which imply immutable in most case Hide the detail of all basic function (equals, hashCode) in the generate class p.s. One thing to notice is that we can still modify the value if it’s Array type like _List_ . What the generate class do? We can also check what do for us. AutoValue_Point final class AutoValue_Point extends Point { private final double x;private final double y; AutoValue_Point(double x,double y) {this.x = x;this.y = y;} @Overridepublic double x() {return x;} @Overridepublic double y() {return y;} @Overridepublic String toString() {return "Point{"+ "x=" + x + ", "+ "y=" + y+ "}";} @Overridepublic boolean equals(Object o) {if (o == this) {return true;}if (o instanceof Point) {Point that = (Point) o;return (Double. (this.x) == Double. (that.x()))&& (Double. (this.y) == Double. (that.y()));}return false;} doubleToLongBits doubleToLongBits doubleToLongBits doubleToLongBits @Overridepublic int hashCode() {int h = 1;h *= 1000003;h ^= (Double. (this.x) >>> 32) ^ Double. (this.x);h *= 1000003;h ^= (Double. (this.y) >>> 32) ^ Double. (this.y);return h;}} doubleToLongBits doubleToLongBits doubleToLongBits doubleToLongBits Builder Pattern What if the parameters is too many so it’s hard to read? AutoValue also support builder mode to instantiate object. import com.google.auto.value.AutoValue; @AutoValuepublic abstract class Point { public abstract double x(); public abstract double y(); public static Builder builder() {return new AutoValue_Point.Builder();} @AutoValue.Builderpublic abstract static class Builder { **public abstract Builder x(double x); public abstract Builder y(double y); public abstract Point build(); }**} The usage is simple as normal builder. Point p = Point.builder().x(100).y(100).build(); Update Value AutoValue object is immutable by its design, but the builder pattern also benefit us to clone object when we have to. Just add an abstract to indicate we have the functionality to create builder by existing value. Than we can transform the builder back to object after we update value or at anytime. toBuilder @AutoValuepublic abstract class Point { public abstract double x(); public abstract double y(); public static Builder builder() {return new AutoValue_Point.Builder();} public abstract Builder toBuilder(); public Point withX(double x) {return toBuilder().x(x).build();} @AutoValue.Builderpublic abstract static class Builder {public abstract Builder x(double x); public abstract Builder y(double y); public abstract Point build(); }} Reference _auto - A collection of source code generators for Java._github.com google/auto