java - Generic Parcelable CircularArray -
i'm implementing generic circular array , make parcelable. able have circular array generic, need pass type in constructor. however, implementing parcelable
requires constructor passes parcel
. 2 requirements conflicting , can't think of way around this.
this attempt, fail on circulararray(parcel in)
constructor it's missing type.
import android.os.parcel; import android.os.parcelable; import android.util.log; import java.lang.reflect.array; import java.util.arrays; /** * created dav on 08/06/15. */ public class circulararray<t> implements parcelable { public static final int default_size = 10; protected class<t> c; protected final t[] mqueue; protected int curr = 0; protected int n; public circulararray(class<t> c, int n) { this.n = n; this.c = c; mqueue = (t[]) new object[n]; curr = 0; } public circulararray(class<t> c) { this(c, default_size); } public circulararray(parcel in) { mqueue = (t[]) in.readarray(circulararray.class.getclassloader()); n = mqueue.length; } public void fill(t value) { arrays.fill(mqueue, value); } public void add(t item) { mqueue[curr] = item; curr = (curr + 1) % n; } /** * sort elements in right order0 , return them * @return queue in correct order. */ public t[] getall() { int last = curr+1% n; int counter = 0; log.i("circular", "component " + mqueue.getclass().getcomponenttype()); t[] out = (t[]) array.newinstance(c, n); //new object[n]; (int = curr; i< n; i++) { out[counter++] = mqueue[i]; } (int = 0; i<curr; i++) { out[counter++] = mqueue[i]; } return out; } public t getone() { return mqueue[curr]; } @override public int describecontents() { return 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writearray(mqueue); } public static final parcelable.creator<circulararray> creator = new parcelable.creator<circulararray>() { public circulararray createfromparcel(parcel in) { return new circulararray(in); } public circulararray[] newarray(int size) { return new circulararray[size]; } }; }
Comments
Post a Comment