Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Auto Value and Its Extension GDG LA April

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Auto Value and Its Extension GDG LA April

Aaron He

April 29, 2016

Transcript

37 slides
  1. Value Types? • “Class without identity: two instances are considered

    interchangeable as long as they have equal field values. Like DateTime, Money etc.”
  2. Effective Java • Item 8: Obey the general contract when

    overriding `equals` • Item 9: Always override `hashcode` when you override `equals`
  3. 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`
  4. POJO public final class Vehicle {
 private final String make;


    private final String model;
 private final String trim;
 private final BigDecimal msrp;
 }
  5. 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);
 }
 }
  6. 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;
 }
 }
  7. 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;
 }

  8. @Override
 public String getMake() {
 return make;
 }
 
 @Override


    public String getModel() {
 return model;
 }
 
 @Override
 public String getTrim() {
 return trim;
 }
 
 @Override
 public BigDecimal getMsrp() {
 return msrp;
 }
  9. 
 @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;
 }

  10. 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
  11. 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
  12. 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
  13. Set it up • If it’s a Maven, or Gradle

    (2.12+) project, just use `provided` or `compileOnly`
  14. 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
  15. 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’
  16. 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’
  17. 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'
  18. 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'
  19. 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
  20. 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/
  21. 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/

More Decks by Aaron He

Other Decks in Programming