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

de.danielbechler.util.Collections Maven / Gradle / Ivy

There is a newer version: 0.95
Show newest version
/*
 * Copyright 2012 Daniel Bechler
 *
 * 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 de.danielbechler.util;

import java.util.*;

/** @author Daniel Bechler */
public class Collections
{
	private Collections()
	{
	}

//	public static  Set setOf(final T... c)
//	{
//		return setOf(Arrays.asList(c));
//	}

	public static  Set setOf(final Collection c)
	{
		return new LinkedHashSet(c);
	}

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

	public static  boolean containsAny(final Iterable haystack, final Iterable needles)
	{
		if (haystack == null || needles == null)
		{
			return false;
		}
		for (final T straw : haystack)
		{
			for (final T needle : needles)
			{
				if (straw.equals(needle))
				{
					return true;
				}
			}
		}
		return false;
	}

//	public static  T get(final Iterable haystack, final T needle)
//	{
//		for (final T t : haystack)
//		{
//			if (t.equals(needle))
//			{
//				return t;
//			}
//		}
//		return null;
//	}
//
//	public static  int indexOf(final Iterable haystack, final T needle)
//	{
//		int index = 0;
//		for (final T item : haystack)
//		{
//			if (item.equals(needle))
//			{
//				return index;
//			}
//			index++;
//		}
//		return -1;
//	}

	public static  Collection filteredCopyOf(final Collection source,
															 final Collection filter)
	{
		final Collection copy;
		if (source != null)
		{
			copy = new LinkedList(source);
		}
		else
		{
			copy = new LinkedList();
		}
		if (filter != null)
		{
			copy.removeAll(new ArrayList(filter));
		}
		return copy;
	}

//	public static  Collection maskedCopyOf(final Collection source,
//														   final Collection mask)
//	{
//		final Collection copy = new LinkedList(source);
//		copy.retainAll(new ArrayList(mask));
//		return copy;
//	}

	public static  T firstElementOf(final Collection items)
	{
		if (items != null && !items.isEmpty())
		{
			return items.iterator().next();
		}
		return null;
	}

//	public static  T lastElementOf(final List items)
//	{
//		if (items != null && !items.isEmpty())
//		{
//			return items.get(items.size() - 1);
//		}
//		return null;
//	}
//
//	public static  T lastElementOf(final Collection items)
//	{
//		if (items != null && !items.isEmpty())
//		{
//			final Iterator iterator = items.iterator();
//			while (iterator.hasNext())
//			{
//				final T t = iterator.next();
//				if (!iterator.hasNext())
//				{
//					return t;
//				}
//			}
//		}
//		return null;
//	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy