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.

MeasureComputerDefinitionImpl.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 com.google.common.collect.ImmutableSet;
  22. import java.util.Set;
  23. import javax.annotation.CheckForNull;
  24. import javax.annotation.Nullable;
  25. import org.sonar.api.ce.measure.MeasureComputer;
  26. import static com.google.common.base.Preconditions.checkArgument;
  27. import static java.util.Objects.requireNonNull;
  28. public class MeasureComputerDefinitionImpl implements MeasureComputer.MeasureComputerDefinition {
  29. private final Set<String> inputMetricKeys;
  30. private final Set<String> outputMetrics;
  31. private MeasureComputerDefinitionImpl(BuilderImpl builder) {
  32. this.inputMetricKeys = ImmutableSet.copyOf(builder.inputMetricKeys);
  33. this.outputMetrics = ImmutableSet.copyOf(builder.outputMetrics);
  34. }
  35. @Override
  36. public Set<String> getInputMetrics() {
  37. return inputMetricKeys;
  38. }
  39. @Override
  40. public Set<String> getOutputMetrics() {
  41. return outputMetrics;
  42. }
  43. @Override
  44. public boolean equals(Object o) {
  45. if (this == o) {
  46. return true;
  47. }
  48. if (o == null || getClass() != o.getClass()) {
  49. return false;
  50. }
  51. MeasureComputerDefinitionImpl that = (MeasureComputerDefinitionImpl) o;
  52. if (!inputMetricKeys.equals(that.inputMetricKeys)) {
  53. return false;
  54. }
  55. return outputMetrics.equals(that.outputMetrics);
  56. }
  57. @Override
  58. public int hashCode() {
  59. int result = inputMetricKeys.hashCode();
  60. result = 31 * result + outputMetrics.hashCode();
  61. return result;
  62. }
  63. @Override
  64. public String toString() {
  65. return "MeasureComputerDefinitionImpl{" +
  66. "inputMetricKeys=" + inputMetricKeys +
  67. ", outputMetrics=" + outputMetrics +
  68. '}';
  69. }
  70. public static class BuilderImpl implements Builder {
  71. private String[] inputMetricKeys = new String[] {};
  72. @CheckForNull
  73. private String[] outputMetrics;
  74. @Override
  75. public Builder setInputMetrics(String... inputMetrics) {
  76. this.inputMetricKeys = validateInputMetricKeys(inputMetrics);
  77. return this;
  78. }
  79. @Override
  80. public Builder setOutputMetrics(String... outputMetrics) {
  81. this.outputMetrics = validateOutputMetricKeys(outputMetrics);
  82. return this;
  83. }
  84. @Override
  85. public MeasureComputer.MeasureComputerDefinition build() {
  86. validateInputMetricKeys(this.inputMetricKeys);
  87. validateOutputMetricKeys(this.outputMetrics);
  88. return new MeasureComputerDefinitionImpl(this);
  89. }
  90. private static String[] validateInputMetricKeys(@Nullable String[] inputMetrics) {
  91. requireNonNull(inputMetrics, "Input metrics cannot be null");
  92. checkNotNull(inputMetrics);
  93. return inputMetrics;
  94. }
  95. private static String[] validateOutputMetricKeys(@Nullable String[] outputMetrics) {
  96. requireNonNull(outputMetrics, "Output metrics cannot be null");
  97. checkArgument(outputMetrics.length > 0, "At least one output metric must be defined");
  98. checkNotNull(outputMetrics);
  99. return outputMetrics;
  100. }
  101. private static void checkNotNull(String[] metrics) {
  102. for (String metric : metrics) {
  103. requireNonNull(metric, "Null metric is not allowed");
  104. }
  105. }
  106. }
  107. }