gpt4 book ai didi

android - 将 protobuff 类转换为模型并使用 Realm 存储其值

转载 作者:搜寻专家 更新时间:2023-11-01 07:51:12 25 4
gpt4 key购买 nike

我正在使用 Squareup Wire protobuf 生成 protobuf 类图书馆

这是我的原型(prototype)文件

syntax = "proto2";

package squareup.dinosaurs;

option java_package = "com.squareup.dinosaurs";


message Dinosaur {
// Common name of this dinosaur, like "Stegosaurus".
optional string name = 1;

// URLs with images of this dinosaur.
repeated string picture_urls = 2;

}

这是我自动生成的代码

// Code generated by Wire protocol buffer compiler, do not edit.
// Source file: dinosaur/dinosaur.proto at 8:1
package com.squareup.dinosaurs;

import com.squareup.wire.FieldEncoding;
import com.squareup.wire.Message;
import com.squareup.wire.ProtoAdapter;
import com.squareup.wire.ProtoReader;
import com.squareup.wire.ProtoWriter;
import java.io.IOException;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;
import java.util.List;
import okio.ByteString;

public final class Dinosaur extends Message<Dinosaur, Dinosaur.Builder> {
public static final ProtoAdapter<Dinosaur> ADAPTER = new ProtoAdapter<Dinosaur>(FieldEncoding.LENGTH_DELIMITED, Dinosaur.class) {
@Override
public int encodedSize(Dinosaur value) {
return (value.name != null ? ProtoAdapter.STRING.encodedSizeWithTag(1, value.name) : 0)
+ ProtoAdapter.STRING.asRepeated().encodedSizeWithTag(2, value.picture_urls)
+ value.unknownFields().size();
}

@Override
public void encode(ProtoWriter writer, Dinosaur value) throws IOException {
if (value.name != null) ProtoAdapter.STRING.encodeWithTag(writer, 1, value.name);
if (value.picture_urls != null) ProtoAdapter.STRING.asRepeated().encodeWithTag(writer, 2, value.picture_urls);
writer.writeBytes(value.unknownFields());
}

@Override
public Dinosaur decode(ProtoReader reader) throws IOException {
Builder builder = new Builder();
long token = reader.beginMessage();
for (int tag; (tag = reader.nextTag()) != -1;) {
switch (tag) {
case 1: builder.name(ProtoAdapter.STRING.decode(reader)); break;
case 2: builder.picture_urls.add(ProtoAdapter.STRING.decode(reader)); break;
default: {
FieldEncoding fieldEncoding = reader.peekFieldEncoding();
Object value = fieldEncoding.rawProtoAdapter().decode(reader);
builder.addUnknownField(tag, fieldEncoding, value);
}
}
}
reader.endMessage(token);
return builder.build();
}

@Override
public Dinosaur redact(Dinosaur value) {
Builder builder = value.newBuilder();
builder.clearUnknownFields();
return builder.build();
}
};

private static final long serialVersionUID = 0L;

public static final String DEFAULT_NAME = "";

/**
* Common name of this dinosaur, like "Stegosaurus".
*/
public final String name;

/**
* URLs with images of this dinosaur.
*/
public final List<String> picture_urls;

public Dinosaur(String name, List<String> picture_urls) {
this(name, picture_urls, ByteString.EMPTY);
}

public Dinosaur(String name, List<String> picture_urls, ByteString unknownFields) {
super(unknownFields);
this.name = name;
this.picture_urls = immutableCopyOf("picture_urls", picture_urls);
}

@Override
public Builder newBuilder() {
Builder builder = new Builder();
builder.name = name;
builder.picture_urls = copyOf("picture_urls", picture_urls);
builder.addUnknownFields(unknownFields());
return builder;
}

@Override
public boolean equals(Object other) {
if (other == this) return true;
if (!(other instanceof Dinosaur)) return false;
Dinosaur o = (Dinosaur) other;
return equals(unknownFields(), o.unknownFields())
&& equals(name, o.name)
&& equals(picture_urls, o.picture_urls);
}

@Override
public int hashCode() {
int result = super.hashCode;
if (result == 0) {
result = unknownFields().hashCode();
result = result * 37 + (name != null ? name.hashCode() : 0);
result = result * 37 + (picture_urls != null ? picture_urls.hashCode() : 1);
super.hashCode = result;
}
return result;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (name != null) builder.append(", name=").append(name);
if (picture_urls != null) builder.append(", picture_urls=").append(picture_urls);
return builder.replace(0, 2, "Dinosaur{").append('}').toString();
}

public static final class Builder extends com.squareup.wire.Message.Builder<Dinosaur, Builder> {
public String name;

public List<String> picture_urls;

public Builder() {
picture_urls = newMutableList();
}

/**
* Common name of this dinosaur, like "Stegosaurus".
*/
public Builder name(String name) {
this.name = name;
return this;
}

/**
* URLs with images of this dinosaur.
*/
public Builder picture_urls(List<String> picture_urls) {
checkElementsNotNull(picture_urls);
this.picture_urls = picture_urls;
return this;
}

@Override
public Dinosaur build() {
return new Dinosaur(name, picture_urls, buildUnknownFields());
}
}
}

现在的问题是我想使用 Realm in androidDinosaur 的值直接存储到数据库中.我希望 Dinosaur 类充当模型。但问题是 Dinosaur 类被声明为最终类,所以我什至无法派生它。

那么是否存在任何设计模式或方法可以重用或将 Dinosaur 类转换为模型?

最佳答案

您不能将 Wire Dinosaur 与 Realm 一起使用,因为 Wire 还需要您扩展 Message 类,而 Realm 需要您扩展 RealmObject .

如果您想将两者结合起来,您可以创建一个 RealmDinosaur 类来接受有线恐龙。像这样:

public class RealmDinosaur extends RealmObject {

private String name;
private RealmList<RealmString> pictureUrls;

public RealmDinosaur(Dinosaur dino) {
// Fill Realm fields. Note that Realm doesn't support Lists
// with primitive strings yet.
// See https://realm.io/docs/java/latest/#primitive-lists
}

// getter and setters

}

realm.beginTransaction();
realm.copyToRealm(new RealmDinosaur(wireDinosaur));
realm.commitTransaction();

关于android - 将 protobuff 类转换为模型并使用 Realm 存储其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34818971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com