
package de.ivu.elect.exchange.csv;

import java.nio.charset.Charset;
import java.util.Optional;

import org.apache.commons.io.ByteOrderMark;
import org.jsefa.csv.CsvIOFactory;

import de.ivu.elect.business.datei.entity.CsvZeichensatz;

public abstract class AbstractAutoCloseableCsvSerializing<T> implements AutoCloseable {
  
  private final CsvDescriptor csvDescriptor;

  private final Charset charset;

  private final Optional<ByteOrderMark> charsetBom;

  public AbstractAutoCloseableCsvSerializing(CsvDescriptor csvDescriptor) {
    super();

    this.csvDescriptor = csvDescriptor;

    CsvZeichensatz zeichensatz = csvDescriptor.getCsvZeichensatz();

    this.charset = zeichensatz.toCharset();
    this.charsetBom = zeichensatz.getCharsetBom();
  }

  public CsvDescriptor getCsvDescriptor() {
    return csvDescriptor;
  }

  protected Charset getCharset() {
    return charset;
  }

  protected Optional<ByteOrderMark> getCharsetBom() {
    return charsetBom;
  }

  protected CsvIOFactory createCsvIOFactory(Class<T> clazz) {
    return ElectCsvIOFactory.create(getCsvDescriptor(), clazz);
  }
}
