mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-04 16:45:09 -05:00
warn if not serializable, also add basic unit test
This commit is contained in:
parent
dd1ec5f47b
commit
c6df8f1ba1
@ -709,21 +709,33 @@ public class Utility {
|
||||
}
|
||||
|
||||
public static <T> Serializable toSerializableList(List<T> list) {
|
||||
return list instanceof Serializable ?
|
||||
return isExpectedCollectionClass(Serializable.class, list) ?
|
||||
(Serializable) list :
|
||||
new ArrayList<T>(list);
|
||||
}
|
||||
|
||||
public static <T> ArrayList<T> toArrayList(List<T> list) {
|
||||
return list instanceof ArrayList ?
|
||||
return isExpectedCollectionClass(ArrayList.class, list) ?
|
||||
(ArrayList<T>) list :
|
||||
new ArrayList<T>(list);
|
||||
}
|
||||
|
||||
|
||||
public static <T,U> Serializable toSerializableConcurrentMap(ConcurrentMap<T,U> list) {
|
||||
return list instanceof ConcurrentHashMap ?
|
||||
(ConcurrentHashMap<T,U>) list :
|
||||
new ConcurrentHashMap<T,U>(list);
|
||||
public static <T, U> Serializable toSerializableConcurrentMap(ConcurrentMap<T, U> map) {
|
||||
return isExpectedCollectionClass(ConcurrentHashMap.class, map) ?
|
||||
(ConcurrentHashMap<T, U>) map :
|
||||
new ConcurrentHashMap<T, U>(map);
|
||||
}
|
||||
|
||||
private static boolean isExpectedCollectionClass(Class<?> expected, Object instance) {
|
||||
// Objects.requireNonNull(instance); // uncomment when project moves to API level 19
|
||||
if (!expected.isInstance(instance)) {
|
||||
Log.w(K9.LOG_TAG, "Instance class [" + instance.getClass().getName()
|
||||
+ "] would be better as [" + expected.getName()
|
||||
+ "] for performance, to prevent collection copying");
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,23 +4,23 @@ import com.fsck.k9.helper.Utility;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedList;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class UtilityTest extends TestCase {
|
||||
|
||||
public void testToSerializableList() {
|
||||
public void testToArrayList() {
|
||||
LinkedList<String> input = new LinkedList<String>(Arrays.asList("a", "b"));
|
||||
|
||||
Serializable serializableList = Utility.toSerializableList(input);
|
||||
Serializable serializableList = Utility.toArrayList(input);
|
||||
|
||||
assertEquals(serializableList, input);
|
||||
}
|
||||
|
||||
public void testToSerializableListAlreadySerializable() {
|
||||
public void testToArrayListAlreadyArrayList() {
|
||||
ArrayList<String> input = new ArrayList<String>(Arrays.asList("a", "b"));
|
||||
|
||||
Serializable serializableList = Utility.toSerializableList(input);
|
||||
Serializable serializableList = Utility.toArrayList(input);
|
||||
|
||||
assertSame(serializableList, input);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user