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  
          * @param engine metrics engine for expression evaluation
 43  
          * @param attributes Attributes of the calculation procedure to process.
 44  
          * @throws SDMetricsException That "stat" attribute is invalid
 45  
          */
 46  86
         public SummationHelper(MetricsEngine engine, ProcedureAttributes attributes)
 47  
                         throws SDMetricsException {
 48  86
                 this.engine = engine;
 49  86
                 sumExpression = attributes.getExpression("sum");
 50  86
                 String statistic = attributes.getStringValue("stat");
 51  86
                 if (statistic != null) {
 52  4
                         if ("min".equals(statistic))
 53  1
                                 processMin = true;
 54  3
                         else if ("max".equals(statistic))
 55  1
                                 processMax = true;
 56  2
                         else if (!"sum".equals(statistic))
 57  2
                                 throw new SDMetricsException(null, null, "Illegal value '"
 58  1
                                                 + statistic + "' for attribute 'stat'.");
 59  
                 }
 60  85
         }
 61  
 
 62  
         /**
 63  
          * Processes the "sum" and "stat" attributes for a model element and updates
 64  
          * the total accordingly.
 65  
          * 
 66  
          * @param element The model element to process.
 67  
          * @param vars Variables for the evaluation of expressions
 68  
          * @throws SDMetricsException An error occurred evaluating the sum
 69  
          *         expression.
 70  
          */
 71  
         public void add(ModelElement element, Variables vars)
 72  
                         throws SDMetricsException {
 73  
                 // just register the element if "sum" is not set.
 74  166
                 if (sumExpression == null) {
 75  158
                         total += 1.0;
 76  158
                         return;
 77  
                 }
 78  
 
 79  
                 // evaluate the "sum" expression and update the total
 80  24
                 float bd = ((Number) engine
 81  16
                                 .evalExpression(element, sumExpression, vars)).floatValue();
 82  8
                 if (processMax) {
 83  3
                         if (firstCall || bd > total)
 84  2
                                 total = bd;
 85  2
                 } else if (processMin) {
 86  3
                         if (firstCall || bd < total)
 87  2
                                 total = bd;
 88  2
                 } else {
 89  2
                         total += bd;
 90  
                 }
 91  8
                 firstCall = false;
 92  8
         }
 93  
 
 94  
         /**
 95  
          * Adds a value to the current total.
 96  
          * 
 97  
          * @param value Value to add
 98  
          */
 99  
         public void add(float value) {
 100  20
                 total += value;
 101  20
         }
 102  
 
 103  
         /**
 104  
          * Raises the total to a specified value if it currently is below that
 105  
          * value. Leaves the total unaffected if it is already greater than the
 106  
          * specified value.
 107  
          * 
 108  
          * @param value new total value to raise to
 109  
          */
 110  
         public void raiseTo(float value) {
 111  4
                 if (value > total)
 112  3
                         total = value;
 113  4
         }
 114  
 
 115  
         /**
 116  
          * Gets the current total.
 117  
          * 
 118  
          * @return The current total.
 119  
          */
 120  
         public Number getTotal() {
 121  98
                 return MetricTools.getNumber(total);
 122  
         }
 123  
 }