package rapanui.core;

import java.util.Collection;
import java.util.List;
import java.util.function.IntFunction;

class Patterns {
	public static <T> boolean addToSet(Collection<T> set, T item) {
		if (!set.contains(item)) {
			set.add(item);
			return true;
		}
		return false;
	}

	public static <T> boolean removeWithCheck(Collection<T> collection, T item) {
		if (collection.contains(item)) {
			collection.remove(item);
			return true;
		}
		return false;
	}

	public static <T> boolean removeWithCheck(List<T> collection, int index) {
		if (0 <= index && index < collection.size()) {
			collection.remove(index);
			return true;
		}
		return false;
	}

	public static <T> T[] listToArray(Collection<T> collection, IntFunction<T[]> constructor) {
		return collection.toArray(constructor.apply(collection.size()));
	}
}

@@ -106,16 +106,54 @@
 unction;
+%0Aimport java.util.function.BiConsumer;
 %0A%0Aclass 
@@ -853,11 +853,369 @@
 ()));%0A%09%7D
+%0A%0A%09public static %3CTListener, TArgument%3E void notifyListeners(%0A%09%09%09Iterable%3CTListener%3E listeners,%0A%09%09%09BiConsumer%3CTListener, TArgument%3E listeningMethod,%0A%09%09%09TArgument argument) %7B%0A%0A%09%09assert listeners != null;%0A%09%09assert listeningMethod != null;%0A%0A%09%09for (TListener listener : listeners) %7B%0A%09%09%09if (listener != null)%0A%09%09%09%09listeningMethod.accept(listener, argument);%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

add pattern for notifying listeners