All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.t3t5u.common.util.CollectionUtils Maven / Gradle / Ivy

The newest version!
package com.github.t3t5u.common.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;

import org.apache.commons.lang3.ObjectUtils;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ContiguousSet;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;

public final class CollectionUtils {
	private CollectionUtils() {
	}

	public static boolean isEmpty(final Collection collection) {
		return (collection == null) || collection.isEmpty();
	}

	public static  E peekOrNull(final Stack stack) {
		return !isEmpty(stack) ? stack.peek() : null;
	}

	public static  E getIfInBounds(final List list, final int index) {
		return (list != null) && (index >= 0) && (index < list.size()) ? list.get(index) : null;
	}

	public static  E removeIfInBounds(final List list, final int index) {
		return (list != null) && (index >= 0) && (index < list.size()) ? list.remove(index) : null;
	}

	public static  boolean removeIfNotNull(final Collection collection, final E element) {
		return (collection != null) && (element != null) && collection.remove(element);
	}

	public static  boolean removeIfNotNull(final Collection collection, final Callable callable) {
		return (collection != null) && (callable != null) && removeIfNotNull(collection, ConcurrentUtils.call(callable));
	}

	public static  boolean removeAllIfNotNull(final Collection collection, final Collection c) {
		return (collection != null) && (c != null) && collection.removeAll(c);
	}

	public static  boolean addIfNotNull(final Collection collection, final E element) {
		return (collection != null) && (element != null) && collection.add(element);
	}

	public static  boolean addIfNotNull(final Collection collection, final Callable callable) {
		return (collection != null) && (callable != null) && addIfNotNull(collection, ConcurrentUtils.call(callable));
	}

	public static  boolean addAllIfNotNull(final Collection collection, final Collection c) {
		return (collection != null) && (c != null) && collection.addAll(c);
	}

	public static  V getIfNotNull(final Map map, final K key) {
		return (map != null) && (key != null) ? map.get(key) : null;
	}

	public static  V putIfNotNull(final Map map, final Map.Entry entry) {
		return (map != null) && (entry != null) ? putIfNotNull(map, entry.getKey(), entry.getValue()) : null;
	}

	public static  V putIfNotNull(final Map map, final K key, final V value) {
		return (map != null) && (key != null) && (value != null) ? map.put(key, value) : null;
	}

	public static  V putIfNotNull(final Map map, final K key, final Callable callable) {
		return (map != null) && (key != null) && (callable != null) ? putIfNotNull(map, key, ConcurrentUtils.call(callable)) : null;
	}

	public static  V putIfNotNull(final Map map, final K key, final Function function) {
		return (map != null) && (key != null) && (function != null) ? putIfNotNull(map, key, function.apply(key)) : null;
	}

	public static  V putIfContainsKey(final Map map, final Map.Entry entry) {
		return (map != null) && (entry != null) ? putIfContainsKey(map, entry.getKey(), entry.getValue()) : null;
	}

	public static  V putIfContainsKey(final Map map, final K key, final V value) {
		return (map != null) && (key != null) && map.containsKey(key) ? map.put(key, value) : null;
	}

	public static  V putIfContainsKey(final Map map, final K key, final Callable callable) {
		return (map != null) && (key != null) && (callable != null) && map.containsKey(key) ? map.put(key, ConcurrentUtils.call(callable)) : null;
	}

	public static  V putIfContainsKey(final Map map, final K key, final Function function) {
		return (map != null) && (key != null) && (function != null) && map.containsKey(key) ? map.put(key, function.apply(key)) : null;
	}

	public static  V putIfContainsKeyAndNotNull(final Map map, final Map.Entry entry) {
		return (map != null) && (entry != null) ? putIfContainsKeyAndNotNull(map, entry.getKey(), entry.getValue()) : null;
	}

	public static  V putIfContainsKeyAndNotNull(final Map map, final K key, final V value) {
		return (map != null) && (key != null) && (value != null) && map.containsKey(key) ? map.put(key, value) : null;
	}

	public static  V putIfContainsKeyAndNotNull(final Map map, final K key, final Callable callable) {
		return (map != null) && (key != null) && (callable != null) && map.containsKey(key) ? putIfNotNull(map, key, ConcurrentUtils.call(callable)) : null;
	}

