001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks;
021
022import java.nio.charset.StandardCharsets;
023import java.util.Arrays;
024
025/**
026 * Represents the options for line separator settings.
027 *
028 * @see NewlineAtEndOfFileCheck
029 */
030public enum LineSeparatorOption {
031
032    /** Windows-style line separators. **/
033    CRLF("\r\n"),
034
035    /** Mac-style line separators. **/
036    CR("\r"),
037
038    /** Unix-style line separators. **/
039    LF("\n"),
040
041    /**
042     * Matches CR, LF and CRLF line separators.
043     * Only the length is used - the actual value is ignored.
044     */
045    LF_CR_CRLF("#"),
046
047    /** System default line separators. **/
048    SYSTEM(System.getProperty("line.separator"));
049
050    /** The line separator representation. */
051    private final byte[] lineSeparator;
052
053    /**
054     * Creates a new {@code LineSeparatorOption} instance.
055     *
056     * @param sep the line separator, e.g. "\r\n"
057     */
058    LineSeparatorOption(String sep) {
059        lineSeparator = sep.getBytes(StandardCharsets.US_ASCII);
060    }
061
062    /**
063     * Checks that bytes is equal to the byte representation of this line separator.
064     *
065     * @param bytes a bytes array to check
066     * @return if bytes is equal to the byte representation
067     *     of this line separator
068     */
069    public boolean matches(byte... bytes) {
070        final boolean result;
071        if (this == LF_CR_CRLF) {
072            // this silently assumes LF and CR are of length 1
073            // CRLF always matches LF, so CRLF isn't tested
074            result = LF.matches(bytes) || CR.matches(bytes);
075        }
076        else {
077            result = Arrays.equals(bytes, lineSeparator);
078        }
079        return result;
080    }
081
082    /**
083     * Returns length of file separator in bytes.
084     *
085     * @return the length of the file separator in bytes,
086     *     e.g. 1 for CR, 2 for CRLF, ...
087     */
088    public int length() {
089        return lineSeparator.length;
090    }
091
092}