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

com.peterphi.std.util.ListUtility Maven / Gradle / Ivy

The newest version!
package com.peterphi.std.util;

import com.peterphi.std.types.HybridIterator;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ListUtility
{
	public static  List list(final T[] items)
	{
		return new ArrayList(Arrays.asList(items));
	}


	public static  List list(final List items)
	{
		return new ArrayList(items);
	}


	/**
	 * Converts an Iterable into a List
	 *
	 * @param iterable
	 *
	 * @return
	 */
	public static  List list(Iterable iterable)
	{
		List list = new ArrayList();

		for (T item : iterable)
		{
			list.add(item);
		}
		return list;
	}


	public static  T[] array(final Iterable itbl, final T[] array)
	{
		Iterator it = itbl.iterator();

		for (int i = 0; i < array.length; i++)
			array[i] = it.next();

		return array;
	}


	/**
	 * Return, at most, the last n items from the source list
	 *
	 * @param 
	 * 		the list type
	 * @param src
	 * 		the source list
	 * @param count
	 * 		the maximum number of items
	 *
	 * @return the last n items from the src list; if the list is not of size n, the original list is copied and returned
	 */
	public static  List last(final List src, int count)
	{
		if (count >= src.size())
		{
			return new ArrayList(src);
		}
		else
		{
			final List dest = new ArrayList(count);

			final int size = src.size();
			for (int i = size - count; i < size; i++)
			{
				dest.add(src.get(i));
			}

			return dest;
		}
	}


	/**
	 * Returns the first element of a list (or null if the list is empty)
	 *
	 * @param list
	 *
	 * @return
	 */
	public static  T head(List list)
	{
		if (list.isEmpty())
			return null;
		else
			return list.get(0);
	}


	/**
	 * Returns a sublist containing all the items in the list after the first
	 *
	 * @param list
	 *
	 * @return
	 */
	public static  List tail(List list)
	{
		if (list.isEmpty())
			return Collections.emptyList();
		else
			return list.subList(1, list.size());
	}


	/**
	 * Reverses an array
	 *
	 * @param 
	 * @param src
	 * @param dest
	 * @param start
	 * @param length
	 *
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static  T[] flip(final T[] src, T[] dest, final int start, final int length)
	{
		if (dest == null || dest.length < length)
			dest = (T[]) Array.newInstance(src.getClass().getComponentType(), length);

		int srcIndex = start + length;
		for (int i = 0; i < length; i++)
		{
			dest[i] = src[--srcIndex];
		}

		return dest;
	}


	/**
	 * Reverses an array
	 *
	 * @param 
	 * @param src
	 *
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static  T[] flip(final T[] src)
	{
		final int length = src.length;

		return flip(src, (T[]) Array.newInstance(src.getClass().getComponentType(), length), 0, length);
	}


	/**
	 * Reverses a list
	 *
	 * @param 
	 * @param src
	 * @param dest
	 * @param start
	 * @param length
	 *
	 * @return
	 */
	public static  List flip(final List src, List dest, final int start, final int length)
	{
		if (dest == null)
			dest = new ArrayList();
		else if (!dest.isEmpty())
			dest.clear();

		final int last = start + length - 1;
		for (int i = last; i >= start; i--)
		{
			dest.add(src.get(i));
		}

		return dest;
	}


	/**
	 * Reverses a list
	 *
	 * @param 
	 * @param src
	 *
	 * @return
	 */
	public static  List flip(final List src)
	{
		final int length = src.size();
		final List dest = new ArrayList(length);

		return flip(src, dest, 0, length);
	}


	/**
	 * Reverses an integer array
	 *
	 * @param src
	 * @param dest
	 * @param start
	 * @param length
	 *
	 * @return
	 */
	public static int[] flip(int[] src, int[] dest, final int start, final int length)
	{
		if (dest == null || dest.length < length)
			dest = new int[length];

		int srcIndex = start + length;
		for (int i = 0; i < length; i++)
		{
			dest[i] = src[--srcIndex];
		}

		return dest;
	}


	/**
	 * Reverses a character array
	 *
	 * @param src
	 * @param dest
	 * @param start
	 * @param length
	 *
	 * @return
	 */
	public static char[] flip(char[] src, char[] dest, final int start, final int length)
	{
		if (dest == null || dest.length < length)
			dest = new char[length];

		int srcIndex = start + length;
		for (int i = 0; i < length; i++)
		{
			dest[i] = src[--srcIndex];
		}

		return dest;
	}


	public static  HybridIterator iterate(final Iterator items)
	{
		return new HybridIterator(items);
	}


	public static  HybridIterator iterate(final Enumeration items)
	{
		return new HybridIterator(items);
	}


	public static  int size(final T[] items)
	{
		return items.length;
	}


	public static  int size(final Collection items)
	{
		return items.size();
	}


	public static  int length(final T[] items)
	{
		return size(items);
	}


	public static  int length(final Collection items)
	{
		return size(items);
	}


	public static boolean empty(final Map m)
	{
		return (m == null || m.size() == 0);
	}


	public static  boolean empty(final Collection c)
	{
		return (c == null || c.size() == 0);
	}


	public static  boolean contains(final Collection c, final T o)
	{
		return (c != null && c.contains(o));
	}


	public static boolean contains(final String[] haystack, final String needle)
	{
		if (haystack != null && haystack.length != 0)
		{
			for (String straw : haystack)
			{
				if (needle.equals(straw))
				{
					return true;
				}
			}

			return false;
		}
		else
		{
			return false;
		}
	}


	/**
	 * Concatenates a number of Collections into a single List
	 *
	 * @param 
	 * @param lists
	 *
	 * @return
	 */
	public static  List concat(final Collection... lists)
	{
		ArrayList al = new ArrayList();

		for (Collection list : lists)
			if (list != null)
				al.addAll(list);

		return al;
	}


	/**
	 * Concatenates a number of Collections into a single Set
	 *
	 * @param 
	 * @param lists
	 *
	 * @return
	 */
	public static  Set union(final Collection... lists)
	{
		Set s = new HashSet();

		for (Collection list : lists)
			if (list != null)
				s.addAll(list);

		return s;
	}


	public static  List join(Iterable... items)
	{
		List list = new ArrayList();

		for (Iterable item : items)
			if (item != null)
				for (T o : item)
					list.add(o);

		return list;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy