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.

ComponentImpl.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.api.measurecomputer;
  21. import javax.annotation.CheckForNull;
  22. import javax.annotation.Nullable;
  23. import javax.annotation.concurrent.Immutable;
  24. import org.sonar.api.ce.measure.Component;
  25. import static com.google.common.base.Preconditions.checkState;
  26. import static java.util.Objects.requireNonNull;
  27. @Immutable
  28. public class ComponentImpl implements Component {
  29. private final String key;
  30. private final Type type;
  31. @CheckForNull
  32. private final FileAttributes fileAttributes;
  33. public ComponentImpl(String key, Type type, @Nullable FileAttributes fileAttributes) {
  34. this.key = requireNonNull(key, "Key cannot be null");
  35. this.type = requireNonNull(type, "Type cannot be null");
  36. this.fileAttributes = checkFileAttributes(fileAttributes);
  37. }
  38. @CheckForNull
  39. private FileAttributes checkFileAttributes(@Nullable FileAttributes fileAttributes) {
  40. if (fileAttributes == null && type == Type.FILE) {
  41. throw new IllegalArgumentException("Component of type FILE must have a FileAttributes object");
  42. } else if (fileAttributes != null && type != Type.FILE) {
  43. throw new IllegalArgumentException("Only component of type FILE have a FileAttributes object");
  44. }
  45. return fileAttributes;
  46. }
  47. @Override
  48. public Type getType() {
  49. return type;
  50. }
  51. @Override
  52. public String getKey() {
  53. return key;
  54. }
  55. @Override
  56. public FileAttributes getFileAttributes() {
  57. checkState(this.type == Component.Type.FILE, "Only component of type FILE have a FileAttributes object");
  58. return fileAttributes;
  59. }
  60. @Override
  61. public boolean equals(Object o) {
  62. if (this == o) {
  63. return true;
  64. }
  65. if (o == null || getClass() != o.getClass()) {
  66. return false;
  67. }
  68. ComponentImpl component = (ComponentImpl) o;
  69. return key.equals(component.key);
  70. }
  71. @Override
  72. public int hashCode() {
  73. return key.hashCode();
  74. }
  75. @Override
  76. public String toString() {
  77. return "ComponentImpl{" +
  78. "key=" + key +
  79. ", type='" + type + '\'' +
  80. ", fileAttributes=" + fileAttributes +
  81. '}';
  82. }
  83. @Immutable
  84. public static class FileAttributesImpl implements FileAttributes {
  85. private final boolean unitTest;
  86. private final String languageKey;
  87. public FileAttributesImpl(@Nullable String languageKey, boolean unitTest) {
  88. this.languageKey = languageKey;
  89. this.unitTest = unitTest;
  90. }
  91. @Override
  92. public boolean isUnitTest() {
  93. return unitTest;
  94. }
  95. @Override
  96. @CheckForNull
  97. public String getLanguageKey() {
  98. return languageKey;
  99. }
  100. @Override
  101. public String toString() {
  102. return "FileAttributesImpl{" +
  103. "languageKey='" + languageKey + '\'' +
  104. ", unitTest=" + unitTest +
  105. '}';
  106. }
  107. }
  108. }