3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.component;
22 import javax.annotation.CheckForNull;
23 import javax.annotation.Nullable;
24 import javax.annotation.concurrent.Immutable;
26 import static com.google.common.base.Preconditions.checkArgument;
27 import static org.apache.commons.lang.StringUtils.abbreviate;
28 import static org.apache.commons.lang.StringUtils.trimToNull;
29 import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_NAME_LENGTH;
32 * The attributes specific to a Component of type {@link Component.Type#FILE}.
35 public class FileAttributes {
36 private final boolean unitTest;
38 private final String languageKey;
39 private final boolean markedAsUnchanged;
40 private final int lines;
41 private String oldRelativePath;
43 public FileAttributes(boolean unitTest, @Nullable String languageKey, int lines) {
44 this(unitTest, languageKey, lines, false, null);
47 public FileAttributes(boolean unitTest, @Nullable String languageKey, int lines, boolean markedAsUnchanged, @Nullable String oldRelativePath) {
48 this.unitTest = unitTest;
49 this.languageKey = languageKey;
50 this.markedAsUnchanged = markedAsUnchanged;
51 checkArgument(lines > 0, "Number of lines must be greater than zero");
53 this.oldRelativePath = formatOldRelativePath(oldRelativePath);
56 public boolean isMarkedAsUnchanged() {
57 return markedAsUnchanged;
60 public boolean isUnitTest() {
65 public String getLanguageKey() {
70 * The old relative path of a file when a move is detected by the SCM in the scope of a Pull Request.
73 public String getOldRelativePath() {
74 return oldRelativePath;
78 * Number of lines of the file, can never be less than 1
80 public int getLines() {
85 public String toString() {
86 return "FileAttributes{" +
87 "languageKey='" + languageKey + '\'' +
88 ", unitTest=" + unitTest +
90 ", markedAsUnchanged=" + markedAsUnchanged +
91 ", oldRelativePath='" + oldRelativePath + '\'' +
95 private static String formatOldRelativePath(@Nullable String path) {
96 return abbreviate(trimToNull(path), MAX_COMPONENT_NAME_LENGTH);