Auto Valueby@jintin
1,573 reads

Auto Value

Read on Terminal Reader

Too Long; Didn't Read


Company Mentioned

Mention Thumbnail
featured image - Auto Value
Jintin HackerNoon profile picture

@jintin

Jintin
react to story with heart

image

Photo by Matthew Szlichta on 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?

  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;

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

@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.doubleToLongBits(this.x) == Double.doubleToLongBits(that.x()))&& (Double.doubleToLongBits(this.y) == Double.doubleToLongBits(that.y()));}return false;}

@Overridepublic 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;

@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 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.

@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

google/auto_auto - A collection of source code generators for Java._github.com

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa