Merge "Skip null values in Utils.safeForeach" into pi-dev

This commit is contained in:
TreeHugger Robot
2018-06-16 03:16:06 +00:00
committed by Android (Google) Code Review
2 changed files with 5 additions and 6 deletions

View File

@@ -113,10 +113,6 @@ public class ZenModeControllerImpl extends CurrentUserTracker implements ZenMode
@Override @Override
public void addCallback(Callback callback) { public void addCallback(Callback callback) {
if (callback == null) {
Slog.e(TAG, "Attempted to add a null callback.");
return;
}
mCallbacks.add(callback); mCallbacks.add(callback);
} }

View File

@@ -26,11 +26,14 @@ public class Utils {
/** /**
* Allows lambda iteration over a list. It is done in reverse order so it is safe * Allows lambda iteration over a list. It is done in reverse order so it is safe
* to add or remove items during the iteration. * to add or remove items during the iteration. Skips over null items.
*/ */
public static <T> void safeForeach(List<T> list, Consumer<T> c) { public static <T> void safeForeach(List<T> list, Consumer<T> c) {
for (int i = list.size() - 1; i >= 0; i--) { for (int i = list.size() - 1; i >= 0; i--) {
c.accept(list.get(i)); T item = list.get(i);
if (item != null) {
c.accept(item);
}
} }
} }