	public static  V putIfContainsKeyAndNotNull(final Map map, final K key, final Function function) {
		return (map != null) && (key != null) && (function != null) && map.containsKey(key) ? putIfNotNull(map, key, function.apply(key)) : null;
	}

	public static  V putIfNotContainsKey(final Map map, final Map.Entry entry) {
		return (map != null) && (entry != null) ? putIfNotContainsKey(map, entry.getKey(), entry.getValue()) : null;
	}

	public static  V putIfNotContainsKey(final Map map, final K key, final V value) {
		return (map != null) && (key != null) && !map.containsKey(key) ? map.put(key, value) : null;
	}

	public static  V putIfNotContainsKey(final Map map, final K key, final Callable callable) {
		return (map != null) && (key != null) && (callable != null) && !map.containsKey(key) ? map.put(key, ConcurrentUtils.call(callable)) : null;
	}

	public static  V putIfNotContainsKey(final Map map, final K key, final Function function) {
		return (map != null) && (key != null) && (function != null) && !map.containsKey(key) ? map.put(key, function.apply(key)) : null;
	}

	public static  V putIfNotContainsKeyAndNotNull(final Map map, final Map.Entry entry) {
		return (map != null) && (entry != null) ? putIfNotContainsKeyAndNotNull(map, entry.getKey(), entry.getValue()) : null;
	}

	public static  V putIfNotContainsKeyAndNotNull(final Map map, final K key, final V value) {
		return (map != null) && (key != null) && (value != null) && !map.containsKey(key) ? map.put(key, value) : null;
	}

	public static  V putIfNotContainsKeyAndNotNull(final Map map, final K key, final Callable callable) {
		return (map != null) && (key != null) && (callable != null) && !map.containsKey(key) ? putIfNotNull(map, key, ConcurrentUtils.call(callable)) : null;
	}

	public static  V putIfNotContainsKeyAndNotNull(final Map map, final K key, final Function function) {
		return (map != null) && (key != null) && (function != null) && !map.containsKey(key) ? putIfNotNull(map, key, function.apply(key)) : null;
	}

	public static  V putIfAbsent(final ConcurrentMap map, final ConcurrentMap.Entry entry) {
		return (map != null) && (entry != null) ? putIfAbsent(map, entry.getKey(), entry.getValue()) : null;
	}

	public static  V putIfAbsent(final ConcurrentMap map, final K key, final V value) {
		return (map != null) && (key != null) ? map.putIfAbsent(key, value) : null;
	}

	public static  V putIfAbsent(final ConcurrentMap map, final K key, final Callable callable) {
		return (map != null) && (key != null) && (callable != null) ? map.putIfAbsent(key, ConcurrentUtils.call(callable)) : null;
	}

	public static  V putIfAbsent(final ConcurrentMap map, final K key, final Function function) {
		return (map != null) && (key != null) && (function != null) ? map.putIfAbsent(key, function.apply(key)) : null;
	}

	public static  V putIfAbsentAndNotNull(final ConcurrentMap map, final ConcurrentMap.Entry entry) {
		return (map != null) && (entry != null) ? putIfAbsentAndNotNull(map, entry.getKey(), entry.getValue()) : null;
	}

	public static  V putIfAbsentAndNotNull(final ConcurrentMap map, final K key, final V value) {
		return (map != null) && (key != null) && (value != null) ? map.putIfAbsent(key, value) : null;
	}

	public static  V putIfAbsentAndNotNull(final ConcurrentMap map, final K key, final Callable callable) {
		return (map != null) && (key != null) && (callable != null) ? putIfAbsentAndNotNull(map, key, ConcurrentUtils.call(callable)) : null;
	}

	public static  V putIfAbsentAndNotNull(final ConcurrentMap map, final K key, final Function function) {
		return (map != null) && (key != null) && (function != null) ? putIfAbsentAndNotNull(map, key, function.apply(key)) : null;
	}

	public static  V replace(final ConcurrentMap map, final ConcurrentMap.Entry entry) {
		return (map != null) && (entry != null) ? replace(map, entry.getKey(), entry.getValue()) : null;
	}

	public static  V replace(final ConcurrentMap map, final K key, final V value) {
		return (map != null) && (key != null) ? map.replace(key, value) : null;
	}

	public static  V replace(final ConcurrentMap map, final K key, final Callable callable) {
		return (map != null) && (key != null) && (callable != null) ? map.replace(key, ConcurrentUtils.call(callable)) : null;
	}

