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.

TreeRootHolderImpl.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 com.google.common.collect.ImmutableMap;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.Optional;
  25. import javax.annotation.CheckForNull;
  26. import static com.google.common.base.Preconditions.checkState;
  27. import static java.util.Objects.requireNonNull;
  28. import static org.apache.commons.lang3.StringUtils.isNotBlank;
  29. import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
  30. /**
  31. * Holds the reference to the root of the {@link Component} tree for the current CE run.
  32. */
  33. public class TreeRootHolderImpl implements MutableTreeRootHolder {
  34. @CheckForNull
  35. private Map<Integer, Component> componentsByRef = null;
  36. @CheckForNull
  37. private Map<Integer, Component> extendedComponentsByRef = null;
  38. @CheckForNull
  39. private Map<String, Component> componentsByUuid = null;
  40. private int size = 0;
  41. private Component root = null;
  42. private Component extendedTreeRoot = null;
  43. @Override
  44. public boolean isEmpty() {
  45. return this.root == null;
  46. }
  47. @Override
  48. public MutableTreeRootHolder setRoots(Component root, Component reportRoot) {
  49. checkState(this.root == null, "root can not be set twice in holder");
  50. this.root = requireNonNull(root, "root can not be null");
  51. this.extendedTreeRoot = requireNonNull(reportRoot, "extended tree root can not be null");
  52. return this;
  53. }
  54. @Override
  55. public Component getRoot() {
  56. checkInitialized();
  57. return this.root;
  58. }
  59. @Override
  60. public Component getReportTreeRoot() {
  61. checkInitialized();
  62. return this.extendedTreeRoot;
  63. }
  64. @Override
  65. public Component getComponentByRef(int ref) {
  66. return getOptionalComponentByRef(ref)
  67. .orElseThrow(() -> new IllegalArgumentException(String.format("Component with ref '%s' can't be found", ref)));
  68. }
  69. @Override
  70. public Component getComponentByUuid(String uuid) {
  71. checkInitialized();
  72. ensureComponentByRefAndUuidArePopulated();
  73. return componentsByUuid.get(uuid);
  74. }
  75. @Override
  76. public Optional<Component> getOptionalComponentByRef(int ref) {
  77. checkInitialized();
  78. ensureComponentByRefAndUuidArePopulated();
  79. return Optional.ofNullable(componentsByRef.get(ref));
  80. }
  81. @Override
  82. public Component getReportTreeComponentByRef(int ref) {
  83. checkInitialized();
  84. ensureExtendedComponentByRefIsPopulated();
  85. Component c = extendedComponentsByRef.get(ref);
  86. if (c == null) {
  87. throw new IllegalArgumentException(String.format("Component with ref '%s' can't be found", ref));
  88. }
  89. return c;
  90. }
  91. @Override
  92. public int getSize() {
  93. checkInitialized();
  94. ensureComponentByRefAndUuidArePopulated();
  95. return size;
  96. }
  97. private void ensureExtendedComponentByRefIsPopulated() {
  98. if (extendedComponentsByRef != null) {
  99. return;
  100. }
  101. final ImmutableMap.Builder<Integer, Component> builder = ImmutableMap.builder();
  102. new DepthTraversalTypeAwareCrawler(
  103. new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
  104. @Override
  105. public void visitAny(Component component) {
  106. if (component.getReportAttributes().getRef() != null) {
  107. builder.put(component.getReportAttributes().getRef(), component);
  108. }
  109. }
  110. }).visit(this.extendedTreeRoot);
  111. this.extendedComponentsByRef = builder.build();
  112. }
  113. private void ensureComponentByRefAndUuidArePopulated() {
  114. if (componentsByRef != null && componentsByUuid != null) {
  115. return;
  116. }
  117. final ImmutableMap.Builder<Integer, Component> builderByRef = ImmutableMap.builder();
  118. final Map<String, Component> builderByUuid = new HashMap<>();
  119. new DepthTraversalTypeAwareCrawler(
  120. new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {
  121. @Override
  122. public void visitAny(Component component) {
  123. size++;
  124. if (component.getReportAttributes().getRef() != null) {
  125. builderByRef.put(component.getReportAttributes().getRef(), component);
  126. }
  127. if (isNotBlank(component.getUuid())) {
  128. builderByUuid.put(component.getUuid(), component);
  129. }
  130. }
  131. }).visit(this.root);
  132. this.componentsByRef = builderByRef.build();
  133. this.componentsByUuid = ImmutableMap.copyOf(builderByUuid);
  134. }
  135. private void checkInitialized() {
  136. checkState(this.root != null, "Holder has not been initialized yet");
  137. }
  138. }