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

org.usergrid.utils.ListUtils Maven / Gradle / Ivy

There is a newer version: 0.0.27.1
Show newest version
/*******************************************************************************
 * Copyright 2012 Apigee Corporation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.usergrid.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.math.NumberUtils;

public class ListUtils extends org.apache.commons.collections.ListUtils {

	public static  A first(List list) {
		if (list == null) {
			return null;
		}
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	public static  A last(List list) {
		if (list == null) {
			return null;
		}
		if (list.size() == 0) {
			return null;
		}
		return list.get(list.size() - 1);
	}

	public static  Integer firstInteger(List list) {
		A a = first(list);
		if (a == null) {
			return null;
		}
		if (a instanceof Integer) {
			return (Integer) a;
		}
		try {
			return NumberUtils.toInt((String) a);
		} catch (Exception e) {
		}
		return null;
	}

	public static  Long firstLong(List list) {
		A a = first(list);
		if (a == null) {
			return null;
		}
		if (a instanceof Long) {
			return (Long) a;
		}
		try {
			return NumberUtils.toLong((String) a);
		} catch (Exception e) {
		}
		return null;
	}

	public static  Boolean firstBoolean(List list) {
		A a = first(list);
		if (a == null) {
			return null;
		}
		if (a instanceof Boolean) {
			return (Boolean) a;
		}
		try {
			return Boolean.parseBoolean((String) a);
		} catch (Exception e) {
		}
		return null;
	}

	public static  UUID firstUuid(List list) {
		A i = first(list);
		if (i == null) {
			return null;
		}
		if (i instanceof UUID) {
			return (UUID) i;
		}
		try {
			return UUIDUtils.tryGetUUID((String) i);
		} catch (Exception e) {
		}
		return null;
	}

	public static boolean isEmpty(List list) {
		return (list == null) || (list.size() == 0);
	}

	public static  List dequeueCopy(List list) {
		if (!isEmpty(list)) {
			list = list.subList(1, list.size());
		}
		return list;
	}

	public static  List queueCopy(List list, T item) {
		if (!isEmpty(list)) {
			list = new ArrayList(list);
		} else {
			list = new ArrayList();
		}
		list.add(item);
		return list;
	}

	public static  List initCopy(List list) {
		if (!isEmpty(list)) {
			list = new ArrayList(list);
		} else {
			list = new ArrayList();
		}
		return list;
	}

	public static  T dequeue(List list) {
		if (!isEmpty(list)) {
			return list.remove(0);
		}
		return null;
	}

	public static  List queue(List list, T item) {
		if (list == null) {
			list = new ArrayList();
		}
		list.add(item);
		return list;
	}

	public static  List requeue(List list, T item) {
		if (list == null) {
			list = new ArrayList();
		}
		list.add(0, item);
		return list;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List flatten(Collection l) {
		boolean hasCollection = false;
		for (Object o : l) {
			if (o instanceof Collection) {
				hasCollection = true;
				break;
			}
		}
		if (!hasCollection && (l instanceof List)) {
			return (List) l;
		}
		List newList = new ArrayList();
		for (Object o : l) {
			if (o instanceof List) {
				newList.addAll(flatten((List) o));
			} else {
				newList.add(o);
			}
		}
		return newList;
	}

	public static boolean anyNull(List l) {
		for (Object o : l) {
			if (o == null) {
				return true;
			}
		}
		return false;
	}

	public static boolean anyNull(Object... objects) {
		for (Object o : objects) {
			if (o == null) {
				return true;
			}
		}
		return false;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy