Coverage Report - com.sdmetrics.metrics.SummationHelper - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
SummationHelper
100%
38/38
100%
24/24
3,8
 
 1  
 /*
 2  
  * SDMetrics Open Core for UML design measurement
 3  
  * Copyright (c) Juergen Wuest
 4  
  * To contact the author, see <http://www.sdmetrics.com/Contact.html>.
 5  
  * 
 6  
  * This file is part of the SDMetrics Open Core.
 7  
  * 
 8  
  * SDMetrics Open Core is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU Affero General Public License as
 10  
  * published by the Free Software Foundation, either version 3 of the
 11  
  * License, or (at your option) any later version.
 12  
     
 13  
  * SDMetrics Open Core is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU Affero General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU Affero General Public License
 19  
  * along with SDMetrics Open Core.  If not, see <http://www.gnu.org/licenses/>.
 20  
  *
 21  
  */
 22  
 package com.sdmetrics.metrics;
 23  
 
 24  
 import com.sdmetrics.math.ExpressionNode;
 25  
 import com.sdmetrics.model.ModelElement;
 26  
 
 27  
 /**
 28  
  * Processes the "sum" and "stat" attributes of metric calculation procedures.
 29  
  */
 30  
 public class SummationHelper {
 31  
         /** Metrics engine for expression evaluations. */
 32  
         private final MetricsEngine engine;
 33  
         /** Sum expression that yields the values to be accumulated. */
 34  
         private final ExpressionNode sumExpression;
 35  
         /** The resulting total. */
 36  86
         private float total = 0;
 37  86
         private boolean processMin = false;
 38  86
         private boolean processMax = false;
 39  86
         private boolean firstCall = true;
 40  
 
 41  
         /**
 42  
          * Constructor.
 43  
          * @param engine metrics engine for expression evaluation
 44  
          * @param attributes Attributes of the calculation procedure to process.
 45  
          * @throws SDMetricsException That "stat" attribute is invalid
 46  
          */
 47  86
         public SummationHelper(MetricsEngine engine, ProcedureAttributes attributes)
 48  
                         throws SDMetricsException {
 49  86
                 this.engine = engine;
 50  86
                 sumExpression = attributes.getExpression("sum");
 51  86
                 String statistic = attributes.getStringValue("stat");
 52  86
                 if (statistic != null) {
 53  4
                         if ("min".equals(statistic)) {
 54  1
                                 processMin = true;
 55  1
                         } else if ("max".equals(statistic)) {
 56  1
                                 processMax = true;
 57  1
                         } else if (!"sum".equals(statistic)) {
 58  2
                                 throw new SDMetricsException(null, null, "Illegal value '"
 59  1
                                                 + statistic + "' for attribute 'stat'.");
 60  
                         }
 61  
                 }
 62  85
         }
 63  
 
 64  
         /**
 65  
          * Processes the "sum" and "stat" attributes for a model element and updates
 66  
          * the total accordingly.
 67  
          * 
 68  
          * @param element The model element to process.
 69  
          * @param vars Variables for the evaluation of expressions
 70  
          * @throws SDMetricsException An error occurred evaluating the sum
 71  
          *         expression.
 72  
          */
 73  
         public void add(ModelElement element, Variables vars)
 74  
                         throws SDMetricsException {
 75  
                 // just register the element if "sum" is not set.
 76  166
                 if (sumExpression == null) {
 77  158
                         total += 1.0;
 78  158
                         return;
 79  
                 }
 80  
 
 81  
                 // evaluate the "sum" expression and update the total
 82  24
                 float sumVal = ((Number) engine
 83  16
                                 .evalExpression(element, sumExpression, vars)).floatValue();
 84  8
                 if (processMax) {
 85  3
                         if (firstCall || sumVal > total) {
 86  2
                                 total = sumVal;
 87  
                         }
 88  2
                 } else if (processMin) {
 89  3
                         if (firstCall || sumVal < total) {
 90  2
                                 total = sumVal;
 91  
                         }
 92  2
                 } else {
 93  2
                         total += sumVal;
 94  
                 }
 95  8
                 firstCall = false;
 96  8
         }
 97  
 
 98  
         /**
 99  
          * Adds a value to the current total.
 100  
          * 
 101  
          * @param value Value to add
 102  
          */
 103  
         public void add(float value) {
 104  20
                 total += value;
 105  20
         }
 106  
 
 107  
         /**
 108  
          * Raises the total to a specified value if it currently is below that
 109  
          * value. Leaves the total unaffected if it is already greater than the
 110  
          * specified value.
 111  
          * 
 112  
          * @param value new total value to raise to
 113  
          */
 114  
         public void raiseTo(float value) {
 115  4
                 if (value > total) {
 116  3
                         total = value;
 117  
                 }
 118  4
         }
 119  
 
 120  
         /**
 121  
          * Gets the current total.
 122  
          * 
 123  
          * @return The current total.
 124  
          */
 125  
         public Number getTotal() {
 126  98
                 return MetricTools.getNumber(total);
 127  
         }
 128  
 }