Support converting integer to/from PersistableBundle
This CL is for facilitating converting Map and List that contains integer type objects. Bug: 163604823 Test: PersistableBundleUtilsTest(new tests added) Change-Id: I24239caf70035e19c3fb5eb4a85b6a0c6ccadb5a
This commit is contained in:
@@ -45,6 +45,7 @@ public class PersistableBundleUtils {
|
||||
|
||||
private static final String PARCEL_UUID_KEY = "PARCEL_UUID";
|
||||
private static final String BYTE_ARRAY_KEY = "BYTE_ARRAY_KEY";
|
||||
private static final String INTEGER_KEY = "INTEGER_KEY";
|
||||
|
||||
/**
|
||||
* Functional interface to convert an object of the specified type to a PersistableBundle.
|
||||
@@ -75,6 +76,21 @@ public class PersistableBundleUtils {
|
||||
T fromPersistableBundle(PersistableBundle in);
|
||||
}
|
||||
|
||||
/** Serializer to convert an integer to a PersistableBundle. */
|
||||
public static final Serializer<Integer> INTEGER_SERIALIZER =
|
||||
(i) -> {
|
||||
final PersistableBundle result = new PersistableBundle();
|
||||
result.putInt(INTEGER_KEY, i);
|
||||
return result;
|
||||
};
|
||||
|
||||
/** Deserializer to convert a PersistableBundle to an integer. */
|
||||
public static final Deserializer<Integer> INTEGER_DESERIALIZER =
|
||||
(bundle) -> {
|
||||
Objects.requireNonNull(bundle, "PersistableBundle is null");
|
||||
return bundle.getInt(INTEGER_KEY);
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts a ParcelUuid to a PersistableBundle.
|
||||
*
|
||||
|
||||
@@ -200,4 +200,15 @@ public class PersistableBundleUtilsTest {
|
||||
|
||||
assertArrayEquals(byteArray, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntegerConversionLossless() throws Exception {
|
||||
final int testInt = 1;
|
||||
final PersistableBundle integerBundle =
|
||||
PersistableBundleUtils.INTEGER_SERIALIZER.toPersistableBundle(testInt);
|
||||
final int result =
|
||||
PersistableBundleUtils.INTEGER_DESERIALIZER.fromPersistableBundle(integerBundle);
|
||||
|
||||
assertEquals(testInt, result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user