Monday, July 22, 2019

Introduction to Google AutoValue



https://www.baeldung.com/introduction-to-autovalue
@AutoValue
public abstract class AutoValueMoney {
    public abstract String getCurrency();
    public abstract long getAmount();
   
    public static AutoValueMoney create(String currency, long amount) {
        return new AutoValue_AutoValueMoney(currency, amount);
    }
}
What has happened is that we write an abstract class, define abstract accessors for it but no fields, we annotate the class with @AutoValue all totalling to only 8 lines of code, and javac generates a concrete subclass for us which looks like this:
public final class AutoValue_AutoValueMoney extends AutoValueMoney {
    private final String currency;
    private final long amount;
   
    AutoValue_AutoValueMoney(String currency, long amount) {
        if (currency == null) throw new NullPointerException(currency);
        this.currency = currency;
        this.amount = amount;
    }
   
    // standard getters

https://mincong-h.github.io/2018/08/21/why-you-should-use-auto-value-in-java/
 value type is class without identity: two instances are considered interchangeable as long as they have equal field values


AutoValue
public abstract class Transaction {

  public static Transaction of(long id, String user) {
    return new AutoValue_Transaction(id, user);
  }

  public abstract long id();

  public abstract String user();
}

Behind the scenes, Auto Value generates all the private fields, the constructor, hashCode()equals(), and toString() for you. The generated class always starts with “AutoValue_“, more explicitly, it naming convention is AutoValue_<MyClass
I would also summary it as a comparison table:
ItemWithout AutoValueAutoValue
Auto attr declarationNY
Auto gettersNN
Auto toString()NY
Auto hashCode()NY
Auto equals()NY
Immutable    Y (*)Y
Auto update toString()NY
Auto update hashCode()NY
Auto update equals()NY


@AutoValue
public abstract class Transaction {

  public static Builder builder() {
    return new AutoValue_Transaction.Builder();
  }

  public abstract long id();

  public abstract String user();

  @AutoValue.Builder
  public abstract static class Builder {
    abstract Builder id(long id);

    abstract Builder user(String user);

    abstract Transaction build();
  }
}

When using it, you can do:

Transaction t = Transaction.builder().id(1).user("foo").build();
https://github.com/google/auto/blob/master/value/userguide/why.md
AutoValue is the only solution to the value class problem in Java having all of the following characteristics:
  • API-invisible (callers cannot become dependent on your choice to use it)
  • No runtime dependencies
  • Negligible cost to performance
  • Very few limitations on what your class can do
  • Extralinguistic "magic" kept to an absolute minimum (uses only standard Java platform technologies, in the manner they were intended)
https://docs.google.com/presentation/d/14u_h-lMn7f1rXE1nDiLX0azS3IkgjGl5uxp5jGJ75RE/edit#slide=id.g2a5e9c4a8_0104
With Lombok, a class with @Value need only declare fields. Lombok hacks the Java compiler to insert the constructor, getters, equals, hashCode, and toString into the class as it is compiled.

  • Pros:
    • Maximally concise value classes.
  • Cons:
    • The inserted code is invisible. This makes for a poor experience with tools, such as debuggers and code explorers.
    • The compiler hacks are non-standard and fragile.
    • Lombok functions outside the boundaries of how things work in Java. It is extralinguistic. This is a dangerous line to cross that is sure to cause confusion.


Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts