com.wwinsoft.modules.utils.reflection.ConvertUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.wwinsoft.modules.utils.reflection.ConvertUtils.java

Source

/**
 * Copyright (c) 2005-2010 wwinsoft.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: ConvertUtils.java 1211 2010-09-10 16:20:45Z calvinxiu $
 */
package com.wwinsoft.modules.utils.reflection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

public class ConvertUtils {

    static {
        registerDateConverter();
    }

    /**
     * ????(getter), ??List.
     * 
     * @param collection ???.
     * @param propertyName ??????.
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> convertElementPropertyToList(final Collection collection, final String propertyName) {
        List<T> list = new ArrayList();

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

        return list;
    }

    /**
     * ????(getter), ??.
     * 
     * @param collection ???.
     * @param propertyName ??????.
     * @param separator .
     */
    @SuppressWarnings("unchecked")
    public static String convertElementPropertyToString(final Collection collection, final String propertyName,
            final String separator) {
        List list = convertElementPropertyToList(collection, propertyName);
        return StringUtils.join(list, separator);
    }

    public static List convertSeparatorStringToList(final String separatorString, final String separator,
            Class<?> toType) {
        List list = new ArrayList();
        String[] split = separatorString.split(separator);
        for (String s : split) {
            if (StringUtils.isNotBlank(s)) {
                list.add(convertStringToObject(s, toType));
            }
        }
        return list;
    }

    public static Set convertSeparatorStringToSet(final String separatorString, final String separator,
            Class<?> toType) {
        Set set = new HashSet();
        String[] split = separatorString.split(separator);
        for (String s : split) {
            if (StringUtils.isNotBlank(s)) {
                set.add(convertStringToObject(s, toType));
            }
        }
        return set;
    }

    /**
     * ?.
     * 
     * @param value ?.
     * @param toType ?.
     */
    public static Object convertStringToObject(String value, Class<?> toType) {
        try {
            return org.apache.commons.beanutils.ConvertUtils.convert(value, toType);
        } catch (Exception e) {
            throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
        }
    }

    /**
     * Converter?: yyyy-MM-dd  yyyy-MM-dd HH:mm:ss
     */
    private static void registerDateConverter() {
        DateConverter dc = new DateConverter();
        dc.setUseLocaleFormat(true);
        dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
        org.apache.commons.beanutils.ConvertUtils.register(dc, Date.class);
    }
}