001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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     * @param sep the line separator, e.g. "\r\n"
056     */
057    LineSeparatorOption(String sep) {
058        lineSeparator = sep.getBytes(StandardCharsets.US_ASCII);
059    }
060
061    /**
062     * Checks that bytes is equal to the byte representation of this line separator.
063     * @param bytes a bytes array to check
064     * @return if bytes is equal to the byte representation
065     *     of this line separator
066     */
067    public boolean matches(byte... bytes) {
068        final boolean result;
069        if (this == LF_CR_CRLF) {
070            // this silently assumes LF and CR are of length 1
071            // CRLF always matches LF, so CRLF isn't tested
072            result = LF.matches(bytes) || CR.matches(bytes);
073        }
074        else {
075            result = Arrays.equals(bytes, lineSeparator);
076        }
077        return result;
078    }
079
080    /**
081     * Returns length of file separator in bytes.
082     * @return the length of the file separator in bytes,
083     *     e.g. 1 for CR, 2 for CRLF, ...
084     */
085    public int length() {
086        return lineSeparator.length;
087    }
088
089}