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());}
- java autovalue
- 安利AutoValue第二波
- 使用 Google AutoValue 自动生成代码
- Android AutoValue使用和扩展库
- Android Model正确使用姿势——AutoValue
- Android Model正确使用姿势——AutoValue
- java
- JAVA
- JAVA
- JAVA
- java
- Java
- Java
- JAVA:
- java
- java
- java
- java
- 堆和栈的区别
- MemCache超详细解读
- JQuery实现提交(submit)表单时候验证所有文本框是否为空
- pcl里面使用KdTree来搜索
- string类成员函数的使用方法(二)
- java autovalue
- STM32系列第17篇--待机唤醒
- N皇后问题的递归与非递归解法
- 【ps】查看进程
- java多线程系列:死锁及检测
- iOS获取设备IP地址
- 小公司如何做项目管理?
- linux 静、动态库笔记
- asp.net 点击服务器按钮 不触发后台事件解决方法