com.jlfex.hermes.common.utils.CollectionUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.jlfex.hermes.common.utils.CollectionUtil.java

Source

package com.jlfex.hermes.common.utils;

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * Collections.
 * 
 * JDKCollectionsGuavaCollections2?, ??Collections3.
 * 
 * ????????Apache Commons Collection, ??
 * 
 * @author calvin
 */
public class CollectionUtil {

    /**
     * ????(Getter), ??Map.
     * 
     * @param collection ???.
     * @param keyPropertyName ????MapKey??.
     * @param valuePropertyName ????MapValue??.
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Map extractToMap(final Collection collection, final String keyPropertyName,
            final String valuePropertyName) {
        Map map = new HashMap(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 ??????.
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    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 .
     */
    @SuppressWarnings("rawtypes")
    public static String extractToString(final Collection collection, final String propertyName,
            final String separator) {
        List list = extractToList(collection, propertyName);
        return StringUtils.join(list, separator);
    }

    /**
     * ?Collection(toString())String,  separator
     */
    @SuppressWarnings("rawtypes")
    public static String convertToString(final Collection collection, final String separator) {
        return StringUtils.join(collection, separator);
    }

    /**
     * ?Collection(toString())String, ???prefix??postfix<div>mymessage</div>
     */
    @SuppressWarnings("rawtypes")
    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();
    }

    /**
     * ?.
     */
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Collection collection) {
        return (collection == null) || collection.isEmpty();
    }

    /**
     * ?.
     */
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Map map) {
        return (map == null) || map.isEmpty();
    }

    /**
     * ?.
     */
    @SuppressWarnings("rawtypes")
    public static boolean isNotEmpty(Collection collection) {
        return (collection != null) && !(collection.isEmpty());
    }

    /**
     * ?Collectioncollectionnull.
     */
    public static <T> T getFirst(Collection<T> collection) {
        if (isEmpty(collection)) {
            return null;
        }

        return collection.iterator().next();
    }

    /**
     * ?Collection? collectionnull.
     */
    public static <T> T getLast(Collection<T> collection) {
        if (isEmpty(collection)) {
            return null;
        }

        // List?? 
        if (collection instanceof List) {
            List<T> list = (List<T>) collection;
            return list.get(list.size() - 1);
        }

        // iterator?.
        Iterator<T> iterator = collection.iterator();
        while (true) {
            T current = iterator.next();
            if (!iterator.hasNext()) {
                return current;
            }
        }
    }

    /**
     * a+bList.
     */
    public static <T> List<T> union(final Collection<T> a, final Collection<T> b) {
        List<T> result = new ArrayList<T>(a);
        result.addAll(b);
        return result;
    }

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

        return list;
    }

    /**
     * abList.
     */
    public static <T> List<T> intersection(Collection<T> a, Collection<T> b) {
        List<T> list = new ArrayList<T>();

        for (T element : a) {
            if (b.contains(element)) {
                list.add(element);
            }
        }
        return list;
    }

    /**
     * Set?  List?
     * @param a
     * @return
     */
    public static <T> List<T> switchSet2List(Set<T> a) {
        List<T> list = new ArrayList<T>();
        list.addAll(a);
        return list;
    }

    /**
     * List?  Set ?
     * @param a
     * @return
     */
    public static <T> Set<T> switchList2Set(List<T> a) {
        Set<T> sets = new HashSet<T>();
        sets.addAll(a);
        return sets;
    }
}