I am investigating auto-value
and its extensions, namely auto-value-parcel
and auto-value-parcel-adapter
within my Android application.
I have these model classes:-
@AutoValue
public abstract class Xenarchaeota implements Parcelable {
@ParcelAdapter(AmoebaTypeAdapter.class)
public abstract Set<Amoeba> amoebas();
public static Builder builder() {
return new AutoValue_Xenarchaeota.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setAmoebas(Set<Amoeba> value);
public abstract Xenarchaeota build();
}
}
and
@AutoValue
public abstract class Amoeba implements Parcelable {
public abstract String surname();
public static Builder builder() {
return new AutoValue_Amoeba.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder surname(final String value);
public abstract Amoeba build();
}
}
My type adapter is where my issues arise
class AmoebaTypeAdapter implements TypeAdapter<Set<Amoeba>> {
@Override
public Set<Amoeba> fromParcel(Parcel in) {
final ArrayList<Amoeba> arrayList = new ArrayList<>();
in.readTypedList(arrayList, Amoeba.CREATOR); // How to access the CREATOR?
return new TreeSet<>(arrayList);
}
@Override
public void toParcel(Set<Amoeba> value, Parcel dest) {
final ArrayList<Amoeba> arrayList = new ArrayList<>(value);
dest.writeTypedList(arrayList);
}
}
The CREATOR I need to pass to the readTypedArray is declared in AutoValue_Amoeba.
Where is my mistake? misunderstanding of auto-value-parcel
?
AutoValue: Parcel extension can't handle sets, but if you convert the property to a List
things will work out of the box without a custom adapter. If you want to treat it as a Set
you could do this. Keep in mind you'll probably also want to cache the Set
.
@AutoValue
public abstract class Xenarchaeota implements Parcelable {
abstract List<Amoeba> amoebaList();
public Set<Amoeba> amoebas() {
return new TreeSet(amoebaList());
}
public static Builder builder() {
return new AutoValue_Xenarchaeota.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setAmoebas(Set<Amoeba> value);
public abstract Xenarchaeota build();
}
}