de.danielbechler.util.Collections Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-object-diff Show documentation
Show all versions of java-object-diff Show documentation
Framework to detect and handle differences between Java objects
/*
* 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 extends T> haystack, final T needle)
// {
// int index = 0;
// for (final T item : haystack)
// {
// if (item.equals(needle))
// {
// return index;
// }
// index++;
// }
// return -1;
// }
public static Collection extends T> filteredCopyOf(final Collection extends T> source,
final Collection extends T> 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 extends T> maskedCopyOf(final Collection extends T> source,
// final Collection extends T> mask)
// {
// final Collection copy = new LinkedList(source);
// copy.retainAll(new ArrayList(mask));
// return copy;
// }
public static T firstElementOf(final Collection extends T> items)
{
if (items != null && !items.isEmpty())
{
return items.iterator().next();
}
return null;
}
// public static T lastElementOf(final List extends T> items)
// {
// if (items != null && !items.isEmpty())
// {
// return items.get(items.size() - 1);
// }
// return null;
// }
//
// public static T lastElementOf(final Collection extends T> items)
// {
// if (items != null && !items.isEmpty())
// {
// final Iterator extends T> iterator = items.iterator();
// while (iterator.hasNext())
// {
// final T t = iterator.next();
// if (!iterator.hasNext())
// {
// return t;
// }
// }
// }
// return null;
// }
}