LineDelimiter.java

package de.siegmar.fastcsv.writer;

/// The line breaks the writer can write after each record and comment (LF, CR, CRLF, platform default).
public enum LineDelimiter {

    /// Line feed (Unix).
    LF("\n"),

    /// Carriage return (classic Mac OS).
    CR("\r"),

    /// Carriage return and line feed (Windows).
    CRLF("\r\n"),

    /// The default of the current platform ([System#lineSeparator()]).
    PLATFORM(System.lineSeparator());

    private final String str;

    LineDelimiter(final String str) {
        this.str = str;
    }

    /// Maps a line break string to the corresponding enum constant.
    ///
    /// Accepted values are `"\r\n"` ([#CRLF]), `"\n"` ([#LF]) and `"\r"` ([#CR]).
    ///
    /// @param str the line break string to look up
    /// @return the matching enum constant
    /// @throws NullPointerException if `str` is `null`
    /// @throws IllegalArgumentException if `str` is not one of the accepted values
    public static LineDelimiter of(final String str) {
        return switch (str) {
            case "\r\n" -> CRLF;
            case "\n" -> LF;
            case "\r" -> CR;
            default -> throw new IllegalArgumentException("Unknown line delimiter: " + str);
        };
    }

    @Override
    public String toString() {
        return str;
    }

}