Share
Embed
Transcript
14 slides
-
Value class • Values container • Final and immutable •
Without identity/Interchangeable • equals/hashCode/toString -
public class Book01 { private final String title; private final
String author; private final int pubYear; private final int pageCount; public Book01(String title, String author, int pubYear, int pageCount) { this.title = title; this.author = author; this.pubYear = pubYear; this.pageCount = pageCount; } public String getTitle() { return title; } public String getAuthor() { return author; } public int getPubYear() { return pubYear; } public int getPageCount() { return pageCount; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Book01 book = (Book01) o; if (pubYear != book.pubYear) return false; if (pageCount != book.pageCount) return false; if (title != null ? !title.equals(book01.title) : book.title != null) return false; return author != null ? author.equals(book.author) : book.author == null; } @Override public int hashCode() { int result = title != null ? title.hashCode() : 0; result = 31 * result + (author != null ? author.hashCode() : 0); result = 31 * result + pubYear; result = 31 * result + pageCount; return result; } @Override public String toString() { return "Book01{" + "title='" + title + '\'' + ", author='" + author + '\'' + ", pubYear=" + pubYear + ", pageCount=" + pageCount + '}'; }} -
AutoValue • Solve problem of creating value classes • Simple
to integrate and use • No runtime dependencies • Negligible cost to performance • Created by Google & Open Sourced • https://github.com/google/auto/tree/master/value -
Adding to project // build.gradle apply plugin: 'android-apt' ... dependencies
{ ... // AutoValue provided 'com.google.auto.value:auto-value:1.2' apt 'com.google.auto.value:auto-value:1.2' ... } -
Example #1 @AutoValue public abstract class Book { public static
Book create(String title, String author, int pubYer, int pageCount) { return new AutoValue_Book(title, author, pubYear, pageCount); } abstract String title(); abstract String author(); abstract int pubYear(); abstract int pageCount(); } -
Example #2 - Builder @AutoValue public abstract class Book {
abstract String title(); abstract String author(); static Builder builder() { return new AutoValue_Book.Builder(); } @AutoValue.Builder abstract static class Builder { abstract Builder title(String title); abstract Builder author(String author); abstract Book build(); }} -
AutoValue Extensions • Easy way to extend AutoValue • You
can create your own extension • or use existing one, e.g. ◦ auto-parcel ◦ auto-value-gson ◦ auto-value-cursor -
What else? • Intellij/Android Studio generator - https://www. jetbrains.com/help/idea/2016.1/generate-equals-and- hashcode-wizard.html
• Data classes in Kotlin - https://kotlinlang. org/docs/reference/data-classes.html • Project Lombok - https://projectlombok.org/ • Apache Commons(HashCodeBuilder and EqualsBuilder) - https://commons.apache.org/ -
Resources • https://github.com/mrmike/AutoValueSample • https://github.com/google/auto/tree/master/value • https://goo.gl/mkft3F • http://jakewharton.com/auto-value-extensions-ny-android- meetup/
• https://github.com/frankiesardo/auto-parcel • https://github.com/rharter/auto-value-gson • https://github.com/gabrielittner/auto-value-cursor • https://bitbucket.org/hvisser/android-apt