auto-value-parcel-adapter 可以处理另一个自动值 Class 的 Typed Set 吗?
Can auto-value-parcel-adapter cope with Typed Set of another auto-value Class?
我正在研究 auto-value
及其扩展,即我的 Android 应用程序中的 auto-value-parcel
和 auto-value-parcel-adapter
。
我有这些型号类:-
@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();
}
}
和
@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();
}
}
我的类型适配器是我的问题所在
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);
}
}
我需要传递给 readTypedArray 的 CREATOR 在 AutoValue_Amoeba 中声明。
我的错误在哪里?对auto-value-parcel
的误解?
我相信我已经确定了如下解决方案:-
目前auto-value-parcel
class
com.ryanharter.auto.value.parcel.AutoValueParcelExtension
有一个方法叫做 generateCreator
:-
FieldSpec generateCreator(ProcessingEnvironment env, TypeName autoValueType, List<Property> properties, TypeName type, Map<TypeMirror, FieldSpec> typeAdapters) {
ClassName creator = ClassName.bestGuess("android.os.Parcelable.Creator");
TypeName creatorOfClass = ParameterizedTypeName.get(creator, type);
...
...
此方法生成类似于此
的Parcelable
CREATOR
public static final Parcelable.Creator<AutoValue_Amoeba> CREATOR = new Parcelable.Creator<AutoValue_Amoeba>() {
@Override
public AutoValue_Amoeba createFromParcel(Parcel in) {
return new AutoValue_Amoeba(
in.readString()
);
}
@Override
public AutoValue_Amoeba[] newArray(int size) {
return new AutoValue_Amoeba[size];
}
};
如果generateCreator方法修改如下:-
FieldSpec generateCreator(ProcessingEnvironment env, TypeName autoValueType, List<Property> properties, TypeName type, Map<TypeMirror, FieldSpec> typeAdapters) {
ClassName creator = ClassName.bestGuess("android.os.Parcelable.Creator");
TypeName creatorOfClass = ParameterizedTypeName.get(creator, autoValueType); // CHANGE MADE HERE!!! swap type with autoValueType
...
...
此方法将生成类似于此
的Parcelable
CREATOR
public static final Parcelable.Creator<Amoeba> CREATOR = new Parcelable.Creator<Amoeba>() {
@Override
public AutoValue_Amoeba createFromParcel(Parcel in) {
return new AutoValue_Amoeba(
in.readString()
);
}
@Override
public AutoValue_Amoeba[] newArray(int size) {
return new AutoValue_Amoeba[size];
}
};
此 CREATOR 现在允许 TypeAdapter 使用 CREATOR,如此处所示
class AmoebaTypeAdapter implements TypeAdapter<Set<Amoeba>> {
@Override
public Set<Amoeba> fromParcel(Parcel in) {
final List<Amoeba> arrayList = new ArrayList<>();
in.readTypedList(arrayList, AutoValue_Amoeba.CREATOR);
return new TreeSet<>(arrayList);
}
@Override
public void toParcel(Set<Amoeba> value, Parcel dest) {
final ArrayList<Amoeba> arrayList = new ArrayList<>(value);
dest.writeTypedList(arrayList);
}
}
AutoValue:包裹扩展无法处理集,但如果您将 属性 转换为 List
,则无需自定义适配器即可开箱即用。如果你想把它当作 Set
你可以这样做。请记住,您可能还想缓存 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();
}
}
我正在研究 auto-value
及其扩展,即我的 Android 应用程序中的 auto-value-parcel
和 auto-value-parcel-adapter
。
我有这些型号类:-
@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();
}
}
和
@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();
}
}
我的类型适配器是我的问题所在
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);
}
}
我需要传递给 readTypedArray 的 CREATOR 在 AutoValue_Amoeba 中声明。
我的错误在哪里?对auto-value-parcel
的误解?
我相信我已经确定了如下解决方案:-
目前auto-value-parcel
class
com.ryanharter.auto.value.parcel.AutoValueParcelExtension
有一个方法叫做 generateCreator
:-
FieldSpec generateCreator(ProcessingEnvironment env, TypeName autoValueType, List<Property> properties, TypeName type, Map<TypeMirror, FieldSpec> typeAdapters) {
ClassName creator = ClassName.bestGuess("android.os.Parcelable.Creator");
TypeName creatorOfClass = ParameterizedTypeName.get(creator, type);
...
...
此方法生成类似于此
的Parcelable
CREATOR
public static final Parcelable.Creator<AutoValue_Amoeba> CREATOR = new Parcelable.Creator<AutoValue_Amoeba>() {
@Override
public AutoValue_Amoeba createFromParcel(Parcel in) {
return new AutoValue_Amoeba(
in.readString()
);
}
@Override
public AutoValue_Amoeba[] newArray(int size) {
return new AutoValue_Amoeba[size];
}
};
如果generateCreator方法修改如下:-
FieldSpec generateCreator(ProcessingEnvironment env, TypeName autoValueType, List<Property> properties, TypeName type, Map<TypeMirror, FieldSpec> typeAdapters) {
ClassName creator = ClassName.bestGuess("android.os.Parcelable.Creator");
TypeName creatorOfClass = ParameterizedTypeName.get(creator, autoValueType); // CHANGE MADE HERE!!! swap type with autoValueType
...
...
此方法将生成类似于此
的Parcelable
CREATOR
public static final Parcelable.Creator<Amoeba> CREATOR = new Parcelable.Creator<Amoeba>() {
@Override
public AutoValue_Amoeba createFromParcel(Parcel in) {
return new AutoValue_Amoeba(
in.readString()
);
}
@Override
public AutoValue_Amoeba[] newArray(int size) {
return new AutoValue_Amoeba[size];
}
};
此 CREATOR 现在允许 TypeAdapter 使用 CREATOR,如此处所示
class AmoebaTypeAdapter implements TypeAdapter<Set<Amoeba>> {
@Override
public Set<Amoeba> fromParcel(Parcel in) {
final List<Amoeba> arrayList = new ArrayList<>();
in.readTypedList(arrayList, AutoValue_Amoeba.CREATOR);
return new TreeSet<>(arrayList);
}
@Override
public void toParcel(Set<Amoeba> value, Parcel dest) {
final ArrayList<Amoeba> arrayList = new ArrayList<>(value);
dest.writeTypedList(arrayList);
}
}
AutoValue:包裹扩展无法处理集,但如果您将 属性 转换为 List
,则无需自定义适配器即可开箱即用。如果你想把它当作 Set
你可以这样做。请记住,您可能还想缓存 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();
}
}