001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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.batch;
021
022import org.sonar.api.measures.Measure;
023import org.sonar.api.measures.MeasureUtils;
024import org.sonar.api.measures.Metric;
025import org.sonar.api.resources.Project;
026import org.sonar.api.resources.Resource;
027
028import java.util.Arrays;
029import java.util.List;
030
031/**
032 * A pre-implementation to decorate metrics that are the result of a division
033 *
034 * @since 1.10
035 */
036public abstract class AbstractDivisionDecorator implements Decorator {
037
038  protected abstract Metric getQuotientMetric();
039
040  protected abstract Metric getDivisorMetric();
041
042  protected abstract Metric getDividendMetric();
043
044  /**
045   * Used to define upstream dependencies
046   */
047  @DependsUpon
048  public List<Metric> dependsUponMetrics() {
049    return Arrays.asList(getDividendMetric(), getDivisorMetric());
050  }
051
052  /**
053   * Used to define downstream dependencies
054   */
055  @DependedUpon
056  public Metric generatesMetric() {
057    return getQuotientMetric();
058  }
059
060  /**
061   * {@inheritDoc}
062   */
063  @Override
064  public boolean shouldExecuteOnProject(Project project) {
065    return true;
066  }
067
068  /**
069   * {@inheritDoc}
070   */
071  @Override
072  public void decorate(Resource resource, DecoratorContext context) {
073    if (!shouldDecorateResource(context)) {
074      return;
075    }
076    Measure dividend = context.getMeasure(getDividendMetric());
077    Measure divisor = context.getMeasure(getDivisorMetric());
078
079    if (MeasureUtils.hasValue(dividend) && MeasureUtils.hasValue(divisor) && divisor.getValue() > 0.0) {
080      context.saveMeasure(new Measure(getQuotientMetric(), compute(dividend, divisor, getQuotientMetric().isPercentageType())));
081    }
082  }
083
084  protected boolean shouldDecorateResource(DecoratorContext context) {
085    return context.getMeasure(getQuotientMetric()) == null;
086  }
087
088
089  protected double compute(Measure dividend, Measure divisor, boolean shouldResultBeInPercent) {
090    double result = dividend.getValue() / divisor.getValue();
091    return shouldResultBeInPercent ? result * 100 : result;
092  }
093
094}