001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 * Sonar 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 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.api.batch; 021 022 import org.sonar.api.measures.FormulaData; 023 import org.sonar.api.measures.Measure; 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.List; 029 030 /** 031 * A pre-implementation of a decorator using a simple calculation formula 032 * @since 1.11 033 */ 034 public class FormulaDecorator implements Decorator { 035 036 private Metric metric; 037 private DefaultFormulaContext formulaContext; 038 039 /** 040 * Creates a FormulaDecorator 041 * 042 * @param metric the metric should have an associated formula 043 * 044 * @throws IllegalArgumentException if no formula is associated to the metric 045 */ 046 public FormulaDecorator(Metric metric) { 047 if (metric.getFormula() == null) { 048 throw new IllegalArgumentException("No formula defined on metric"); 049 } 050 this.metric = metric; 051 this.formulaContext = new DefaultFormulaContext(metric); 052 } 053 054 /** 055 * {@inheritDoc} 056 */ 057 public boolean shouldExecuteOnProject(Project project) { 058 return true; 059 } 060 061 /** 062 * @return metric generated by the decorator 063 */ 064 @DependedUpon 065 public Metric generatesMetric() { 066 return metric; 067 } 068 069 /** 070 * @return metric the decorator depends upon 071 */ 072 @DependsUpon 073 public List<Metric> dependsUponMetrics() { 074 return metric.getFormula().dependsUponMetrics(); 075 } 076 077 /** 078 * {@inheritDoc} 079 */ 080 public void decorate(Resource resource, DecoratorContext context) { 081 if (context.getMeasure(metric) != null) { 082 return; 083 } 084 085 formulaContext.setDecoratorContext(context); 086 FormulaData data = new DefaultFormulaData(context); 087 Measure measure = metric.getFormula().calculate(data, formulaContext); 088 if (measure != null) { 089 context.saveMeasure(measure); 090 } 091 } 092 093 @Override 094 public boolean equals(Object o) { 095 if (this == o) { 096 return true; 097 } 098 if (o == null || getClass() != o.getClass()) { 099 return false; 100 } 101 102 FormulaDecorator that = (FormulaDecorator) o; 103 104 if (metric != null ? !metric.equals(that.metric) : that.metric != null) { 105 return false; 106 } 107 return true; 108 } 109 110 @Override 111 public int hashCode() { 112 return metric != null ? metric.hashCode() : 0; 113 } 114 115 @Override 116 public String toString() { 117 return new StringBuilder().append("f(").append(metric).append(")").toString(); 118 } 119 }