/*
 * Copyright (c) 2007 NTT DATA Corporation
 *
 * 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 jp.terasoluna.fw.file.dao.standard;


import java.util.Map;

import jp.terasoluna.fw.file.annotation.FileFormat;
import jp.terasoluna.fw.file.dao.FileException;

/**
 * 壜曄挿僼傽僀儖梡偺僼傽僀儖傾僋僙僗(僨乕僞彂崬)僋儔僗丅
 * 
 * <p>
 * 僼傽僀儖峴僆僽僕僃僋僩偐傜僨乕僞傪撉傒崬傒丄1峴暘偺僨乕僞傪壜曄挿宍幃偱
 * 僼傽僀儖偵彂偒崬傓丅
 * </p>
 * 
 * <b>仸棙梡偡傞僼傽僀儖峴僆僽僕僃僋僩偺傾僲僥乕僔儑儞崁栚</b><br>
 * 鶣丏@{@link FileFormat}偺愝掕崁栚<br>
 * <div align="center">
 *  <table width="90%" border="1" bgcolor="#FFFFFF">
 *   <tr>
 *    <td> <b>榑棟崁栚柤</b> </td>
 *    <td> <b>暔棟崁栚柤</b> </td>
 *    <td> <b>僨僼僅儖僩抣</b> </td>
 *    <td> <b>昁恵惈</b> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>峴嬫愗傝暥帤</code> </td>
 *    <td> <code>lineFeedChar</code> </td>
 *    <td> <code>僔僗僥儉偺峴嬫愗傝暥帤</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>嬫愗傝暥帤</code> </td>
 *    <td> <code>delimiter</code> </td>
 *    <td> <code>','</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>埻傒暥帤</code> </td>
 *    <td> <code>encloseChar</code> </td>
 *    <td> <code>側偟('\u0000')</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>僼傽僀儖僄儞僐乕僨傿儞僌</code> </td>
 *    <td> <code>fileEncodeing</code> </td>
 *    <td> <code>僔僗僥儉偺僼傽僀儖僄儞僐乕僨傿儞僌</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>僼傽僀儖忋彂偒僼儔僌</code> </td>
 *    <td> <code>overWriteFlg</code> </td>
 *    <td> <code>false</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *  </table>
 * </div>
 * <br>
 * 鶤丏@{@link jp.terasoluna.fw.file.annotation.InputFileColumn}丄@{@link jp.terasoluna.fw.file.annotation.OutputFileColumn}偺愝掕崁栚<br>
 * <div align="center">
 *  <table width="90%" border="1" bgcolor="#FFFFFF">
 *   <tr>
 *    <td> <b>榑棟崁栚柤</b> </td>
 *    <td> <b>暔棟崁栚柤</b> </td>
 *    <td> <b>僨僼僅儖僩抣</b> </td>
 *    <td> <b>昁恵惈</b> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>僇儔儉僀儞僨僢僋僗</code> </td>
 *    <td> <code>columnIndex</code> </td>
 *    <td> - </td>
 *    <td> <code>昁恵</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>僼僅乕儅僢僩</code> </td>
 *    <td> <code>columnFormat</code> </td>
 *    <td> <code>""</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>僶僀僩挿</code> </td>
 *    <td> <code>bytes</code> </td>
 *    <td> <code>-1</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>僷僨傿儞僌庬暿</code> </td>
 *    <td> <code>paddingType</code> </td>
 *    <td> <code>僷僨傿儞僌側偟</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>僷僨傿儞僌暥帤</code> </td>
 *    <td> <code>paddingChar</code> </td>
 *    <td> <code>' '</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>僩儕儉庬暿</code> </td>
 *    <td> <code>trimType</code> </td>
 *    <td> <code>僩儕儉側偟</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>僩儕儉暥帤</code> </td>
 *    <td> <code>trimChar</code> </td>
 *    <td> <code>' '</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *   <tr>
 *    <td> <code>暥帤曄姺庬暿</code> </td>
 *    <td> <code>stringConverter</code> </td>
 *    <td> <code>NullStringConverter.class</code> </td>
 *    <td> <code>僆僾僔儑儞</code> </td>
 *   </tr>
 *  </table>
 * </div>
 * <br>
 * 
 * <b>仸拲堄帠崁</b><br>
 * <ul>
 * 丂<li>嬫愗傝暥帤偵Caracter.MIN_VALUE傪愝掕偡傞偙偲偼弌棃側偄丅(僄儔乕敪惗)</li>
 * </ul>
 * 
 * 
 * @param <T>
 *            僼傽僀儖峴僆僽僕僃僋僩
 */
public class VariableFileLineWriter<T> extends AbstractFileLineWriter<T> {

    /**
     * 嬫愗傝暥帤丅壜曄挿僼傽僀儖弌椡偺応崌丄僨僼僅儖僩偼乽,(僇儞儅)乿丅
     */
    private char delimiter = ',';

    /**
     * 埻傒暥帤丅
     */
    private char encloseChar = Character.MIN_VALUE;

    /**
     * 僐儞僗僩儔僋僞丅
     * 
     * @param fileName
     *            僼傽僀儖柤
     * @param clazz
     *            僷儔儊乕僞僋儔僗
     * @param columnFormatterMap
     *            僥僉僗僩庢摼儖乕儖
     */
    public VariableFileLineWriter(String fileName, Class<T> clazz,
            Map<String, ColumnFormatter> columnFormatterMap) {
    
        super(fileName, clazz, columnFormatterMap);
    
        FileFormat fileFormat = clazz.getAnnotation(FileFormat.class);

        // 嬫愗傝暥帤偑Character.MIN_VALUE偺応崌丄椺奜傪僗儘乕偡傞丅
        if (fileFormat.delimiter() == Character.MIN_VALUE) {
            throw new FileException("Delimiter can not use '\\u0000'.",
                    new IllegalStateException(), fileName);
        }
        
        // 嬫愗傝暥帤傪愝掕偡傞丅
        delimiter = fileFormat.delimiter();
    
        // 埻傒暥帤傪愝掕偡傞丅
        encloseChar = fileFormat.encloseChar();
        
        //弶婜壔張棟
        super.init();
    }

    /**
     * 嬫愗傝暥帤傪庢摼偡傞丅
     * 
     * @return 嬫愗傝暥帤
     */
    public char getDelimiter() {

        return delimiter;
    }

    /**
     * 埻傒暥帤傪庢摼偡傞丅
     * 
     * @return 埻傒暥帤
     */
    public char getEncloseChar() {

        return encloseChar;
    }
}