	public static  V replace(final ConcurrentMap map, final K key, final Function function) {
		return (map != null) && (key != null) && (function != null) ? map.replace(key, function.apply(key)) : null;
	}

	public static  V replaceNotNull(final ConcurrentMap map, final ConcurrentMap.Entry entry) {
		return (map != null) && (entry != null) ? replaceNotNull(map, entry.getKey(), entry.getValue()) : null;
	}

	public static  V replaceNotNull(final ConcurrentMap map, final K key, final V value) {
		return (map != null) && (key != null) && (value != null) ? map.replace(key, value) : null;
	}

	public static  V replaceNotNull(final ConcurrentMap map, final K key, final Callable callable) {
		return (map != null) && (key != null) && (callable != null) ? replaceNotNull(map, key, ConcurrentUtils.call(callable)) : null;
	}

	public static  V replaceNotNull(final ConcurrentMap map, final K key, final Function function) {
		return (map != null) && (key != null) && (function != null) ? replaceNotNull(map, key, function.apply(key)) : null;
	}

	public static  boolean replace(final ConcurrentMap map, final K key, final V oldValue, final V newValue) {
		return (map != null) && (key != null) && map.replace(key, oldValue, newValue);
	}

	public static  boolean replaceNotNull(final ConcurrentMap map, final K key, final V oldValue, final V newValue) {
		return (map != null) && (key != null) && (oldValue != null) && (newValue != null) && map.replace(key, oldValue, newValue);
	}

	public static  boolean putAllIfNotNull(final Map map, final Map m) {
		if ((map == null) || (m == null)) {
			return false;
		}
		map.putAll(m);
		return true;
	}

	public static > T min(final Collection collection) {
		return min(collection, true);
	}

	public static > T min(final Collection collection, final boolean nullGreater) {
		return !isEmpty(collection) ? Collections.min(collection, new Comparator() {
			@Override
			public int compare(final T lhs, final T rhs) {
				return ObjectUtils.compare(lhs, rhs, nullGreater);
			}
		}) : null;
	}

	public static > T max(final Collection collection) {
		return max(collection, false);
	}

	public static > T max(final Collection collection, final boolean nullGreater) {
		return !isEmpty(collection) ? Collections.max(collection, new Comparator() {
			@Override
			public int compare(final T lhs, final T rhs) {
				return ObjectUtils.compare(lhs, rhs, nullGreater);
			}
		}) : null;
	}

	public static  int count(final Iterable iterable, final Predicate predicate) {
		if ((iterable == null) || (predicate == null)) {
			return -1;
		}
		int count = 0;
		for (final T element : iterable) {
			if (!predicate.apply(element)) {
				continue;
			}
			count++;
		}
		return count;
	}

	public static  R fold(final Iterable iterable, final Function> function, final R init) {
		if ((iterable == null) || (function == null)) {
			return null;
		}
		R result = init;
		for (final T element : iterable) {
			result = function.apply(result).apply(element);
		}
		return result;
	}

	public static  R foldLeft(final T[] array, final Function> function, final R init) {
		if ((array == null) || (function == null)) {
			return null;
		}
		R result = init;
		for (int index = 0; index < array.length; index++) {
			result = function.apply(result).apply(index);
		}
		return result;
	}

	public static  R foldRight(final T[] array, final Function> function, final R init) {
		if ((array == null) || (function == null)) {
			return null;
		}
		R result = init;
		for (int index = array.length - 1; index >= 0; index--) {
			result = function.apply(result).apply(index);
		}
		return result;
	}

	public static  R foldLeft(final Collection collection, final Function> function, final R init) {
		if ((collection == null) || (function == null)) {
			return null;
		}
		R result = init;
		for (int index = 0; index < collection.size(); index++) {
			result = function.apply(result).apply(index);
		}
		return result;
	}

	public static  R foldRight(final Collection collection, final Function> function, final R init) {
		if ((collection == null) || (function == null)) {
			return null;
		}
		R result = init;
		for (int index = collection.size() - 1; index >= 0; index--) {
			result = function.apply(result).apply(index);
		}
		return result;
	}

	public static  void forEach(final T[] array, final Procedure procedure) {
		if ((array == null) || (procedure == null)) {
			return;
		}
		for (final T element : array) {
			procedure.proceed(element);
		}
	}

