Auto Value

Jintin
HackerNoon.com
Published in
3 min readOct 29, 2017

--

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?

  1. Getter and setter seems boilerplate but we still have to add to comply with the encapsulate rule.
  2. Manually override hasCode() and equals() to distinguish different objects.
  3. Manually override toString() for better logging purpose.

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;

@AutoValue
public 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 @AutoValue annotation, Auto Value auto generate the AutoValue_Point class for us. We access the real class by the abstract interface Point. So what problem we solved?

  1. No getter anymore
  2. No setter which imply immutable in most case
  3. 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 whatAutoValue_Point do for us.

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;
}

@Override
public double x() {
return x;
}

@Override
public double y() {
return y;
}

@Override
public String toString() {
return "Point{"
+ "x=" + x + ", "
+ "y=" + y
+ "}";
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof Point) {
Point that = (Point) o;
return (Double.doubleToLongBits(this.x) == Double.doubleToLongBits(that.x()))
&& (Double.doubleToLongBits(this.y) == Double.doubleToLongBits(that.y()));
}
return false;
}

@Override
public int hashCode() {
int h = 1;
h *= 1000003;
h ^= (Double.doubleToLongBits(this.x) >>> 32) ^ Double.doubleToLongBits(this.x);
h *= 1000003;
h ^= (Double.doubleToLongBits(this.y) >>> 32) ^ Double.doubleToLongBits(this.y);
return h;
}
}

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;

@AutoValue
public abstract class Point {

public abstract double x();

public abstract double y();

public static Builder builder() {
return new AutoValue_Point.Builder();
}

@AutoValue.Builder
public 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 toBuilder 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.

@AutoValue
public 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.Builder
public abstract static class Builder {
public abstract Builder x(double x);

public abstract Builder y(double y);

public abstract Point build();
}
}

Reference

--

--

Jintin
HackerNoon.com

Android/iOS developer, husband and dad. Love to build interesting things to make life easier.