JAVA泛型使用初体验

package com.baifubao.ebbp.base.util.common;

import java.util.Collection;
import java.util.Collections;

import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 集合类的安全操作
 * <li>other @see org.springframework.util.CollectionUtils</li>
 * </p>
 * 
 * @author lianzerong
 */
public class EbbpCollectionUtils extends CollectionUtils {

    /**
     * 获取集合的size,为空返回0
     * 
     * @param collection
     * @return
     */
    public static <E> int size(Collection<E> collection) {
        return isEmpty(collection) ? 0 : collection.size();
    }

    /**
     * <p>
     * 获取几个集合的size之和
     * <li>例如:List<List<Bean>>{list1, list2,list3} ;return list1.size+list2.size+list3.size</li>
     * <p>
     * 
     * @param elements
     * @return
     */
    public static <E> int sumSize(Collection<? extends Collection<E>> collections) {
        int sumSize = 0;
        if (isEmpty(collections)) {
            for (Collection<E> collection : collections) {
                sumSize += size(collection);
            }
        }

        return sumSize;
    }

    /**
     * save addAll,detail @see java.util.List
     * 
     * @param target
     * @param source
     * @return
     */
    public static <E> boolean addAll(Collection<E> target, Collection<E> source) {
        Assert.notNull(target);
        if (isEmpty(source)) {
            return true;
        }

        return target.addAll(source);
    }

    /**
     * add elements
     * 
     * @param target
     * @param elements
     * @return
     */
    public static <T> boolean addAll(Collection<? super T> target, T...elements) {
        Assert.notNull(target);
        Assert.notNull(elements);

        return Collections.addAll(target, elements);
    }

}

你可能感兴趣的:(java)