001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.ce.measure.test;
021
022import com.google.common.collect.ImmutableSet;
023import java.util.List;
024import java.util.Set;
025import org.sonar.api.ce.measure.MeasureComputer;
026import org.sonar.api.measures.CoreMetrics;
027import org.sonar.api.measures.Metric;
028
029import static com.google.common.base.Preconditions.checkArgument;
030import static java.util.Arrays.asList;
031import static java.util.Objects.requireNonNull;
032
033public class TestMeasureComputerDefinition implements MeasureComputer.MeasureComputerDefinition {
034
035  private final Set<String> inputMetricKeys;
036  private final Set<String> outputMetrics;
037
038  private TestMeasureComputerDefinition(MeasureComputerDefinitionBuilderImpl builder) {
039    this.inputMetricKeys = ImmutableSet.copyOf(builder.inputMetricKeys);
040    this.outputMetrics = ImmutableSet.copyOf(builder.outputMetrics);
041  }
042
043  @Override
044  public Set<String> getInputMetrics() {
045    return inputMetricKeys;
046  }
047
048  @Override
049  public Set<String> getOutputMetrics() {
050    return outputMetrics;
051  }
052
053  public static class MeasureComputerDefinitionBuilderImpl implements Builder {
054
055    private String[] inputMetricKeys = new String[] {};
056    private String[] outputMetrics;
057
058    @Override
059    public Builder setInputMetrics(String... inputMetrics) {
060      this.inputMetricKeys = validateInputMetricKeys(inputMetrics);
061      return this;
062    }
063
064    @Override
065    public Builder setOutputMetrics(String... outputMetrics) {
066      this.outputMetrics = validateOutputMetricKeys(outputMetrics);
067      return this;
068    }
069
070    @Override
071    public MeasureComputer.MeasureComputerDefinition build() {
072      validateInputMetricKeys(this.inputMetricKeys);
073      validateOutputMetricKeys(this.outputMetrics);
074      return new TestMeasureComputerDefinition(this);
075    }
076
077    private static String[] validateInputMetricKeys(String[] inputMetrics) {
078      requireNonNull(inputMetrics, "Input metrics cannot be null");
079      checkNotNull(inputMetrics);
080      return inputMetrics;
081    }
082
083    private static String[] validateOutputMetricKeys(String[] outputMetrics) {
084      requireNonNull(outputMetrics, "Output metrics cannot be null");
085      checkArgument(outputMetrics.length > 0, "At least one output metric must be defined");
086
087      List<String> outputMetricKeys = asList(outputMetrics);
088      CoreMetrics.getMetrics().stream()
089        .map(Metric::getKey)
090        .forEach(metricKey -> checkArgument(!outputMetricKeys.contains(metricKey), "Core metrics are not allowed"));
091      checkNotNull(outputMetrics);
092      return outputMetrics;
093    }
094
095    private static void checkNotNull(String[] metrics) {
096      for (String metric : metrics) {
097        requireNonNull(metric, "Null metric is not allowed");
098      }
099    }
100  }
101}