All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.openforis.commons.collection.CollectionUtils Maven / Gradle / Ivy
/**
*
*/
package org.openforis.commons.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.lang3.ObjectUtils;
import org.openforis.commons.lang.DeepComparable;
import org.openforis.commons.lang.Objects;
/**
* @author M. Togna
* @author S. Ricci
*
*/
public abstract class CollectionUtils {
/**
*
* Returns an unmodifiable view of the specified list.
* This method makes use of the method unmodifiableList of java.util.Collections and returns an empty list if the provided list is null.
*
* @param list
* @return
* @see java.util.Collections.unmodifiableList
*/
public static List unmodifiableList(List extends T> list) {
if ( list == null ) {
return Collections.emptyList();
} else {
return Collections.unmodifiableList(list);
}
}
public static Set unmodifiableSet(Set extends T> set) {
if ( set == null ) {
return Collections.emptySet();
} else {
return Collections.unmodifiableSet(set);
}
}
public static Map unmodifiableMap(Map extends K, ? extends V> map) {
if ( map == null ) {
return Collections.emptyMap();
} else {
return Collections.unmodifiableMap(map);
}
}
public static Collection unmodifiableCollection(Collection extends T> collection) {
if ( collection == null ) {
return Collections.emptyList();
} else {
return Collections.unmodifiableCollection(collection);
}
}
/**
* Shifts the item to the specified index.
*/
public static void shiftItem(List list, T item, int toIndex) {
int oldIndex = list.indexOf(item);
if ( oldIndex < 0 ) {
throw new IllegalArgumentException("Item not found");
}
if ( toIndex >= 0 && toIndex < list.size() ) {
list.remove(oldIndex);
list.add(toIndex, item);
} else {
throw new IndexOutOfBoundsException("Index out of bounds: " + toIndex + " (list size = " + list.size() + ")");
}
}
public static boolean isNotEmpty(Collection> collection) {
return collection != null && !collection.isEmpty();
}
public static > void filter(C collection, Predicate predicate) {
if (collection != null && predicate != null) {
Iterator it = collection.iterator();
while(it.hasNext()) {
T item = it.next();
if (!predicate.evaluate(item)) {
it.remove();
}
}
}
}
public static > List filterIntoList(C collection, Predicate predicate) {
List result = new ArrayList();
if (collection != null && predicate != null) {
for (T item : collection) {
if (predicate.evaluate(item)) {
result.add(item);
}
}
}
return result;
}
public static T find (Collection items, Predicate predicate) {
for (T item : items) {
if (predicate.evaluate(item)) {
return item;
}
}
return null;
}
public static T findItem(Collection items, Object key) {
return findItem(items, key, "id");
}
public static T findItem(Collection items, Object key, String keyPropertyName) {
for (T item : items) {
Object keyValue = Objects.getPropertyValue(item, keyPropertyName);
if (key.equals(keyValue)) {
return item;
}
}
return null;
}
public static Collection addIgnoreNull(Collection items, T item) {
if (item != null ) {
items.add(item);
}
return items;
}
public static List project(Collection items, String propertyName) {
List result = new ArrayList(items.size());
for (T item : items) {
I keyValue = Objects.getPropertyValue(item, propertyName);
result.add(keyValue);
}
return result;
}
public static boolean deepEquals(Collection coll1, Collection coll2) {
return deepEquals(coll1, coll2, false);
}
public static boolean deepEquals(Collection coll1, Collection coll2, boolean ignoreId) {
if (coll1 == coll2)
return true;
Iterator e1 = coll1.iterator();
Iterator e2 = coll2.iterator();
while (e1.hasNext() && e2.hasNext()) {
DeepComparable o1 = e1.next();
DeepComparable o2 = e2.next();
if (! Objects.deepEquals(o1, o2, ignoreId))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
/**
* Creates a clone of the specified list (NOT a DEEP CLONE of the objects inside the list)
*
* @param list
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List clone(List list) {
if (list == null) {
return null;
}
if (list instanceof ArrayList) {
return (List) ((ArrayList) list).clone();
} else {
List result = new ArrayList(list.size());
for (T item : list) {
result.add(item);
}
return result;
}
}
public static void cloneValuesInto(Map fromMap, Map intoMap) {
for (Entry entry : fromMap.entrySet()) {
intoMap.put(ObjectUtils.cloneIfPossible(entry.getKey()), ObjectUtils.cloneIfPossible(entry.getValue()));
}
}
public static List copyAndFillWithNulls(List list, int newSize) {
return copyAndFillWith(list, newSize, null);
}
public static List copyAndFillWith(List list, int newSize, T fillValue) {
if (list.size() > newSize) {
throw new IllegalArgumentException(String.format("Original collection size (%d) must not exceed the new collection size: %d", list.size(), newSize));
}
ArrayList result = new ArrayList(newSize);
result.addAll(list);
result.addAll(Collections.nCopies(newSize - list.size(), fillValue));
return result;
}
public static Set intersect(Collection listA, Collection listB) {
Set result = new LinkedHashSet();
for (T item : listA) {
if (listB.contains(item)) {
result.add(item);
}
}
return result;
}
public static List indexOfItems(List list, Collection items) {
List result = new ArrayList();
for (T item : items) {
int index = list.indexOf(item);
if (index >= 0) {
result.add(index);
}
}
return result;
}
public static List sublistByIndexes(List source, List indexes) {
List result = new ArrayList();
for (Integer index : indexes) {
result.add(source.get(index));
}
return result;
}
public static List map(Collection collection, Transformer transformer) {
List result = new ArrayList();
if (collection == null || transformer == null) return result;
for (T item : collection) {
T transformedItem = transformer.transform(item);
result.add(transformedItem);
}
return result;
}
}