diff options
author | Tyler Wear <twear@quicinc.com> | 2020-01-27 10:55:38 -0800 |
---|---|---|
committer | Tyler Wear <twear@quicinc.com> | 2020-01-27 10:55:38 -0800 |
commit | 08e8216c6d32b617211b40f4160446659c3d3a99 (patch) | |
tree | 6cedcafbe1a56845a0f1513f019e3423a078b336 | |
parent | e5dd2ee84ddec815a0833790d5176c7d033a8531 (diff) | |
download | net-08e8216c6d32b617211b40f4160446659c3d3a99.tar.gz |
Update CompareOrUpdateResult with NULL Check
Like CompareResult, add in null check for input objects.
Test: updated unit test
Change-Id: Iea63752ff23a5bfb1cc5cb716ec579bec6c403bf
-rw-r--r-- | common/src_frameworkcommon/android/net/util/LinkPropertiesUtils.java | 27 | ||||
-rw-r--r-- | common/tests/unit/src/android/net/util/LinkPropertiesUtilsTest.java | 12 |
2 files changed, 26 insertions, 13 deletions
diff --git a/common/src_frameworkcommon/android/net/util/LinkPropertiesUtils.java b/common/src_frameworkcommon/android/net/util/LinkPropertiesUtils.java index c50ca0b6..bd96edbf 100644 --- a/common/src_frameworkcommon/android/net/util/LinkPropertiesUtils.java +++ b/common/src_frameworkcommon/android/net/util/LinkPropertiesUtils.java @@ -91,23 +91,26 @@ public final class LinkPropertiesUtils { Function<T, K> keyCalculator) { HashMap<K, T> updateTracker = new HashMap<>(); - for (T oldItem : oldItems) { - updateTracker.put(keyCalculator.apply(oldItem), oldItem); + if (oldItems != null) { + for (T oldItem : oldItems) { + updateTracker.put(keyCalculator.apply(oldItem), oldItem); + } } - for (T newItem : newItems) { - T oldItem = updateTracker.remove(keyCalculator.apply(newItem)); - if (oldItem != null) { - if (!oldItem.equals(newItem)) { - // Update of existing item. - updated.add(newItem); + if (newItems != null) { + for (T newItem : newItems) { + T oldItem = updateTracker.remove(keyCalculator.apply(newItem)); + if (oldItem != null) { + if (!oldItem.equals(newItem)) { + // Update of existing item. + updated.add(newItem); + } + } else { + // New item. + added.add(newItem); } - } else { - // New item. - added.add(newItem); } } - removed.addAll(updateTracker.values()); } diff --git a/common/tests/unit/src/android/net/util/LinkPropertiesUtilsTest.java b/common/tests/unit/src/android/net/util/LinkPropertiesUtilsTest.java index e2cde34a..0fa1c52e 100644 --- a/common/tests/unit/src/android/net/util/LinkPropertiesUtilsTest.java +++ b/common/tests/unit/src/android/net/util/LinkPropertiesUtilsTest.java @@ -229,7 +229,7 @@ public final class LinkPropertiesUtilsTest { CompareOrUpdateResult<String, String> result = new CompareOrUpdateResult<String, String>(oldItems, emptyList, extractPrefix); assertCompareOrUpdateResult(result, - emptyList, strArray("hello123", "howareyou669", "goodbye5678"), emptyList); + emptyList, strArray("hello123", "howareyou669", "goodbye5678"), emptyList); // Empty -> items: everything added. result = new CompareOrUpdateResult<String, String>(emptyList, newItems, extractPrefix); @@ -244,5 +244,15 @@ public final class LinkPropertiesUtilsTest { result = new CompareOrUpdateResult<>(oldItems, newItems, extractPrefix); assertCompareOrUpdateResult(result, strArray("verywell"), strArray("howareyou669"), strArray("goodbye000")); + + // Null -> items: everything added. + result = new CompareOrUpdateResult<String, String>(null, newItems, extractPrefix); + assertCompareOrUpdateResult(result, + strArray("hello123", "goodbye000", "verywell"), emptyList, emptyList); + + // Items -> null: everything removed. + result = new CompareOrUpdateResult<String, String>(oldItems, null, extractPrefix); + assertCompareOrUpdateResult(result, + emptyList, strArray("hello123", "howareyou669", "goodbye5678"), emptyList); } }
\ No newline at end of file |