com.hihframework.core.utils.CollectionUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.hihframework.core.utils.CollectionUtils.java

Source

/**
 * Copyright (c) 2013-2015 www.javahih.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.hihframework.core.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

/**
 * <p> Title:??Utils? </p>
 * <p> Description:</p>
 * <p> Copyright: Copyright (c) 2013 </p>
 * <p> Company:hihsoft.co.,ltd </p>
 *
 * @author hihsoft.co.,ltd
 * @version 1.0
 */

public class CollectionUtils {

    private CollectionUtils() {
    }

    /**
     * ????(Getter), ??Map.
     * add by zhujw ???map
     * @param collection ???.
     * @param keyPropertyName ????MapKey??.
     * @param valuePropertyName ????MapValue??.
     */
    public static Map<Object, Object> fetchPropertyToMap(final Collection<?> collection,
            final String keyPropertyName, final String valuePropertyName) throws Exception {
        Map<Object, Object> map = new HashMap<Object, Object>();
        for (Object obj : collection) {
            map.put(PropertyUtils.getProperty(obj, keyPropertyName),
                    PropertyUtils.getProperty(obj, valuePropertyName));
        }
        return map;
    }

    /**
     * ????,??.
     * 
     * @param collection
     *            ???.
     * @param propertyName
     *            ??????.
     */
    public static List<Object> fetchPropertyToList(Collection<?> collection, String propertyName) throws Exception {

        List<Object> list = new ArrayList<Object>();

        for (Object obj : collection) {
            list.add(PropertyUtils.getProperty(obj, propertyName));
        }

        return list;
    }

    /**
     * ????,??.
     * 
     * @param collection
     *            ???.
     * @param propertyName
     *            ??????.
     * @param separator
     *            .
     */
    public static String fetchPropertyToString(Collection<Object> collection, String propertyName, String separator)
            throws Exception {
        List<Object> list = fetchPropertyToList(collection, propertyName);
        return StringUtils.join(list, separator);
    }

    /**
     * ?ID?,???.
     * ????ID?,ID?ID??.
     * ?httpid??.
     * 
     * @param collection
     *            ??
     * @param checkedIds
     *            ?
     * @param clazz
     *            ?
     */
    public static <T, ID> void mergeByCheckedIds(Collection<T> collection, Collection<ID> checkedIds,
            Class<T> clazz) throws Exception {
        mergeByCheckedIds(collection, checkedIds, "id", clazz);
    }

    /**
     * ?ID?,???.
     * http?????idhibernate???????
     * ?
     * ???ID?,ID?ID??.
     * 
     * @param collection
     *            ??
     * @param checkedIds
     *            ?
     * @param idName
     *            ID??
     * @param clazz
     *            ?
     */
    public static <T, ID> void mergeByCheckedIds(Collection<T> collection, Collection<ID> checkedIds, String idName,
            Class<T> clazz) throws Exception {

        if (checkedIds == null) {
            collection.clear();
            return;
        }

        Iterator<T> it = collection.iterator();

        while (it.hasNext()) {
            T obj = it.next();
            if (checkedIds.contains(PropertyUtils.getProperty(obj, idName))) {
                checkedIds.remove(PropertyUtils.getProperty(obj, idName));
            } else {
                it.remove();
            }
        }

        for (ID id : checkedIds) {
            T obj = clazz.newInstance();
            PropertyUtils.setProperty(obj, idName, id);
            collection.add(obj);
        }
    }
}