Coverage Report - com.puppycrawl.tools.checkstyle.api.Comment
 
Classes in this File Line Coverage Branch Coverage Complexity
Comment
100%
20/20
100%
4/4
1.125
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2017 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.api;
 21  
 
 22  
 import java.util.Arrays;
 23  
 
 24  
 /**
 25  
  * Representation of the comment block.
 26  
  *
 27  
  * @author o_sukhodolsky
 28  
  */
 29  
 public class Comment implements TextBlock {
 30  
     /** Text of the comment. */
 31  
     private final String[] text;
 32  
 
 33  
     /** Number of first line of the comment. */
 34  
     private final int startLineNo;
 35  
 
 36  
     /** Number of last line of the comment. */
 37  
     private final int endLineNo;
 38  
 
 39  
     /** Number of first column of the comment. */
 40  
     private final int startColNo;
 41  
 
 42  
     /** Number of last column of the comment. */
 43  
     private final int endColNo;
 44  
 
 45  
     /**
 46  
      * Creates new instance.
 47  
      * @param text the lines that make up the comment.
 48  
      * @param firstCol number of the first column of the comment.
 49  
      * @param lastLine number of the last line of the comment.
 50  
      * @param lastCol number of the last column of the comment.
 51  
      */
 52  
     public Comment(final String[] text, final int firstCol,
 53  192950
             final int lastLine, final int lastCol) {
 54  192950
         this.text = new String[text.length];
 55  192950
         System.arraycopy(text, 0, this.text, 0, this.text.length);
 56  192950
         startLineNo = lastLine - this.text.length + 1;
 57  192950
         endLineNo = lastLine;
 58  192950
         startColNo = firstCol;
 59  192950
         endColNo = lastCol;
 60  192950
     }
 61  
 
 62  
     @Override
 63  
     public final String[] getText() {
 64  1540
         return text.clone();
 65  
     }
 66  
 
 67  
     @Override
 68  
     public final int getStartLineNo() {
 69  1150
         return startLineNo;
 70  
     }
 71  
 
 72  
     @Override
 73  
     public final int getEndLineNo() {
 74  7
         return endLineNo;
 75  
     }
 76  
 
 77  
     @Override
 78  
     public int getStartColNo() {
 79  438
         return startColNo;
 80  
     }
 81  
 
 82  
     @Override
 83  
     public int getEndColNo() {
 84  18
         return endColNo;
 85  
     }
 86  
 
 87  
     @Override
 88  
     public boolean intersects(int startLine, int startCol,
 89  
                               int endLine, int endCol) {
 90  
         // compute a single number for start and end
 91  
         // to simplify conditional logic
 92  1454
         final long multiplier = Integer.MAX_VALUE;
 93  1454
         final long thisStart = startLineNo * multiplier + startColNo;
 94  1454
         final long thisEnd = endLineNo * multiplier + endColNo;
 95  1454
         final long inStart = startLine * multiplier + startCol;
 96  1454
         final long inEnd = endLine * multiplier + endCol;
 97  
 
 98  1454
         return thisEnd >= inStart && inEnd >= thisStart;
 99  
     }
 100  
 
 101  
     @Override
 102  
     public String toString() {
 103  11
         return "Comment[text=" + Arrays.toString(text)
 104  
                 + ", startLineNo=" + startLineNo
 105  
                 + ", endLineNo=" + endLineNo
 106  
                 + ", startColNo=" + startColNo
 107  
                 + ", endColNo=" + endColNo + ']';
 108  
     }
 109  
 }