You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileAttributes.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.ce.task.projectanalysis.component;
  21. import javax.annotation.CheckForNull;
  22. import javax.annotation.Nullable;
  23. import javax.annotation.concurrent.Immutable;
  24. import static com.google.common.base.Preconditions.checkArgument;
  25. import static org.apache.commons.lang3.StringUtils.abbreviate;
  26. import static org.apache.commons.lang3.StringUtils.trimToNull;
  27. import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_NAME_LENGTH;
  28. /**
  29. * The attributes specific to a Component of type {@link Component.Type#FILE}.
  30. */
  31. @Immutable
  32. public class FileAttributes {
  33. private final boolean unitTest;
  34. @CheckForNull
  35. private final String languageKey;
  36. private final boolean markedAsUnchanged;
  37. private final int lines;
  38. private String oldRelativePath;
  39. public FileAttributes(boolean unitTest, @Nullable String languageKey, int lines) {
  40. this(unitTest, languageKey, lines, false, null);
  41. }
  42. public FileAttributes(boolean unitTest, @Nullable String languageKey, int lines, boolean markedAsUnchanged, @Nullable String oldRelativePath) {
  43. this.unitTest = unitTest;
  44. this.languageKey = languageKey;
  45. this.markedAsUnchanged = markedAsUnchanged;
  46. checkArgument(lines > 0, "Number of lines must be greater than zero");
  47. this.lines = lines;
  48. this.oldRelativePath = formatOldRelativePath(oldRelativePath);
  49. }
  50. public boolean isMarkedAsUnchanged() {
  51. return markedAsUnchanged;
  52. }
  53. public boolean isUnitTest() {
  54. return unitTest;
  55. }
  56. @CheckForNull
  57. public String getLanguageKey() {
  58. return languageKey;
  59. }
  60. /**
  61. * The old relative path of a file when a move is detected by the SCM in the scope of a Pull Request.
  62. */
  63. @CheckForNull
  64. public String getOldRelativePath() {
  65. return oldRelativePath;
  66. }
  67. /**
  68. * Number of lines of the file, can never be less than 1
  69. */
  70. public int getLines() {
  71. return lines;
  72. }
  73. @Override
  74. public String toString() {
  75. return "FileAttributes{" +
  76. "languageKey='" + languageKey + '\'' +
  77. ", unitTest=" + unitTest +
  78. ", lines=" + lines +
  79. ", markedAsUnchanged=" + markedAsUnchanged +
  80. ", oldRelativePath='" + oldRelativePath + '\'' +
  81. '}';
  82. }
  83. private static String formatOldRelativePath(@Nullable String path) {
  84. return abbreviate(trimToNull(path), MAX_COMPONENT_NAME_LENGTH);
  85. }
  86. }