Share
Embed
Auto Value and Its Extension GDG LA April
Aaron He
April 29, 2016
Transcript
37 slides
-
Value Types? • “Class without identity: two instances are considered
interchangeable as long as they have equal field values. Like DateTime, Money etc.” -
Effective Java • Item 8: Obey the general contract when
overriding `equals` -
Effective Java • Item 8: Obey the general contract when
overriding `equals` • Item 9: Always override `hashcode` when you override `equals` -
Effective Java • Item 8: Obey the general contract when
overriding `equals` • Item 9: Always override `hashcode` when you override `equals` • Item 10: Always override `toString` -
POJO public final class Vehicle { private final String make;
private final String model; private final String trim; private final BigDecimal msrp; } -
With Auto Value @AutoValue public abstract class Vehicle { public
abstract String getMake(); public abstract String getModel(); public abstract String getTrim(); public abstract BigDecimal getMsrp(); public static Vehicle newInstance(String make, String model, String trim, BigDecimal msrp) { return new AutoValue_Vehicle(make, model, trim, msrp); } } -
final class AutoValue_Vehicle extends Vehicle { private final String
make; private final String model; private final String trim; private final BigDecimal msrp; AutoValue_Vehicle( String make, String model, String trim, BigDecimal msrp) { if (make == null) { throw new NullPointerException("Null make"); } this.make = make; if (model == null) { throw new NullPointerException("Null model"); } this.model = model; if (trim == null) { throw new NullPointerException("Null trim"); } this.trim = trim; if (msrp == null) { throw new NullPointerException("Null msrp"); } this.msrp = msrp; } @Override public String getMake() { return make; } @Override public String getModel() { return model; } @Override public String getTrim() { return trim; } @Override public BigDecimal getMsrp() { return msrp; } @Override public String toString() { return "Vehicle{" + "make=" + make + ", " + "model=" + model + ", " + "trim=" + trim + ", " + "msrp=" + msrp + "}"; } @Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof Vehicle) { Vehicle that = (Vehicle) o; return (this.make.equals(that.getMake())) && (this.model.equals(that.getModel())) && (this.trim.equals(that.getTrim())) && (this.msrp.equals(that.getMsrp())); } return false; } @Override public int hashCode() { int h = 1; h *= 1000003; h ^= this.make.hashCode(); h *= 1000003; h ^= this.model.hashCode(); h *= 1000003; h ^= this.trim.hashCode(); h *= 1000003; h ^= this.msrp.hashCode(); return h; } } -
final class AutoValue_Vehicle extends Vehicle { private final String
make; private final String model; private final String trim; private final BigDecimal msrp; AutoValue_Vehicle( String make, String model, String trim, BigDecimal msrp) { if (make == null) { throw new NullPointerException("Null make"); } this.make = make; if (model == null) { throw new NullPointerException("Null model"); } this.model = model; if (trim == null) { throw new NullPointerException("Null trim"); } this.trim = trim; if (msrp == null) { throw new NullPointerException("Null msrp"); } this.msrp = msrp; } -
@Override public String getMake() { return make; } @Override
public String getModel() { return model; } @Override public String getTrim() { return trim; } @Override public BigDecimal getMsrp() { return msrp; } -
@Override public String toString() { return "Vehicle{" + "make="
+ make + ", " + "model=" + model + ", " + "trim=" + trim + ", " + "msrp=" + msrp + "}"; } @Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof Vehicle) { Vehicle that = (Vehicle) o; return (this.make.equals(that.getMake())) && (this.model.equals(that.getModel())) && (this.trim.equals(that.getTrim())) && (this.msrp.equals(that.getMsrp())); } return false; } @Override public int hashCode() { int h = 1; h *= 1000003; h ^= this.make.hashCode(); h *= 1000003; h ^= this.model.hashCode(); h *= 1000003; h ^= this.trim.hashCode(); h *= 1000003; h ^= this.msrp.hashCode(); return h; } -
Extensions! • What if we need Parcelable/Gson/Moshi etc.? • Parcelable:
https://github.com/rharter/auto-value- parcel -
Extensions! • What if we need Parcelable/Gson/Moshi etc.? • Parcelable:
https://github.com/rharter/auto-value- parcel • Gson: https://github.com/rharter/auto-value-gson -
Extensions! • What if we need Parcelable/Gson/Moshi etc.? • Parcelable:
https://github.com/rharter/auto-value- parcel • Gson: https://github.com/rharter/auto-value-gson • Moshi: https://github.com/rharter/auto-value-moshi -
Extensions! • What if we need Parcelable/Gson/Moshi etc.? • Parcelable:
https://github.com/rharter/auto-value- parcel • Gson: https://github.com/rharter/auto-value-gson • Moshi: https://github.com/rharter/auto-value-moshi • Redacted: https://github.com/square/auto-value- redacted -
Set it up • If it’s a Maven, or Gradle
(2.12+) project, just use `provided` or `compileOnly` -
Set it up • If it’s a Maven, or Gradle
(2.12+) project, just use `provided` or `compileOnly` • For Android, use `apt` plugin: https://bitbucket.org/ hvisser/android-apt -
Set it up • If it’s a Maven, or Gradle
(2.12+) project, just use `provided` or `compileOnly` • For Android, use `apt` plugin: https://bitbucket.org/ hvisser/android-apt apt ‘com.google.auto.value:auto-value:1.2’ -
Set it up • If it’s a Maven, or Gradle
(2.12+) project, just use `provided` or `compileOnly` • For Android, use `apt` plugin: https://bitbucket.org/ hvisser/android-apt apt ‘com.google.auto.value:auto-value:1.2’ provided ‘com.google.auto.value:auto-value:1.2’ -
Set it up • If it’s a Maven, or Gradle
(2.12+) project, just use `provided` or `compileOnly` • For Android, use `apt` plugin: https://bitbucket.org/ hvisser/android-apt apt ‘com.google.auto.value:auto-value:1.2’ provided ‘com.google.auto.value:auto-value:1.2’ Or provided 'com.jakewharton.auto.value:auto-value-annotations:1.2' -
Set it up • If it’s a Maven, or Gradle
(2.12+) project, just use `provided` or `compileOnly` • For Android, use `apt` plugin: https://bitbucket.org/ hvisser/android-apt apt ‘com.google.auto.value:auto-value:1.2’ provided ‘com.google.auto.value:auto-value:1.2’ Or provided 'com.jakewharton.auto.value:auto-value-annotations:1.2' -
References • AutoValue: https://github.com/google/auto/tree/master/value • AutoValue Intro by Googlers: https://docs.google.com/
presentation/d/14u_h- lMn7f1rXE1nDiLX0azS3IkgjGl5uxp5jGJ75RE/ -
References • AutoValue: https://github.com/google/auto/tree/master/value • AutoValue Intro by Googlers: https://docs.google.com/
presentation/d/14u_h- lMn7f1rXE1nDiLX0azS3IkgjGl5uxp5jGJ75RE/ • AutoValue Extensions - Jake Wharton: https:// www.youtube.com/watch?v=FfBBTHkRC-o -
References • AutoValue: https://github.com/google/auto/tree/master/value • AutoValue Intro by Googlers: https://docs.google.com/
presentation/d/14u_h- lMn7f1rXE1nDiLX0azS3IkgjGl5uxp5jGJ75RE/ • AutoValue Extensions - Jake Wharton: https:// www.youtube.com/watch?v=FfBBTHkRC-o • An introduction to AutoValue: http://ryanharter.com/blog/ 2016/03/22/autovalue/ -
References • AutoValue: https://github.com/google/auto/tree/master/value • AutoValue Intro by Googlers: https://docs.google.com/
presentation/d/14u_h- lMn7f1rXE1nDiLX0azS3IkgjGl5uxp5jGJ75RE/ • AutoValue Extensions - Jake Wharton: https:// www.youtube.com/watch?v=FfBBTHkRC-o • An introduction to AutoValue: http://ryanharter.com/blog/ 2016/03/22/autovalue/ • A deeper look at AutoValue: http://ryanharter.com/blog/ 2016/04/08/autovalue-deep-dive/