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.
com.zipwhip.util.CollectionUtil Maven / Gradle / Ivy
package com.zipwhip.util;
import java.lang.reflect.Array;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: Michael
* Date: Nov 10, 2010
* Time: 10:59:49 PM
*
* Some helpful things with collections
*/
public class CollectionUtil {
public static void deepJoinArrays(Map result, Map addition) {
for (String key : addition.keySet()) {
Object secondItem = addition.get(key);
if (result.containsKey(key)) {
Object item = result.get(key);
if (item instanceof List) {
// we have a collision
List existingList = (List) item;
if (secondItem instanceof List) {
for (Object value : (List) secondItem) {
existingList.add(value);
}
} else {
throw new RuntimeException("I cant deal with this scenario");
}
} else {
throw new RuntimeException("I cant deal with this scenario");
}
} else {
// we dont have a collision
result.put(key, secondItem);
}
}
}
/**
* Try to find something in param1, if its not found, fall back to param2, then otherwise just go with defaultValue.
*
* @param params
* @return
*/
public static String getString(Map params, String... keys) {
return getString(params, Arrays.asList(keys));
}
public static String getString(Map params, Collection keys) {
for (String key : keys) {
String result = getString(params, key);
if (!StringUtil.isNullOrEmpty(result)) {
return result;
}
}
return null;
}
public static Number getNumber(Map params, String key) {
Object param = getParam(params, key);
if (param instanceof Number) {
return ((Number) param);
}
if (param instanceof String) {
NumberFormat defForm = NumberFormat.getInstance();
try {
if (!StringUtil.isNullOrEmpty(param.toString())) { //empty string causes known exception
return defForm.parse((String) param);
}
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
return null;
}
public static boolean getBoolean(Map params, String key, boolean defaultValue) {
Boolean param = getBoolean(params, key);
if (param == null) {
return defaultValue;
}
return param;
}
public static Boolean getBoolean(Map params, String key) {
Object param = getParam(params, key);
if (param == null) {
return null;
}
if (param instanceof Boolean) {
return (Boolean) param;
} else if (param instanceof String) {
return (Boolean.parseBoolean((String) param));
}
return null;
}
public static int getInteger(Map params, String key, int defaultValue) {
Integer result = getInteger(params, key);
if (result == null) {
return defaultValue;
}
return result;
}
public static Integer getInteger(Map params, String key) {
Number param = getNumber(params, key);
if (param == null) {
return null;
}
int result = param.intValue();
if (result == -1) {
return null;
}
return result;
}
public static long getLong(Map params, String key, long defaultValue) {
Long result = getLong(params, key);
if (result == null) {
return defaultValue;
}
return result;
}
public static Long getLong(Map params, String key) {
Number param = getNumber(params, key);
if (param == null) {
return null;
}
long result = param.longValue();
if (result == -1L) {
return null;
}
return result;
}
public static Date getDate(Map params, String key) {
Long param = getLong(params, key);
if (param == null) {
return null;
}
if (param == -1L) {
return null;
}
return new Date(param);
}
public static Date getDate(Map params, String key, Date defaultDate) {
Long param = getLong(params, key);
if (param == null) {
return defaultDate;
}
if (param < 0) {
return defaultDate;
}
return new Date(param);
}
public static String getString(Map params, String key) {
Object param = getParam(params, key);
if (param == null) {
return null;
}
if (param instanceof String) {
return (String) param;
}
// if (param instanceof org.codehaus.groovy.grails.web.json.JSONObject){
// }
// if (param instanceof Collection) {
// Collection collection = ((Collection) param);
//
// return delimitedParser.deliminate(collection);
// }
return String.valueOf(param);
// return null;
}
public static List getList(Map params, String key, List defaultValue) {
List result = getList(params, key);
if ((result == null) || result.isEmpty()) {
return defaultValue;
}
return result;
}
public static List getList(Map params, String... keys) {
for (String key : keys) {
List result = getList(params, key);
if (!isNullOrEmpty(result)) {
return result;
}
}
return null;
}
public static List getList(Map params, String key) {
Object param = getParam(params, key);
if (param == null) {
return null;
}
if (param instanceof Collection) {
Collection collection = (Collection) param;
List result = new Vector();
for (Object part : collection) {
result.add(String.valueOf(part));
}
return result;
}
List result = new ArrayList();
if (param.getClass().isArray()) {
List items = Arrays.asList(toObjectArray(param));
for (Object item : items) {
result.add(String.valueOf(item));
}
} else {
result.add(String.valueOf(param));
}
return result;
}
public static Object getParam(Map params, String key) {
Object object = get(params, key);
// if (object == org.codehaus.groovy.grails.web.json.JSONObject.NULL) {
// return null;
// }
return object;
}
public static Object getParam(Map params, String key, Object defaultValue) {
Object result = getParam(params, key);
if (result == null) {
return defaultValue;
}
return result;
}
public static Map getMap(Map params, String key) {
Object result = getParam(params, key);
if (result instanceof Map) {
return (Map) result;
}
return null;
}
public static class DiffResult {
public List current;
public Collection previous;
public List additions;
public List subtractions;
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("current: ").append(current).append("\n");
sb.append("previous: ").append(previous).append("\n");
sb.append("additions: ").append(additions).append("\n");
sb.append("subtractions: ").append(subtractions).append("\n");
return sb.toString();
}
}
public static , TItem> TColl add(Class clazz, TColl list, TItem item) {
if (list == null) {
try {
list = clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
list.add(item);
return list;
}
public static Collection add(Collection list, T item) {
if (list == null) {
list = new ArrayList();
}
list.add(item);
return list;
}
/**
* Will not create a new list.
*
* @param
* @param result
* @param other
* @return
*/
public static > TV addAllEfficient(TV result, TV other) {
if (isNullOrEmpty(other)) {
return result;
}
if (isNullOrEmpty(result)) {
return other;
}
result.addAll(other);
return result;
}
public static List remove(List callbackList, T item) {
if (CollectionUtil.isNullOrEmpty(callbackList)) {
return callbackList;
}
callbackList.remove(item);
return callbackList;
}
public static Object get(Object[] objects, int index) {
if (isNullOrEmpty(objects)) {
return null;
}
if (index > objects.length) {
return null;
}
return objects[index];
}
public static DiffResult diff(Collection original, List current) {
DiffResult result = new DiffResult();
result.previous = original;
result.current = current;
if (isNullOrEmpty(original)) {
result.additions = current;
result.subtractions = null;
return result;
}
if (isNullOrEmpty(current)) {
result.additions = null;
result.subtractions = current;
return result;
}
Map map = new HashMap();
for (T t : original) {
map.put(t, false);
}
for (T t : current) {
if (!map.containsKey(t)) {
// addition
result.additions = (List) add(result.additions, t);
} else {
// it already exists
map.put(t, true);
}
}
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue().equals(false)) {
// it was never updated
result.subtractions = (List) add(result.subtractions, entry.getKey());
}
}
return result;
}
public static boolean containsAny(Map params, String... param) {
return containsAny(params, Arrays.asList(param));
}
public static boolean containsAll(Map params, String... param) {
return containsAll(params, Arrays.asList(param));
}
public static boolean containsAny(Map params, Collection param) {
if (!CollectionUtil.isNullOrEmpty(params)) {
for (String string : param) {
Object object = getParam(params, string);
if (object != null) { // because params.containsKey() on a Json map is completely broken.
return true;
}
}
}
return false;
}
public static boolean containsAll(Map params, Collection param) {
if (!CollectionUtil.isNullOrEmpty(params)) {
for (String string : param) {
Object object = getParam(params, string);
if (object != null) { // because params.containsKey() on a Json map is completely broken.
return true;
}
}
}
return true;
}
public static boolean containsAny(Map params, String param) {
if (!CollectionUtil.isNullOrEmpty(params)) {
Object object = getParam(params, param);
if (object != null) { // because params.containsKey() on a Json map is completely broken.
return true;
}
}
return false;
}
/**
* Null safe wrapper around Arrays.asList...
*
* @param items
* @param
* @return
*/
public static List asList(T... items) {
if (isNullOrEmpty(items)) {
return null;
}
return Arrays.asList(items);
}
public static boolean isNullOrEmpty(Collection collection) {
return ((collection == null) || collection.isEmpty());
}
public static boolean isNullOrEmpty(Map map) {
return ((map == null) || map.isEmpty());
}
public static boolean exists(Collection collection) {
return !isNullOrEmpty(collection);
}
public static boolean exists(Map map) {
return !isNullOrEmpty(map);
}
public static T first(List collection) {
return get(collection, 0);
}
public static T last(List collection) {
if (isNullOrEmpty(collection)) {
return null;
}
return get(collection, collection.size() - 1);
}
public static V get(Map collection, K param) {
if (collection == null) {
return null;
}
if (param == null) {
return null;
}
try {
return collection.get(param);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static T find(List collection, T needle) {
if (isNullOrEmpty(collection)) {
return null;
}
if (needle == null) {
return null;
}
if (collection.size() == 0) {
return null;
}
for (T item : collection) {
if (item.equals(needle)) {
return item;
}
}
return null;
}
public static T get(List collection, int index) {
if (isNullOrEmpty(collection)) {
return null;
}
if (index < 0) {
return null;
}
if ((collection.size() - 1) < index) {
return null;
}
return collection.get(index);
}
public static boolean isNullOrEmpty(T[] parts) {
if ((parts == null) || (parts.length <= 0)) {
return true;
}
return false;
}
public static Map add(Map result, K key, V value) {
if (result == null) {
result = new HashMap();
}
result.put(key, value);
return result;
}
public static List addAll(List result, List toAdd) {
if (result == null) {
if (toAdd == null) {
return null;
}
return toAdd;
}
if (toAdd == null) {
return result;
}
result.addAll(toAdd);
return result;
}
public static boolean isNullOrEmpty(byte[] sourceImageData) {
return ((sourceImageData == null) || (sourceImageData.length == 0));
}
public static void remove(Collection items, T item) {
if (isNullOrEmpty(items)) {
return;
}
items.remove(item);
}
public static Map asMap(TKey key, TValue value) {
Map result = new HashMap();
result.put(key, value);
return result;
}
/**
* Create a Set from a lst of items of the same type, T.
* The implementation is {@code HashSet} so iterating will become slow as you add to it.
*
* @param items Zero or more items of type T from which to create the Set
* @param The type of Set to produce.
* @return A Set of type T containing the arguments.
*/
public static Set asSet(T... items) {
Set set = new HashSet();
set.addAll(Arrays.asList(items));
return set;
}
public static List subList(List arguments, int index) {
if (index == 0) {
return arguments;
}
if (isNullOrEmpty(arguments)) {
return null;
}
int max = arguments.size() - 1;
if (index >= max) {
return null;
}
return arguments.subList(index, max);
}
public static Set getSet(Map params, String key) {
Object param = getParam(params, key);
if (param == null) {
return null;
}
if (param instanceof Collection) {
Collection collection = (Collection) param;
Set result = new HashSet();
for (Object part : collection) {
result.add(String.valueOf(part));
}
return result;
}
Set result = new HashSet();
if (param.getClass().isArray()) {
List items = Arrays.asList(toObjectArray(param));
for (Object item : items) {
result.add(String.valueOf(item));
}
} else {
result.add(String.valueOf(param));
}
return result;
}
/**
* Convert the given array (which may be a primitive array) to an
* object array (if necessary of primitive wrapper objects).
* A null
source value will be converted to an
* empty Object array.
*
* @param source the (potentially primitive) array
* @return the corresponding object array (never null
)
* @throws IllegalArgumentException if the parameter is not an array
*/
public static Object[] toObjectArray(Object source) {
if (source instanceof Object[]) {
return (Object[]) source;
}
if (source == null) {
return new Object[0];
}
if (!source.getClass().isArray()) {
throw new IllegalArgumentException("Source is not an array: " + source);
}
int length = Array.getLength(source);
if (length == 0) {
return new Object[0];
}
Class wrapperType = Array.get(source, 0).getClass();
Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
for (int i = 0; i < length; i++) {
newArray[i] = Array.get(source, i);
}
return newArray;
}
/**
* Returns the size of the of a Collection or or -1 if c is null.
*
* For example:
*
* {@code If (CollectionUtil.size(c) <= 0)...}
*
* @param c The collection to test the size of.
* @return The size of c or or -1 if c is null
*/
public static int size(Collection c) {
return c == null ? -1 : c.size();
}
}