com.micmiu.modules.utils.Collections3.java Source code

Java tutorial

Introduction

Here is the source code for com.micmiu.modules.utils.Collections3.java

Source

/**
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.micmiu.modules.utils;

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

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

/**
 * Collections.
 * JDKCollectionsGuavaCollections2?, ??Collections3.
 * 
 * @author calvin
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Collections3 {

    private Collections3() {
    }

    /**
     * a-b?.
     */
    public static <T> List<T> subtract(final Collection<T> a, final Collection<T> b) {
        ArrayList<T> list = new ArrayList<T>(a);
        for (Object element : b) {
            list.remove(element);
        }
        return list;
    }

    /**
     * ????(Getter), ??Map.
     * 
     * @param collection ???.
     * @param keyPropertyName ????MapKey??.
     * @param valuePropertyName ????MapValue??.
     */
    public static Map<?, ?> extractToMap(final Collection<?> collection, final String keyPropertyName,
            final String valuePropertyName) {
        Map<Object, Object> map = new HashMap<Object, Object>(collection.size());

        try {
            for (Object obj : collection) {
                map.put(PropertyUtils.getProperty(obj, keyPropertyName),
                        PropertyUtils.getProperty(obj, valuePropertyName));
            }
        } catch (Exception e) {
            throw Reflections.convertReflectionExceptionToUnchecked(e);
        }

        return map;
    }

    /**
     * ????(Getter), ??List.
     * 
     * @param collection ???.
     * @param propertyName ??????.
     */

    public static List extractToList(final Collection collection, final String propertyName) {
        List list = new ArrayList(collection.size());

        try {
            for (Object obj : collection) {
                list.add(PropertyUtils.getProperty(obj, propertyName));
            }
        } catch (Exception e) {
            throw Reflections.convertReflectionExceptionToUnchecked(e);
        }

        return list;
    }

    /**
     * ????(Getter), ??.
     * 
     * @param collection ???.
     * @param propertyName ??????.
     * @param separator .
     */
    public static String extractToString(final Collection collection, final String propertyName,
            final String separator) {
        List list = extractToList(collection, propertyName);
        return StringUtils.join(list, separator);
    }

    /**
     * ?CollectionString,  separator
     */
    public static String convertToString(final Collection collection, final String separator) {
        return StringUtils.join(collection, separator);
    }

    /**
     * ?CollectionString, ???prefix??postfix<div>mymessage</div>
     */
    public static String convertToString(final Collection collection, final String prefix, final String postfix) {
        StringBuilder builder = new StringBuilder();
        for (Object o : collection) {
            builder.append(prefix).append(o).append(postfix);
        }
        return builder.toString();
    }
}