java autovalue

来源:互联网 发布:ons游戏解压软件 编辑:程序博客网 时间:2024/05/17 23:14

java autovalue

参考资料:https://github.com/google/auto/blob/master/value/userguide/index.md
https://docs.google.com/presentation/d/14u_h-lMn7f1rXE1nDiLX0azS3IkgjGl5uxp5jGJ75RE/edit#slide=id.g2a5e9c4a8_032
“AutoValue is a great tool for eliminating the drudgery of writing mundane value classes in Java. It encapsulates much of the advice in Effective Java Chapter 2, and frees you to concentrate on the more interesting aspects of your program. The resulting program is likely to be shorter, clearer, and freer of bugs. Two thumbs up.”
– Joshua Bloch, author, Effective Java
也就是说autovalue是一个在java实体类中减少一些平凡又累的代码的工具,使你更专注的处理项目的其他逻辑,它可以使代码更少,更干净,跟少的bug。

Why use AutoValue?

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)

我们通常写java实体类代码都是把成员变量写出来,私有的,加上get、set方法或者直接写成公有的(不用写get、set方法),还有toSting放法,有些还有equal和hashcode方法。
现在我们可以这样写:

import com.google.auto.value.AutoValue;@AutoValueabstract class Animal {  static Animal create(String name, int numberOfLegs) {    // See "How do I...?" below for nested classes.    return new AutoValue_Animal(name, numberOfLegs);  }  abstract String name();  abstract int numberOfLegs();}

也可以这样:

import com.google.auto.value.AutoValue;@AutoValueabstract class Animal {  abstract String name();  abstract int numberOfLegs();  static Builder builder() {    return new AutoValue_Animal.Builder();  }  @AutoValue.Builder  abstract static class Builder {    abstract Builder name(String value);    abstract Builder numberOfLegs(int value);    abstract Animal build();  }}

第一次会报错,编译一遍就可以了。
因为编译之后会产生实现类:

import javax.annotation.Generated;@Generated("com.google.auto.value.processor.AutoValueProcessor")final class AutoValue_Animal extends Animal {  private final String name;  private final int numberOfLegs;  AutoValue_Animal(String name, int numberOfLegs) {    if (name == null) {      throw new NullPointerException("Null name");    }    this.name = name;    this.numberOfLegs = numberOfLegs;  }  @Override  String name() {    return name;  }  @Override  int numberOfLegs() {    return numberOfLegs;  }  @Override  public String toString() {    return "Animal{"        + "name=" + name + ", "        + "numberOfLegs=" + numberOfLegs + "}";  }  @Override  public boolean equals(Object o) {    if (o == this) {      return true;    }    if (o instanceof Animal) {      Animal that = (Animal) o;      return this.name.equals(that.name())          && this.numberOfLegs == that.numberOfLegs();    }    return false;  }  @Override  public int hashCode() {    int h = 1;    h *= 1000003;    h ^= this.name.hashCode();    h *= 1000003;    h ^= this.numberOfLegs;    return h;  }}

在单元测试中应用如下:

public void testAnimal() {  Animal dog = Animal.create("dog", 4);  assertEquals("dog", dog.name());  assertEquals(4, dog.numberOfLegs());  // You probably don't need to write assertions like these; just illustrating.  assertTrue(Animal.create("dog", 4).equals(dog));  assertFalse(Animal.create("cat", 4).equals(dog));  assertFalse(Animal.create("dog", 2).equals(dog));  assertEquals("Animal{name=dog, numberOfLegs=4}", dog.toString());}
0 0
原创粉丝点击