View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin.beans;
2   
3   /*
4    * Copyright 2007 Matthew Beermann
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.text.NumberFormat;
20  import java.util.Date;
21  
22  /**
23   * @author <a href="mbeerman@yahoo.com">Matthew Beermann</a>
24   */
25  public class CloverReportBean extends AbstractReportBean
26  {
27      private int conditionals, statements, methods, elements;
28  
29      private int coveredConditionals, coveredStatements, coveredMethods, coveredElements;
30  
31      private static final NumberFormat FormatPercent = NumberFormat.getPercentInstance();
32      static
33      {
34          FormatPercent.setMaximumFractionDigits( 1 );
35      }
36  
37      /**
38       * Construct a new CloverReportBean against the given project.
39       */
40      public CloverReportBean()
41      {
42  
43      }
44  
45      /**
46       * 
47       * @param dateGeneration
48       */
49      public CloverReportBean( Date dateGeneration )
50      {
51          super( dateGeneration );
52  
53      }
54  
55      /**
56       * @return the conditionals
57       */
58      public int getConditionals()
59      {
60          return conditionals;
61      }
62  
63      /**
64       * @return a formatted version of the conditionals
65       */
66      public String getConditionalsLabel()
67      {
68          return getPercentage( coveredConditionals, conditionals ) + " (" + coveredConditionals + " / " + conditionals
69                          + ")";
70      }
71  
72      /**
73       * @param conditionals
74       *            the conditionals to set
75       */
76      public void setConditionals( int conditionals )
77      {
78          this.conditionals = conditionals;
79      }
80  
81      /**
82       * @return the coveredConditionals
83       */
84      public int getCoveredConditionals()
85      {
86          return coveredConditionals;
87      }
88  
89      /**
90       * @param coveredConditionals
91       *            the coveredConditionals to set
92       */
93      public void setCoveredConditionals( int coveredConditionals )
94      {
95          this.coveredConditionals = coveredConditionals;
96      }
97  
98      /**
99       * @return the coveredElements
100      */
101     public int getCoveredElements()
102     {
103         return coveredElements;
104     }
105 
106     /**
107      * @param coveredElements
108      *            the coveredElements to set
109      */
110     public void setCoveredElements( int coveredElements )
111     {
112         this.coveredElements = coveredElements;
113     }
114 
115     /**
116      * @return the coveredMethods
117      */
118     public int getCoveredMethods()
119     {
120         return coveredMethods;
121     }
122 
123     /**
124      * @param coveredMethods
125      *            the coveredMethods to set
126      */
127     public void setCoveredMethods( int coveredMethods )
128     {
129         this.coveredMethods = coveredMethods;
130     }
131 
132     /**
133      * @return the coveredStatements
134      */
135     public int getCoveredStatements()
136     {
137         return coveredStatements;
138     }
139 
140     /**
141      * @param coveredStatements
142      *            the coveredStatements to set
143      */
144     public void setCoveredStatements( int coveredStatements )
145     {
146         this.coveredStatements = coveredStatements;
147     }
148 
149     /**
150      * @return the elements
151      */
152     public int getElements()
153     {
154         return elements;
155     }
156 
157     /**
158      * @return a formatted version of the elements
159      */
160     public String getElementsLabel()
161     {
162         return getPercentage( coveredElements, elements ) + " (" + coveredElements + " / " + elements + ")";
163     }
164 
165     /**
166      * @param elements
167      *            the elements to set
168      */
169     public void setElements( int elements )
170     {
171         this.elements = elements;
172     }
173 
174     /**
175      * @return the methods
176      */
177     public int getMethods()
178     {
179         return methods;
180     }
181 
182     /**
183      * @return a formatted version of the methods
184      */
185     public String getMethodsLabel()
186     {
187         return getPercentage( coveredMethods, methods ) + " (" + coveredMethods + " / " + methods + ")";
188     }
189 
190     /**
191      * @param methods
192      *            the methods to set
193      */
194     public void setMethods( int methods )
195     {
196         this.methods = methods;
197     }
198 
199     /**
200      * @return the statements
201      */
202     public int getStatements()
203     {
204         return statements;
205     }
206 
207     /**
208      * @return a formatted version of the statements
209      */
210     public String getStatementsLabel()
211     {
212         return getPercentage( coveredStatements, statements ) + " (" + coveredStatements + " / " + statements + ")";
213     }
214 
215     /**
216      * @param statements
217      *            the statements to set
218      */
219     public void setStatements( int statements )
220     {
221         this.statements = statements;
222     }
223 
224     /**
225      * @param dashboardReport
226      */
227     public void merge( IDashBoardReportBean dashboardReport )
228     {
229         if ( dashboardReport != null && dashboardReport instanceof CloverReportBean )
230         {
231             conditionals += ( (CloverReportBean) dashboardReport ).getConditionals();
232 
233             statements += ( (CloverReportBean) dashboardReport ).getStatements();
234 
235             methods += ( (CloverReportBean) dashboardReport ).getMethods();
236 
237             elements += ( (CloverReportBean) dashboardReport ).getElements();
238 
239             coveredConditionals += ( (CloverReportBean) dashboardReport ).getCoveredConditionals();
240 
241             coveredStatements += ( (CloverReportBean) dashboardReport ).getCoveredStatements();
242 
243             coveredMethods += ( (CloverReportBean) dashboardReport ).getCoveredMethods();
244 
245             coveredElements += ( (CloverReportBean) dashboardReport ).getCoveredElements();
246         }
247     }
248 
249     private String getPercentage( int numerator, int denominator )
250     {
251         String percent = "0%";
252         if ( denominator > 0 )
253         {
254             double percentage = numerator / (double) denominator;
255             percent = FormatPercent.format( percentage );
256         }
257         return percent;
258     }
259 
260     public double getPercentCoveredStatements()
261     {
262         return getPercentageValue( this.coveredStatements, this.statements );
263     }
264 
265     public double getPercentCoveredConditionals()
266     {
267         return getPercentageValue( this.coveredConditionals, this.conditionals );
268     }
269 
270     public double getPercentCoveredElements()
271     {
272         return getPercentageValue( this.coveredElements, this.elements );
273     }
274 
275     public double getPercentCoveredMethods()
276     {
277         return getPercentageValue( this.coveredMethods, this.methods );
278     }
279 
280     public double getPercentUnCoveredStatements()
281     {
282         int uncovered = this.statements - this.coveredStatements;
283         return getPercentageValue( uncovered, this.statements );
284     }
285 
286     public double getPercentUnCoveredConditionals()
287     {
288         int uncovered = this.conditionals - this.coveredConditionals;
289         return getPercentageValue( uncovered, this.conditionals );
290     }
291 
292     public double getPercentUnCoveredElements()
293     {
294         int uncovered = this.elements - this.coveredElements;
295         return getPercentageValue( uncovered, this.elements );
296     }
297 
298     public double getPercentUnCoveredMethods()
299     {
300         int uncovered = this.methods - this.coveredMethods;
301         return getPercentageValue( uncovered, this.methods );
302     }
303 }