Created
October 21, 2016 02:31
-
-
Save cattaka/09d14678ef0d1ac10db202b2201241cb to your computer and use it in GitHub Desktop.
For CatHandsGendroid 0.5.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.ContentValues; | |
import android.database.Cursor; | |
import android.os.Parcel; | |
import android.os.Parcelable; | |
import net.cattaka.util.cathandsgendroid.accessor.Accessors; | |
import net.cattaka.util.cathandsgendroid.accessor.IAccessor; | |
import net.cattaka.util.cathandsgendroid.accessor.ParcelableAccessor; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import io.realm.RealmList; | |
import io.realm.RealmObject; | |
/** | |
* These codes are licensed under CC0. | |
* <p/> | |
* Created by cattaka on 2016/10/21. | |
*/ | |
public class RealmListAccessor<T extends RealmObject> implements IAccessor<RealmList<T>> { | |
public static <T> IAccessor<T> createAccessor(Class listClazz, Class objClass) { | |
@SuppressWarnings("unchecked") | |
IAccessor child = ParcelableAccessor.createAccessor((Class<? extends Parcelable>) objClass); | |
return new RealmListAccessor(child); | |
} | |
private IAccessor<T> child; | |
public RealmListAccessor(IAccessor<T> child) { | |
this.child = child; | |
} | |
@Override | |
public RealmList<T> readFromStream(DataInputStream in) throws IOException { | |
int n = in.readInt(); | |
if (n >= 0) { | |
RealmList<T> list = new RealmList<T>(); | |
for (int i = 0; i < n; i++) { | |
list.add(child.readFromStream(in)); | |
} | |
return list; | |
} else { | |
return null; | |
} | |
} | |
@Override | |
public void writeToStream(DataOutputStream out, RealmList<T> value) throws IOException { | |
if (value != null) { | |
out.writeInt(value.size()); | |
for (T v : value) { | |
child.writeToStream(out, v); | |
} | |
} else { | |
out.writeInt(-1); | |
} | |
} | |
@Override | |
public RealmList<T> readFromParcel(Parcel p) { | |
int n = p.readInt(); | |
if (n >= 0) { | |
RealmList<T> list = new RealmList<T>(); | |
for (int i = 0; i < n; i++) { | |
list.add(child.readFromParcel(p)); | |
} | |
return list; | |
} else { | |
return null; | |
} | |
} | |
@Override | |
public void writeToParcel(Parcel p, RealmList<T> value) { | |
if (value != null) { | |
p.writeInt(value.size()); | |
for (T v : value) { | |
child.writeToParcel(p, v); | |
} | |
} else { | |
p.writeInt(-1); | |
} | |
} | |
@Override | |
public RealmList<T> readFromCursor(Cursor c, int idx) { | |
byte[] bs = Accessors.BlobAccessor.createAccessor(byte[].class).readFromCursor(c, idx); | |
if (bs != null) { | |
try { | |
DataInputStream din = new DataInputStream(new ByteArrayInputStream(bs)); | |
return readFromStream(din); | |
} catch (IOException e) { | |
return null; | |
} | |
} else { | |
return null; | |
} | |
} | |
@Override | |
public void putToContentValues(ContentValues values, String columnName, RealmList<T> value) { | |
byte[] bs = null; | |
if (value != null) { | |
try { | |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
DataOutputStream dout = new DataOutputStream(bout); | |
writeToStream(dout, value); | |
dout.flush(); | |
bs = bout.toByteArray(); | |
} catch (IOException e) { | |
// ignore | |
} | |
} | |
values.put(columnName, bs); | |
} | |
@Override | |
public String stringValue(RealmList<T> value) { | |
return value != null ? String.valueOf(value) : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment