0%

安利AutoValue第二波

安利AutoValue第二波


简介

前面介绍了如何使用AutoValue如何使用, 参见这里Android Model正确使用姿势——AutoValue ,如果喜欢或者决定使用AutoValue的小伙伴是不是迫不及待打算开始使用。

那么问题了,项目中原先写好的Model再改成AutovValue的形式,只有几个类还好,如果Model很多,一个个改不得累死。没关系,我这边已经写好了一个AndroidStudio 插件,可以自动把原先的类给转化成AutoValue的形式。

插件Github地址

如,一个普通的Java Model:

1
public class User {
2
3
    public static final int STATIC_FIELD = 0;
4
5
    public static void staticMethod() {
6
7
    }
8
9
    private byte byteField;
10
    private short shortField;
11
    private int intField;
12
    private long longField;
13
    private char chartField;
14
    private float floatField;
15
    private double doubleField;
16
    private boolean booleanField;
17
    private String stringField;
18
    private Set<String> setField;
19
    private List<String> listField;
20
    private Map<String, String> mapField;
21
    private String[] arrayField;
22
23
    public byte getByteField() {
24
        return byteField;
25
    }
26
27
    public void setByteField(byte byteField) {
28
        this.byteField = byteField;
29
    }
30
31
    public short getShortField() {
32
        return shortField;
33
    }
34
35
    public void setShortField(short shortField) {
36
        this.shortField = shortField;
37
    }
38
39
    public int getIntField() {
40
        return intField;
41
    }
42
43
    public void setIntField(int intField) {
44
        this.intField = intField;
45
    }
46
47
    public long getLongField() {
48
        return longField;
49
    }
50
51
    public void setLongField(long longField) {
52
        this.longField = longField;
53
    }
54
55
    public char getChartField() {
56
        return chartField;
57
    }
58
59
    public void setChartField(char chartField) {
60
        this.chartField = chartField;
61
    }
62
63
    public float getFloatField() {
64
        return floatField;
65
    }
66
67
    public void setFloatField(float floatField) {
68
        this.floatField = floatField;
69
    }
70
71
    public double getDoubleField() {
72
        return doubleField;
73
    }
74
75
    public void setDoubleField(double doubleField) {
76
        this.doubleField = doubleField;
77
    }
78
79
    public boolean isBooleanField() {
80
        return booleanField;
81
    }
82
83
    public void setBooleanField(boolean booleanField) {
84
        this.booleanField = booleanField;
85
    }
86
87
    public String getStringField() {
88
        return stringField;
89
    }
90
91
    public void setStringField(String stringField) {
92
        this.stringField = stringField;
93
    }
94
95
    public Set<String> getSetField() {
96
        return setField;
97
    }
98
99
    public void setSetField(Set<String> setField) {
100
        this.setField = setField;
101
    }
102
103
    public List<String> getListField() {
104
        return listField;
105
    }
106
107
    public void setListField(List<String> listField) {
108
        this.listField = listField;
109
    }
110
111
    public Map<String, String> getMapField() {
112
        return mapField;
113
    }
114
115
    public void setMapField(Map<String, String> mapField) {
116
        this.mapField = mapField;
117
    }
118
119
    public String[] getArrayField() {
120
        return arrayField;
121
    }
122
123
    public void setArrayField(String[] arrayField) {
124
        this.arrayField = arrayField;
125
    }
126
127
    @Override
128
    public boolean equals(Object o) {
129
        if (this == o) return true;
130
        if (o == null || getClass() != o.getClass()) return false;
131
132
        User user = (User) o;
133
134
        if (byteField != user.byteField) return false;
135
        if (shortField != user.shortField) return false;
136
        if (intField != user.intField) return false;
137
        if (longField != user.longField) return false;
138
        if (chartField != user.chartField) return false;
139
        if (Float.compare(user.floatField, floatField) != 0) return false;
140
        if (Double.compare(user.doubleField, doubleField) != 0) return false;
141
        if (booleanField != user.booleanField) return false;
142
        if (stringField != null ? !stringField.equals(user.stringField) : user.stringField != null) return false;
143
        if (setField != null ? !setField.equals(user.setField) : user.setField != null) return false;
144
        if (listField != null ? !listField.equals(user.listField) : user.listField != null) return false;
145
        if (mapField != null ? !mapField.equals(user.mapField) : user.mapField != null) return false;
146
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
147
        return Arrays.equals(arrayField, user.arrayField);
148
149
    }
150
151
    @Override
152
    public int hashCode() {
153
        int result;
154
        long temp;
155
        result = (int) byteField;
156
        result = 31 * result + (int) shortField;
157
        result = 31 * result + intField;
158
        result = 31 * result + (int) (longField ^ (longField >>> 32));
159
        result = 31 * result + (int) chartField;
160
        result = 31 * result + (floatField != +0.0f ? Float.floatToIntBits(floatField) : 0);
161
        temp = Double.doubleToLongBits(doubleField);
162
        result = 31 * result + (int) (temp ^ (temp >>> 32));
163
        result = 31 * result + (booleanField ? 1 : 0);
164
        result = 31 * result + (stringField != null ? stringField.hashCode() : 0);
165
        result = 31 * result + (setField != null ? setField.hashCode() : 0);
166
        result = 31 * result + (listField != null ? listField.hashCode() : 0);
167
        result = 31 * result + (mapField != null ? mapField.hashCode() : 0);
168
        result = 31 * result + Arrays.hashCode(arrayField);
169
        return result;
170
    }
171
172
    @Override
173
    public String toString() {
174
        return "User{" +
175
                "byteField=" + byteField +
176
                ", shortField=" + shortField +
177
                ", intField=" + intField +
178
                ", longField=" + longField +
179
                ", chartField=" + chartField +
180
                ", floatField=" + floatField +
181
                ", doubleField=" + doubleField +
182
                ", booleanField=" + booleanField +
183
                ", stringField='" + stringField + '\'' +
184
                ", setField=" + setField +
185
                ", listField=" + listField +
186
                ", mapField=" + mapField +
187
                ", arrayField=" + Arrays.toString(arrayField) +
188
                '}';
189
    }
190
}

转化过就是这样,基本满足AutoValue使用:

1
@AutoValue
2
public abstract class User {
3
4
    public static final int STATIC_FIELD = 0;
5
6
    public static void staticMethod() {
7
8
    }
9
10
    @SerializedName("byteField")
11
    public abstract byte byteField();
12
13
    @SerializedName("shortField")
14
    public abstract short shortField();
15
16
    @SerializedName("intField")
17
    public abstract int intField();
18
19
    @SerializedName("longField")
20
    public abstract long longField();
21
22
    @SerializedName("chartField")
23
    public abstract char chartField();
24
25
    @SerializedName("floatField")
26
    public abstract float floatField();
27
28
    @SerializedName("doubleField")
29
    public abstract double doubleField();
30
31
    @SerializedName("booleanField")
32
    public abstract boolean booleanField();
33
34
    @Nullable
35
    @SerializedName("stringField")
36
    public abstract String stringField();
37
38
    @Nullable
39
    @SerializedName("setField")
40
    public abstract Set<String> setField();
41
42
    @Nullable
43
    @SerializedName("listField")
44
    public abstract List<String> listField();
45
46
    @Nullable
47
    @SerializedName("mapField")
48
    public abstract Map<String, String> mapField();
49
50
    @Nullable
51
    @SerializedName("arrayField")
52
    public abstract String[] arrayField();
53
}

操作示范

其他

坚持原创技术分享,您的支持将鼓励我继续创作!