Auto Value

Jintin
Jintin
Oct 29, 2017 · 3 min read

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 {

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 {

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 {

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 {

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

HackerNoon.com

how hackers start their afternoons.

Jintin

Written by

Jintin

Android enthusiast

HackerNoon.com

how hackers start their afternoons.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade