Skip to content

Instantly share code, notes, and snippets.

@kaushikgopal
Last active June 26, 2017 16:30
Show Gist options
  • Save kaushikgopal/9eea148a2188dc58fe37 to your computer and use it in GitHub Desktop.
Save kaushikgopal/9eea148a2188dc58fe37 to your computer and use it in GitHub Desktop.
Parcelable version of Android's SparseArray. Apache 2 licensed
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseArray;
import junit.framework.TestCase;
public class ParcelableSparseArray
extends SparseArray<Object>
implements Parcelable {
public static final Parcelable.Creator<ParcelableSparseArray> CREATOR = new Creator<ParcelableSparseArray>() {
@Override
public ParcelableSparseArray createFromParcel(Parcel source) {
return new ParcelableSparseArray(source);
}
@Override
public ParcelableSparseArray[] newArray(int size) {
return new ParcelableSparseArray[size];
}
};
private SparseArray<Object> _sparseArray;
public ParcelableSparseArray() {
super();
}
// -----------------------------------------------------------------------
// Parcelable implementation
private ParcelableSparseArray(Parcel in) {
_sparseArray = in.readSparseArray(ParcelableSparseArray.class.getClassLoader());
}
public Object[] values() {
Object[] values = new Object[size()];
for (int i = 0; i < size(); i++) {
values[i] = valueAt(i);
}
return values;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int _) {
out.writeSparseArray(_sparseArray);
}
// -----------------------------------------------------------------------
// Tests
public class ParcelableSparseArrayTest
extends TestCase {
public static final String TEST_STRING = "Test String";
public static final int TEST_INTEGER = 3;
public static final Event TEST_EVENT = new Event();
private ParcelableSparseArray _testSparserray;
public void test_IsParcelable() {
Bundle testBundle = new Bundler("testSparseArray", _testSparserray).build();
Intent testIntent = new Intent("someAction");
testIntent.putExtras(testBundle);
ParcelableSparseArray testSource = testIntent.getParcelableExtra("testSparseArray");
assertThat(testSource.get(1)).isEqualTo(TEST_STRING);
assertThat(testSource.get(3)).isEqualTo(TEST_INTEGER);
assertThat(testSource.get(5)).isEqualTo(TEST_EVENT);
}
public void test_ValuesReturnsObjectArray() {
Object[] testObjectWithValues = _testSparserray.values();
assertThat(testObjectWithValues.length).isEqualTo(3);
assertThat(testObjectWithValues[0]).isEqualTo(TEST_STRING);
assertThat(testObjectWithValues[1]).isEqualTo(TEST_INTEGER);
assertThat(testObjectWithValues[2]).isEqualTo(TEST_EVENT);
}
@Override
protected void setUp() throws Exception {
super.setUp();
_testSparserray = new ParcelableSparseArray();
_testSparserray.put(1, TEST_STRING);
_testSparserray.put(3, TEST_INTEGER);
_testSparserray.put(5, TEST_EVENT);
}
}
}
@gabin8
Copy link

gabin8 commented Jun 15, 2017

it doesn't work

@kaushikgopal
Copy link
Author

Haven't revisited this in sometime but are you folks saying the test also doesn't run successfully?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment