001package jmri.jmrit.logixng.implementation;
002
003import java.io.File;
004import java.io.FileNotFoundException;
005
006import javax.annotation.CheckForNull;
007import javax.annotation.Nonnull;
008
009/**
010 * The default implementation of a NamedTable
011 *
012 * @author Daniel Bergqvist 2018
013 */
014public class DefaultCsvNamedTable extends AbstractNamedTable {
015
016    private String _fileName;
017    private boolean _fileHasSystemUserName;
018
019    private CsvType _csvType;
020
021    /**
022     * Create a new named table.
023     * @param sys the system name
024     * @param user the user name or null if no user name
025     * @param fileName the file name of the CSV table
026     * @param data the data in the table. Note that this data is not copied to
027     *        a new array but used by the table as is.
028     * @param csvType the type of delimiter used for the file (comma or tab)
029     * @throws BadUserNameException when needed
030     * @throws BadSystemNameException when needed
031     */
032    public DefaultCsvNamedTable(
033            @Nonnull String sys, @CheckForNull String user,
034            @CheckForNull String fileName,
035            @Nonnull Object[][] data,
036            CsvType csvType)
037            throws BadUserNameException, BadSystemNameException {
038        super(sys,user,data);
039
040        _fileName = fileName;
041        _csvType = csvType;
042    }
043
044    /**
045     * Create a new named table.
046     * @param sys the system name
047     * @param user the user name or null if no user name
048     * @param fileName the file name of the CSV table
049     * @param fileHasSystemUserName true if the file has system name and user name
050     * @param data the data in the table. Note that this data is not copied to
051     *        a new array but used by the table as is.
052     * @param csvType the type of delimiter used for the file (comma or tab)
053     * @throws BadUserNameException when needed
054     * @throws BadSystemNameException when needed
055     */
056    public DefaultCsvNamedTable(
057            @Nonnull String sys, @CheckForNull String user,
058            @CheckForNull String fileName,
059            boolean fileHasSystemUserName,
060            @Nonnull Object[][] data,
061            CsvType csvType)
062            throws BadUserNameException, BadSystemNameException {
063        super(sys,user,data);
064
065        _fileName = fileName;
066        _fileHasSystemUserName = fileHasSystemUserName;
067        _csvType = csvType;
068    }
069
070    @Override
071    public boolean isCsvTypeSupported() {
072        return true;
073    }
074
075    public String getFileName() {
076        return _fileName;
077    }
078
079    public void setFileName(String fileName) {
080        this._fileName = fileName;
081    }
082
083    @Override
084    public void setCsvType(CsvType csvType) {
085        _csvType = csvType;
086    }
087
088    @Override
089    public CsvType getCsvType() {
090        return _csvType;
091    }
092
093    @Override
094    public void storeTableAsCSV() throws FileNotFoundException {
095        storeTableAsCSV(new File(_fileName), _fileHasSystemUserName);
096    }
097
098}