com.github.dactiv.common.bundle.BeanResourceBundle.java Source code

Java tutorial

Introduction

Here is the source code for com.github.dactiv.common.bundle.BeanResourceBundle.java

Source

/*
 * Copyright 2013-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.dactiv.common.bundle;

import java.beans.PropertyDescriptor;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.Vector;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import com.github.dactiv.common.utils.ReflectionUtils;
import org.springframework.beans.BeanUtils;

/**
 * ?map,???key?valuemap
 * ?{@link MapUtils#toMap(ResourceBundle)}?map
 * 
 * @author maurice
 *
 */
public class BeanResourceBundle extends ResourceBundle {

    //???mapbean
    private Object bean;
    //bean?
    private String[] include;
    //?bean?
    private String[] exclude;
    //?null
    private boolean ignoreNullValue;

    /**
     * ?map,???key?valuemap
     * ?{@link MapUtils#toMap(ResourceBundle)}?map
     * 
     * @param bean ??mapbean
     */
    public BeanResourceBundle(Object bean) {
        this(bean, null);
    }

    /**
     * ?map,???key?valuemap
     * ?{@link MapUtils#toMap(ResourceBundle)}?map
     * 
     * @param bean ??mapbean
     * @param include bean???map
     * 
     */
    public BeanResourceBundle(Object bean, String[] include) {
        this(bean, include, null);
    }

    /**
     * ?map,???key?valuemap
     * ?{@link MapUtils#toMap(ResourceBundle)}?map
     * 
     * @param bean ??mapbean
     * @param include bean???map
     * @param exclude bean??map
     */
    public BeanResourceBundle(Object bean, String[] include, String[] exclude) {
        this(bean, include, exclude, true);
    }

    /**
     * ?map,???key?valuemap
     * ?{@link MapUtils#toMap(ResourceBundle)}?map
     * 
     * @param bean ??mapbean
     * @param include bean???map
     * @param exclude bean??map
     * @param ignoreNullValue ?nul
     */
    public BeanResourceBundle(Object bean, String[] include, String[] exclude, boolean ignoreNullValue) {
        this.bean = bean;
        this.include = include;
        this.exclude = exclude;
        this.ignoreNullValue = ignoreNullValue;
    }

    /**
     * ?key???bean
     */
    @Override
    protected Object handleGetObject(String key) {
        Object result = ReflectionUtils.invokeGetterMethod(bean, key);
        return result == null ? "" : result;
    }

    /**
     * ?bean?mapkey
     */
    @Override
    public Enumeration<String> getKeys() {

        Vector<String> vector = new Vector<String>();
        PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(bean.getClass());
        //???bean
        for (PropertyDescriptor pd : propertyDescriptors) {
            //get/set???
            if ((pd.getWriteMethod() == null) || pd.getReadMethod() == null) {
                continue;
            }

            String key = pd.getName();

            /*
             * ?map?:
             * 1.include
             * 2.?exclude
             * 3.ignoreNullValuetrue,?null
             */
            if (isIncludeProperty(key) && !isExcludeProperty(key)) {

                if (ignoreNullValue && ReflectionUtils.invokeGetterMethod(bean, key) == null) {
                    continue;
                }

                vector.addElement(key);
            }
        }

        //?mapkey
        return vector.elements();
    }

    /**
     * ???true,false?
     * 
     * @param name ??
     * 
     * @return boolean
     */
    private boolean isIncludeProperty(String name) {
        return include != null && include.length > 0 ? ArrayUtils.contains(include, name) : true;
    }

    /**
     * ???true,false?
     * 
     * @param name ??
     * 
     * @return boolean
     */
    private boolean isExcludeProperty(String name) {
        return exclude != null && exclude.length > 0 ? ArrayUtils.contains(exclude, name) : false;
    }
}