/*
* XAdES4j - A Java library for generation and verification of XAdES signatures.
* Copyright (C) 2010 Luis Goncalves.
*
* XAdES4j is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or any later version.
*
* XAdES4j is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with XAdES4j. If not, see .
*/
package xades4j.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Provides some utility methods over collections.
* @author Luís
*/
public class CollectionUtils
{
/**
* Get a collection with the specified initial size if the given collection is {@code null}.
* The returned collection, if new, will support the {@code remove} operation.
* @param c the collection to be tested
* @param size the initial size of the returned collection, if new
* @return a new collection or {@code c} if it is not {@code null}
*/
public static Collection newIfNull(Collection c, int size)
{
if (null == c)
c = new ArrayList(size);
return c;
}
/**
* Same as {@code newIfNull} but for maps.
* @param m the map to be tested
* @param size the initial size of the returned map, if new
* @return a new map or {@code m} if it is not {@code null}
*/
public static Map newIfNull(
Map m, int size)
{
if (null == m)
m = new HashMap(size);
return m;
}
/**
* Get a set with the specified initial size if the given set is {@code null}.
* @param c the collection to be tested
* @param size the initial size of the returned collection, if new
* @return a new collection or {@code c} if it is not {@code null}
*/
// public static Set newIfNull(Set s, int size)
// {
// if (null == s)
// s = new HashSet(size);
// return s;
// }
/**
* Get an empty collection if the given collection is {@code null}.
* The returned collection, if new, is immutable.
* @param c the collection to be tested
* @return a new empty collection or {@code c} if it is not null
*/
public static Collection emptyIfNull(Collection c)
{
if (null == c)
c = Collections.emptyList();
return c;
}
public static Map emptyIfNull(Map m)
{
if (null == m)
m = Collections.emptyMap();
return m;
}
public static Collection cloneOrEmptyIfNull(Collection c)
{
if (null == c)
c = new ArrayList(0);
else
c = new ArrayList(c);
return c;
}
public static Map cloneOrEmptyIfNull(Map m)
{
if (null == m)
m = new HashMap(0);
else
m = new HashMap(m);
return m;
}
/**
* Indicates whether a collection is {@code null} or empty.
* @param c the collection to be tested
* @return {@code true} if the collection is {@code null} or empty
*/
public static boolean nullOrEmpty(Collection c)
{
return null == c || c.isEmpty();
}
public interface Predicate
{
public boolean verifiedBy(T elem);
}
public static List filter(Collection c, Predicate p)
{
List filtered = new ArrayList();
for (T e : c)
{
if (p.verifiedBy(e))
filtered.add(e);
}
return filtered;
}
public interface Projector
{
public T2 project(T1 e);
}
public static List project(
Collection c,
Projector p)
{
List projected = new ArrayList();
for (TSrc e : c)
{
projected.add(p.project(e));
}
return projected;
}
public static List filterByType(Collection c, final Class clazz){
return project(
filter(c,new Predicate()
{
@Override
public boolean verifiedBy(T elem)
{
return clazz.isAssignableFrom(elem.getClass());
}
}),
new Projector()
{
@Override
public T1 project(T e)
{
return (T1)e;
}
});
}
}