安利AutoValue第二波


安利AutoValue第二波


简介

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

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

插件Github地址

如,一个普通的Java Model:

public class User {

    public static final int STATIC_FIELD = 0;

    public static void staticMethod() {

    }

    private byte byteField;
    private short shortField;
    private int intField;
    private long longField;
    private char chartField;
    private float floatField;
    private double doubleField;
    private boolean booleanField;
    private String stringField;
    private Set<String> setField;
    private List<String> listField;
    private Map<String, String> mapField;
    private String[] arrayField;

    public byte getByteField() {
        return byteField;
    }

    public void setByteField(byte byteField) {
        this.byteField = byteField;
    }

    public short getShortField() {
        return shortField;
    }

    public void setShortField(short shortField) {
        this.shortField = shortField;
    }

    public int getIntField() {
        return intField;
    }

    public void setIntField(int intField) {
        this.intField = intField;
    }

    public long getLongField() {
        return longField;
    }

    public void setLongField(long longField) {
        this.longField = longField;
    }

    public char getChartField() {
        return chartField;
    }

    public void setChartField(char chartField) {
        this.chartField = chartField;
    }

    public float getFloatField() {
        return floatField;
    }

    public void setFloatField(float floatField) {
        this.floatField = floatField;
    }

    public double getDoubleField() {
        return doubleField;
    }

    public void setDoubleField(double doubleField) {
        this.doubleField = doubleField;
    }

    public boolean isBooleanField() {
        return booleanField;
    }

    public void setBooleanField(boolean booleanField) {
        this.booleanField = booleanField;
    }

    public String getStringField() {
        return stringField;
    }

    public void setStringField(String stringField) {
        this.stringField = stringField;
    }

    public Set<String> getSetField() {
        return setField;
    }

    public void setSetField(Set<String> setField) {
        this.setField = setField;
    }

    public List<String> getListField() {
        return listField;
    }

    public void setListField(List<String> listField) {
        this.listField = listField;
    }

    public Map<String, String> getMapField() {
        return mapField;
    }

    public void setMapField(Map<String, String> mapField) {
        this.mapField = mapField;
    }

    public String[] getArrayField() {
        return arrayField;
    }

    public void setArrayField(String[] arrayField) {
        this.arrayField = arrayField;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (byteField != user.byteField) return false;
        if (shortField != user.shortField) return false;
        if (intField != user.intField) return false;
        if (longField != user.longField) return false;
        if (chartField != user.chartField) return false;
        if (Float.compare(user.floatField, floatField) != 0) return false;
        if (Double.compare(user.doubleField, doubleField) != 0) return false;
        if (booleanField != user.booleanField) return false;
        if (stringField != null ? !stringField.equals(user.stringField) : user.stringField != null) return false;
        if (setField != null ? !setField.equals(user.setField) : user.setField != null) return false;
        if (listField != null ? !listField.equals(user.listField) : user.listField != null) return false;
        if (mapField != null ? !mapField.equals(user.mapField) : user.mapField != null) return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        return Arrays.equals(arrayField, user.arrayField);

    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = (int) byteField;
        result = 31 * result + (int) shortField;
        result = 31 * result + intField;
        result = 31 * result + (int) (longField ^ (longField >>> 32));
        result = 31 * result + (int) chartField;
        result = 31 * result + (floatField != +0.0f ? Float.floatToIntBits(floatField) : 0);
        temp = Double.doubleToLongBits(doubleField);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (booleanField ? 1 : 0);
        result = 31 * result + (stringField != null ? stringField.hashCode() : 0);
        result = 31 * result + (setField != null ? setField.hashCode() : 0);
        result = 31 * result + (listField != null ? listField.hashCode() : 0);
        result = 31 * result + (mapField != null ? mapField.hashCode() : 0);
        result = 31 * result + Arrays.hashCode(arrayField);
        return result;
    }

    @Override
    public String toString() {
        return "User{" +
                "byteField=" + byteField +
                ", shortField=" + shortField +
                ", intField=" + intField +
                ", longField=" + longField +
                ", chartField=" + chartField +
                ", floatField=" + floatField +
                ", doubleField=" + doubleField +
                ", booleanField=" + booleanField +
                ", stringField='" + stringField + '\'' +
                ", setField=" + setField +
                ", listField=" + listField +
                ", mapField=" + mapField +
                ", arrayField=" + Arrays.toString(arrayField) +
                '}';
    }
}

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

@AutoValue
public abstract class User {

    public static final int STATIC_FIELD = 0;

    public static void staticMethod() {

    }

    @SerializedName("byteField")
    public abstract byte byteField();

    @SerializedName("shortField")
    public abstract short shortField();

    @SerializedName("intField")
    public abstract int intField();

    @SerializedName("longField")
    public abstract long longField();

    @SerializedName("chartField")
    public abstract char chartField();

    @SerializedName("floatField")
    public abstract float floatField();

    @SerializedName("doubleField")
    public abstract double doubleField();

    @SerializedName("booleanField")
    public abstract boolean booleanField();

    @Nullable
    @SerializedName("stringField")
    public abstract String stringField();

    @Nullable
    @SerializedName("setField")
    public abstract Set<String> setField();

    @Nullable
    @SerializedName("listField")
    public abstract List<String> listField();

    @Nullable
    @SerializedName("mapField")
    public abstract Map<String, String> mapField();

    @Nullable
    @SerializedName("arrayField")
    public abstract String[] arrayField();
}

操作示范

其他


文章作者: 流水不腐小夏
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 流水不腐小夏 !
 上一篇
Android Model正确使用姿势——AutoValue Android Model正确使用姿势——AutoValue
Android Model正确使用姿势——AutoValue 最近看到几篇博客是关于AutoValue的,然后自己十分喜欢,一下子觉的这样写代码很优雅,所以决定自己也写一篇文章介绍下AutoValue。 本文最先发表于Github,如有转载
2017-05-11
下一篇 
Kotlin之let,apply,with,run函数区别 Kotlin之let,apply,with,run函数区别
Kotlin之let,apply,with,run函数区别 很长一段时间内都一直使用Kotlin这门语言,也只是纯粹使用简单语法,最近有时候写的代码,编辑器自动提示使用let等函数,然后就专门花点时间研究了下。
2017-04-15
  目录