	public static  void forEach(final Iterable iterable, final Procedure procedure) {
		if ((iterable == null) || (procedure == null)) {
			return;
		}
		for (final T element : iterable) {
			procedure.proceed(element);
		}
	}

	public static void forEach(final int from, final int to, final int step, final Procedure procedure) {
		if ((from > to) || (procedure == null)) {
			return;
		}
		for (int count = from; count <= to; count += step) {
			procedure.proceed(count);
		}
	}

	public static > void forEach(final Range range, final DiscreteDomain domain, final Procedure procedure) {
		if ((range == null) || (domain == null) || (procedure == null)) {
			return;
		}
		for (final C c : ContiguousSet.create(range, domain)) {
			procedure.proceed(c);
		}
	}

	public static  List addForEach(final T[] array, final Function function) {
		if ((array == null) || (function == null)) {
			return null;
		}
		final List list = new ArrayList();
		for (final T element : array) {
			addIfNotNull(list, function.apply(element));
		}
		return list;
	}

	public static  List addForEach(final Iterable iterable, final Function function) {
		if ((iterable == null) || (function == null)) {
			return null;
		}
		final List list = new ArrayList();
		for (final T element : iterable) {
			addIfNotNull(list, function.apply(element));
		}
		return list;
	}

	public static  List addForEach(final int from, final int to, final int step, final Function function) {
		if ((from > to) || (function == null)) {
			return null;
		}
		final List list = new ArrayList();
		for (int count = from; count <= to; count += step) {
			addIfNotNull(list, function.apply(count));
		}
		return list;
	}

	public static > List addForEach(final Range range, final DiscreteDomain domain, final Function function) {
		if ((range == null) || (domain == null) || (function == null)) {
			return null;
		}
		final List list = new ArrayList();
		for (final C c : ContiguousSet.create(range, domain)) {
			addIfNotNull(list, function.apply(c));
		}
		return list;
	}

	public static  List addAllForEach(final T[] array, final Function> function) {
		if ((array == null) || (function == null)) {
			return null;
		}
		final List list = new ArrayList();
		for (final T element : array) {
			addAllIfNotNull(list, function.apply(element));
		}
		return list;
	}

	public static  List addAllForEach(final Iterable iterable, final Function> function) {
		if ((iterable == null) || (function == null)) {
			return null;
		}
		final List list = new ArrayList();
		for (final T element : iterable) {
			addAllIfNotNull(list, function.apply(element));
		}
		return list;
	}

	public static  List addAllForEach(final int from, final int to, final int step, final Function> function) {
		if ((from > to) || (function == null)) {
			return null;
		}
		final List list = new ArrayList();
		for (int count = from; count <= to; count += step) {
			addAllIfNotNull(list, function.apply(count));
		}
		return list;
	}

	public static > List addAllForEach(final Range range, final DiscreteDomain domain, final Function> function) {
		if ((range == null) || (domain == null) || (function == null)) {
			return null;
		}
		final List list = new ArrayList();
		for (final C c : ContiguousSet.create(range, domain)) {
			addAllIfNotNull(list, function.apply(c));
		}
		return list;
	}

	public static  Map putForEach(final Iterable iterable, final Function> function) {
		if ((iterable == null) || (function == null)) {
			return null;
		}
		final Map map = new LinkedHashMap();
		for (final T element : iterable) {
			putIfNotNull(map, function.apply(element));
		}
		return map;
	}

	public static  Map putForEachKey(final Iterable keys, final Function function) {
		if ((keys == null) || (function == null)) {
			return null;
		}
		final Map map = new LinkedHashMap();
		for (final K key : keys) {
			putIfNotNull(map, key, function.apply(key));
		}
		return map;
	}

	public static  Map putForEachValue(final Iterable values, final Function function) {
		if ((values == null) || (function == null)) {
			return null;
		}
		final Map map = new LinkedHashMap();
		for (final V value : values) {
			putIfNotNull(map, function.apply(value), value);
		}
		return map;
	}

	public static  Map putAllForEach(final Iterable iterable, final Function> function) {
		if ((iterable == null) || (function == null)) {
			return null;
		}
		final Map map = new LinkedHashMap();
		for (final T element : iterable) {
			putAllIfNotNull(map, function.apply(element));
		}
		return map;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy