lcn.module.batch.core.item.file.mapping.ObjectMapper.java Source code

Java tutorial

Introduction

Here is the source code for lcn.module.batch.core.item.file.mapping.ObjectMapper.java

Source

/*
 * Copyright 2006-2007 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 lcn.module.batch.core.item.file.mapping;

import java.util.List;

import lcn.module.batch.core.reflection.ReflectionSupport;

import org.springframework.batch.item.file.transform.IncorrectTokenCountException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

/**
 * ObjectMapper ?
 * @author   ??
 * @since 2012.07.20
 * @version 1.0
 * @see 
 * <pre>
 *      ?(Modification Information)
 *   
 *   ?      ?           
 *  ------- -------- ---------------------------
 *  2012.07.20  ??      ?
 * </pre>
*/
public class ObjectMapper<T> implements InitializingBean {
    // VO type 
    private Class<? extends T> type;

    // names()
    private String[] names;

    // Reflection  ?
    private ReflectionSupport<?> egovReflectionSupport;

    /**
     * VO type? .
     * 
     * @param type
     */
    public void setType(Class<? extends T> type) {
        this.type = type;
    }

    /**
     * names .
     * 
     * @param names
     */
    public void setNames(String[] names) {
        this.names = names;
        Assert.notNull(names, "Names must be non-null");
    }

    /**
     * VO , Token? .
     * 
     * @param tokens: LineTokenizer? line? ? token
     * @return T: VO
     */
    @SuppressWarnings("unchecked")
    public T mapObject(List<String> tokens) {
        int tokenSize = tokens.size();

        // ? names()?      Token    ?.
        if (names.length != tokenSize) {
            throw new IncorrectTokenCountException(names.length, tokenSize);
        }

        return (T) egovReflectionSupport.generateObject((Class<?>) type, tokens, names);
    }

    public void afterPropertiesSet() throws Exception {
        Assert.notNull(type, "The type must be set");
        Assert.notNull(names, "The names must be set");

        egovReflectionSupport = new ReflectionSupport<T>();
        egovReflectionSupport.generateSetterMethodMap(type, names);
        egovReflectionSupport.getFieldType(type, names);
    }
}