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 java.util.List;
023import org.sonar.api.measures.Measure;
024import org.sonar.api.measures.MeasureUtils;
025import org.sonar.api.measures.Metric;
026import org.sonar.api.resources.Project;
027import org.sonar.api.resources.Resource;
028
029/**
030 * Sum measures of child resources.
031 *
032 * @since 1.10
033 * @deprecated since 5.2 there's no more decorator on batch side
034 */
035@Deprecated
036public abstract class AbstractSumChildrenDecorator implements Decorator {
037
038
039  /**
040   * Each metric is used individually. There are as many generated measures than metrics.
041   * <p/>
042   * <p><b>Important</b> : annotations are not inherited, so you have to copy the @DependedUpon annotation
043   * when implementing this method.</p>
044   *
045   * @return not null list of metrics
046   */
047  @DependedUpon
048  public abstract List<Metric> generatesMetrics();
049
050  /**
051   * {@inheritDoc}
052   */
053  @Override
054  public boolean shouldExecuteOnProject(Project project) {
055    return true;
056  }
057
058  /**
059   * @return whether it should save zero if no child measures
060   */
061  protected abstract boolean shouldSaveZeroIfNoChildMeasures();
062
063  /**
064   * {@inheritDoc}
065   */
066  @Override
067  public void decorate(Resource resource, DecoratorContext context) {
068    if (!shouldDecorateResource(resource)) {
069      return;
070    }
071    for (Metric metric : generatesMetrics()) {
072      if (context.getMeasure(metric) == null) {
073        Double sum = MeasureUtils.sum(shouldSaveZeroIfNoChildMeasures(), context.getChildrenMeasures(metric));
074        if (sum != null) {
075          context.saveMeasure(new Measure(metric, sum));
076        }
077      }
078    }
079  }
080
081  /**
082   * @return whether the resource should be decorated or not
083   */
084  public boolean shouldDecorateResource(Resource resource) {
085    return true;
086  }
087
088  @Override
089  public String toString() {
090    return getClass().getSimpleName();
091  }
092}