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     */
020    package org.sonar.api.batch;
021    
022    import org.sonar.api.measures.Measure;
023    import org.sonar.api.measures.MeasureUtils;
024    import org.sonar.api.measures.Metric;
025    import org.sonar.api.resources.Project;
026    import org.sonar.api.resources.Resource;
027    
028    import java.util.Arrays;
029    import 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     */
036    public 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      public boolean shouldExecuteOnProject(Project project) {
064        return true;
065      }
066    
067      /**
068       * {@inheritDoc}
069       */
070      public void decorate(Resource resource, DecoratorContext context) {
071        if (!shouldDecorateResource(context)) {
072          return;
073        }
074        Measure dividend = context.getMeasure(getDividendMetric());
075        Measure divisor = context.getMeasure(getDivisorMetric());
076    
077        if (MeasureUtils.hasValue(dividend) && MeasureUtils.hasValue(divisor) && divisor.getValue() > 0.0) {
078          context.saveMeasure(new Measure(getQuotientMetric(), compute(dividend, divisor, getQuotientMetric().isPercentageType())));
079        }
080      }
081    
082      protected boolean shouldDecorateResource(DecoratorContext context) {
083        return context.getMeasure(getQuotientMetric()) == null;
084      }
085    
086    
087      protected double compute(Measure dividend, Measure divisor, boolean shouldResultBeInPercent) {
088        double result = dividend.getValue() / divisor.getValue();
089        return shouldResultBeInPercent ? result * 100 : result;
090      }
091    
092    }