com.xdtech.core.orm.utils.CollectionUtils.java Source code

Java tutorial

Introduction

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

Source

/**
 * Copyright (c) 2005-2011 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: Fixtures.java 1593 2011-05-11 10:37:12Z calvinxiu $
 */
package com.xdtech.core.orm.utils;

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

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

public class CollectionUtils {

    /**
     * ????(Getter), ??List.
     * 
     * @param collection ???.
     * @param propertyName ??????.
     */
    public static List extractElementPropertyToList(final Collection collection, final String propertyName) {
        List list = new ArrayList();

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

        return list;
    }

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

}