Coverage Report - com.puppycrawl.tools.checkstyle.checks.LineSeparatorOption
 
Classes in this File Line Coverage Branch Coverage Complexity
LineSeparatorOption
100%
16/16
100%
8/8
2
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2018 the original author or authors.
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 
 20  
 package com.puppycrawl.tools.checkstyle.checks;
 21  
 
 22  
 import java.nio.charset.StandardCharsets;
 23  
 import java.util.Arrays;
 24  
 
 25  
 /**
 26  
  * Represents the options for line separator settings.
 27  
  *
 28  
  * @author lkuehne
 29  
  * @see NewlineAtEndOfFileCheck
 30  
  */
 31  7
 public enum LineSeparatorOption {
 32  
 
 33  
     /** Windows-style line separators. **/
 34  3
     CRLF("\r\n"),
 35  
 
 36  
     /** Mac-style line separators. **/
 37  3
     CR("\r"),
 38  
 
 39  
     /** Unix-style line separators. **/
 40  3
     LF("\n"),
 41  
 
 42  
     /**
 43  
      * Matches CR, LF and CRLF line separators.
 44  
      * Only the length is used - the actual value is ignored.
 45  
      */
 46  3
     LF_CR_CRLF("##"),
 47  
 
 48  
     /** System default line separators. **/
 49  3
     SYSTEM(System.getProperty("line.separator"));
 50  
 
 51  
     /** The line separator representation. */
 52  
     private final byte[] lineSeparator;
 53  
 
 54  
     /**
 55  
      * Creates a new {@code LineSeparatorOption} instance.
 56  
      * @param sep the line separator, e.g. "\r\n"
 57  
      */
 58  15
     LineSeparatorOption(String sep) {
 59  15
         lineSeparator = sep.getBytes(StandardCharsets.US_ASCII);
 60  15
     }
 61  
 
 62  
     /**
 63  
      * Checks that bytes is equal to the byte representation of this line separator.
 64  
      * @param bytes a bytes array to check
 65  
      * @return if bytes is equal to the byte representation
 66  
      *     of this line separator
 67  
      */
 68  
     public boolean matches(byte... bytes) {
 69  
         final boolean result;
 70  20
         if (this == LF_CR_CRLF) {
 71  
             // this silently assumes CRLF and ANY have the same length
 72  
             // and LF and CR are of length 1
 73  4
             result = CRLF.matches(bytes)
 74  3
                 || LF.matches(Arrays.copyOfRange(bytes, 1, 2))
 75  2
                 || CR.matches(Arrays.copyOfRange(bytes, 1, 2));
 76  
         }
 77  
         else {
 78  16
             result = Arrays.equals(bytes, lineSeparator);
 79  
         }
 80  20
         return result;
 81  
     }
 82  
 
 83  
     /**
 84  
      * Returns length of file separator in bytes.
 85  
      * @return the length of the file separator in bytes,
 86  
      *     e.g. 1 for CR, 2 for CRLF, ...
 87  
      */
 88  
     public int length() {
 89  15
         return lineSeparator.length;
 90  
     }
 91  
 
 92  
 }