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 java.util.HashSet;
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.Collections.unmodifiableSet;
032import static java.util.Objects.requireNonNull;
033
034public class TestMeasureComputerDefinition implements MeasureComputer.MeasureComputerDefinition {
035
036  private final Set<String> inputMetricKeys;
037  private final Set<String> outputMetrics;
038
039  private TestMeasureComputerDefinition(MeasureComputerDefinitionBuilderImpl builder) {
040    this.inputMetricKeys = unmodifiableSet(new HashSet<>(asList(builder.inputMetricKeys)));
041    this.outputMetrics = unmodifiableSet(new HashSet<>(asList(builder.outputMetrics)));
042  }
043
044  @Override
045  public Set<String> getInputMetrics() {
046    return inputMetricKeys;
047  }
048
049  @Override
050  public Set<String> getOutputMetrics() {
051    return outputMetrics;
052  }
053
054  public static class MeasureComputerDefinitionBuilderImpl implements Builder {
055
056    private String[] inputMetricKeys = new String[] {};
057    private String[] outputMetrics;
058
059    @Override
060    public Builder setInputMetrics(String... inputMetrics) {
061      this.inputMetricKeys = validateInputMetricKeys(inputMetrics);
062      return this;
063    }
064
065    @Override
066    public Builder setOutputMetrics(String... outputMetrics) {
067      this.outputMetrics = validateOutputMetricKeys(outputMetrics);
068      return this;
069    }
070
071    @Override
072    public MeasureComputer.MeasureComputerDefinition build() {
073      validateInputMetricKeys(this.inputMetricKeys);
074      validateOutputMetricKeys(this.outputMetrics);
075      return new TestMeasureComputerDefinition(this);
076    }
077
078    private static String[] validateInputMetricKeys(String[] inputMetrics) {
079      requireNonNull(inputMetrics, "Input metrics cannot be null");
080      checkNotNull(inputMetrics);
081      return inputMetrics;
082    }
083
084    private static String[] validateOutputMetricKeys(String[] outputMetrics) {
085      requireNonNull(outputMetrics, "Output metrics cannot be null");
086      checkArgument(outputMetrics.length > 0, "At least one output metric must be defined");
087
088      List<String> outputMetricKeys = asList(outputMetrics);
089      CoreMetrics.getMetrics().stream()
090        .map(Metric::getKey)
091        .forEach(metricKey -> checkArgument(!outputMetricKeys.contains(metricKey), "Core metrics are not allowed"));
092      checkNotNull(outputMetrics);
093      return outputMetrics;
094    }
095
096    private static void checkNotNull(String[] metrics) {
097      for (String metric : metrics) {
098        requireNonNull(metric, "Null metric is not allowed");
099      }
100    }
101  }
102}