android - convert protobuff class as model and store its value using realm -


i generating protobuf class using squareup wire protobuf libary

here proto file

syntax = "proto2";  package squareup.dinosaurs;  option java_package = "com.squareup.dinosaurs";   message dinosaur {   // common name of dinosaur, "stegosaurus".   optional string name = 1;    // urls images of dinosaur.   repeated string picture_urls = 2;  } 

and here auto generated code

// code generated wire protocol buffer compiler, not edit. // source file: dinosaur/dinosaur.proto @ 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();       (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 dinosaur, "stegosaurus".    */   public final string name;    /**    * urls images of 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 dinosaur, "stegosaurus".      */     public builder name(string name) {       this.name = name;       return this;     }      /**      * urls images of 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());     }   } } 

now issue want directly store value of dinosaur database using realm in android. want dinosaur class act model. problem dinosaur class declared final cant derive it.

so there design pattern or way exists reuse or convert dinosaur class model?

you cannot use wire dinosaur realm wire require extend message class, while realm require extend realmobject.

if want combine 2 can create realmdinosaur class accept wire dinosaur. this:

public class realmdinosaur extends realmobject {    private string name;   private realmlist<realmstring> pictureurls;    public realmdinosaur(dinosaur dino) {     // fill realm fields. note realm doesn't support lists      // primitive strings yet.     // see https://realm.io/docs/java/latest/#primitive-lists   }    // getter , setters  }  realm.begintransaction(); realm.copytorealm(new realmdinosaur(wiredinosaur)); realm.committransaction(); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -