aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-squid/src/main/java/org/sonar/squid/text/Line.java
blob: 5a46adecf5c43401fa1d89d5cd5c992f6a4fae1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2013 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.sonar.squid.text;

import org.apache.commons.lang.StringUtils;
import org.sonar.squid.measures.Measurable;
import org.sonar.squid.measures.Metric;

class Line implements Measurable<Metric> {

  private static final String NOSONAR_TAG = "NOSONAR";

  private final int lineIndex;
  private int blankLine = 0;
  private int line = 1;
  private int lineOfCode = 0;
  private int commentLine = 0;
  private int headerCommentLine = 0;
  private int commentBlankLine = 0;
  private int commentedOutCodeLine = 0;
  private String comment = null;
  private StringBuilder stringLine;
  private boolean isBlank;
  private boolean isThereJavadoc;
  private boolean isThereLicenseHeaderComment;

  Line() {
    this.lineIndex = 0;
  }

  Line(String stringLine) {
    this();
    setString(new StringBuilder(stringLine));
  }

  Line(int lineIndex, StringBuilder stringLine) {
    this(lineIndex);
    setString(stringLine);
  }

  Line(int lineIndex) {
    this.lineIndex = lineIndex;
  }

  final void setString(StringBuilder stringLine) {
    this.stringLine = stringLine;
    isBlank = isBlankLine();
  }

  private boolean isBlankLine() {
    for (int i = 0; i < stringLine.length(); i++) {
      if ( !Character.isWhitespace(stringLine.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  /**
   * {@inheritDoc}
   */
  public double getDouble(Metric metric) {
    return getInt(metric);
  }

  /**
   * {@inheritDoc}
   */
  public int getInt(Metric metric) {
    switch (metric) {
      case BLANK_LINES:
        return blankLine;
      case LINES:
        return line;
      case LINES_OF_CODE:
        return lineOfCode;
      case COMMENT_LINES:
        return commentLine;
      case COMMENTED_OUT_CODE_LINES:
        return commentedOutCodeLine;
      case COMMENT_BLANK_LINES:
        return commentBlankLine;
      case HEADER_COMMENT_LINES:
        return headerCommentLine;
      default:
        throw new IllegalStateException("Metric " + metric.name() + " is not available on Line object.");
    }
  }

  /**
   * {@inheritDoc}
   */
  public void setMeasure(Metric metric, double measure) {
    setMeasure(metric, (int) measure);
  }

  /**
   * {@inheritDoc}
   */
  public void setMeasure(Metric metric, int measure) {
    switch (metric) {
      case BLANK_LINES:
        blankLine = measure;
        break;
      case LINES_OF_CODE:
        lineOfCode = measure;
        break;
      case COMMENT_LINES:
        commentLine = measure;
        break;
      case COMMENTED_OUT_CODE_LINES:
        commentedOutCodeLine = measure;
        break;
      case COMMENT_BLANK_LINES:
        commentBlankLine = measure;
        break;
      case HEADER_COMMENT_LINES:
        headerCommentLine = measure;
        break;
      case LINES:
        throw new IllegalStateException("Metric LINES always equals 1 on a Line and you are not permitted to change this value.");
      default:
        throw new IllegalStateException("Metric " + metric.name() + " is not suitable for Line object.");
    }
  }

  void setComment(String comment) {
    this.comment = comment;
  }

  void setComment(String comment, boolean isJavadoc) {
    setComment(comment);
    this.isThereJavadoc = isJavadoc;
  }

  void setComment(String comment, boolean isJavadoc, boolean isLicenseHeader) {
    setComment(comment, isJavadoc);
    this.isThereLicenseHeaderComment = isLicenseHeader;
  }

  String getString() {
    return stringLine.toString();
  }

  boolean isBlank() {
    return !isThereComment() && isBlank;
  }

  boolean isThereCode() {
    if ( !isBlank() && !isThereComment()) {
      return true;
    }
    if (isThereComment() && isThereCodeBeforeOrAfterComment()) {
      return true;
    }
    return false;
  }

  private boolean isThereCodeBeforeOrAfterComment() {
    if ( !isThereComment()) {
      throw new IllegalStateException("You can't call this method when there isn't any comment");
    }
    boolean isThereCodeBeforeComment = false;
    boolean isThereCodeAfterComment = false;
    int commentStartIndex = stringLine.indexOf(comment);
    int commentEndIndex = commentStartIndex + comment.length() - 1;
    if (commentStartIndex > 0) {
      isThereCodeBeforeComment = !StringUtils.isBlank(stringLine.substring(0, commentStartIndex - 1));
    }
    if (commentEndIndex > 0 && commentEndIndex != stringLine.length() - 1) {
      isThereCodeAfterComment = !StringUtils.isBlank(stringLine.substring(commentEndIndex + 1));
    }
    return isThereCodeBeforeComment || isThereCodeAfterComment;
  }

  boolean isThereComment() {
    return comment != null;
  }

  String getComment() {
    return comment;
  }

  boolean isThereBlankComment() {
    if (isThereComment()) {
      for (int i = 0; i < comment.length(); i++) {
        char character = comment.charAt(i);
        if ( !Character.isWhitespace(character) && character != '*' && character != '/') {
          return false;
        }
      }
      return true;
    }
    return false;
  }

  boolean isThereJavadoc() {
    return isThereJavadoc;
  }

  boolean isThereLicenseHeaderComment() {
    return isThereLicenseHeaderComment;
  }

  boolean isThereNoSonarTag() {
    return isThereComment() && comment.contains(NOSONAR_TAG);
  }

  int getLineIndex() {
    return lineIndex;
  }

  void deleteLineContent() {
    comment = null;
    stringLine = null;
  }
}