package org.mtransit.android.commons;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

public final class CollectionUtils implements MTLog.Loggable {

	private static final String LOG_TAG = CollectionUtils.class.getSimpleName();

	@NonNull
	@Override
	public String getLogTag() {
		return LOG_TAG;
	}

	private CollectionUtils() {
	}

	public static int getSize(final @Nullable Collection<?> collection) {
		if (collection == null) {
			return 0;
		}
		return collection.size();
	}

	public static int getSize(final @Nullable Map<?, ?> map) {
		if (map == null) {
			return 0;
		}
		return map.size();
	}

	public static <T> void sort(@Nullable java.util.List<T> list, @NonNull Comparator<? super T> comparator) {
		if (list == null || list.size() < 2) {
			return; // nothing to sort if null / empty / 1 element
		}
		Collections.sort(list, comparator);
	}

	@Nullable
	public static <T> T getOrNull(@Nullable List<T> list, int i) {
		try {
			if (list == null) {
				return null;
			}
			if (i < list.size()) {
				return list.get(i);
			} else {
				return null;
			}
		} catch (Exception e) {
			MTLog.d(LOG_TAG, e, "Error while reading object '%d' from '%s'!", i, list);
			return null;
		}
	}

	@NonNull
	public static <T> List<T> removeDuplicates(@NonNull List<T> list) {
		return new ArrayList<>(new LinkedHashSet<>(list));
	}
}

@@ -104,16 +104,53 @@
 ullable;
+%0Aimport androidx.core.util.Predicate;
 %0A%0Aimport
@@ -259,16 +259,43 @@
 arator;%0A
+import java.util.Iterator;%0A
 import j
@@ -364,16 +364,55 @@
 l.Map;%0A%0A
+@SuppressWarnings(%22UnusedReturnValue%22)%0A
 public f
@@ -1650,11 +1650,300 @@
 st));%0A%09%7D
+%0A%0A%09public static %3CT%3E int removeIf(@NonNull List%3CT%3E list, @NonNull Predicate%3C? super T%3E filter) %7B%0A%09%09int removed = 0;%0A%09%09final Iterator%3CT%3E each = list.listIterator();%0A%09%09while (each.hasNext()) %7B%0A%09%09%09if (filter.test(each.next())) %7B%0A%09%09%09%09each.remove();%0A%09%09%09%09removed++;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return removed;%0A%09%7D
 %0A%7D%0A

Add utility method to remove item from list that match predicte.