Coverage Report - com.sdmetrics.metrics.SetSummationHelper - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
SetSummationHelper
100%
23/23
100%
6/6
1,6
 
 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 java.util.Collection;
 25  
 
 26  
 import com.sdmetrics.math.ExpressionNode;
 27  
 import com.sdmetrics.model.ModelElement;
 28  
 
 29  
 /**
 30  
  * Helps set calculation procedures to build up their cumulative result sets.
 31  
  */
 32  
 public class SetSummationHelper {
 33  
 
 34  
         /** Expression that yields the sets to be accumulated. */
 35  
         private final ExpressionNode cumulativeSetExpression;
 36  
         /** Expression that yields values to be accumulated in a set. */
 37  
         private final ExpressionNode valueSetExpression;
 38  
         /** The resulting cumulative set. */
 39  
         @SuppressWarnings("rawtypes")
 40  
         private final Collection resultSet;
 41  
         /** Indicates if the principal must be removed from the result set. */
 42  
         private final boolean excludeSelf;
 43  
         /** Metrics engine to use for evaluations of expressions. */
 44  
         private final MetricsEngine engine;
 45  
 
 46  
         /**
 47  
          * Constructor.
 48  
          * @param engine metrics engine for expression evaluation
 49  
          * @param set The definition of the set to process
 50  
          * @param setAttributeName The name of the attribute defining the sets to
 51  
          *        accumulate.
 52  
          * @throws SDMetricsException Summation attributes have illegal values
 53  
          */
 54  130
         public SetSummationHelper(MetricsEngine engine, Set set,
 55  
                         String setAttributeName) throws SDMetricsException {
 56  130
                 this.engine = engine;
 57  130
                 ProcedureAttributes attributes = set.getAttributes();
 58  130
                 cumulativeSetExpression = attributes.getExpression(setAttributeName);
 59  130
                 valueSetExpression = attributes.getExpression("valueset");
 60  130
                 excludeSelf = attributes.getBooleanValue("exclude_self", false);
 61  
 
 62  130
                 resultSet = MetricTools.createHashSet(set.isMultiSet());
 63  130
         }
 64  
 
 65  
         /**
 66  
          * Processes the cumulative or valueset expression for a model element and
 67  
          * adds its contents to the result set.
 68  
          * <p>
 69  
          * <ul>
 70  
          * <li>If the "valueset" attribute is set, it is evaluated for the model
 71  
          * element and the expression result added to the result set.</li>
 72  
          * <li>If the cumulative set expression is set, it is evaluated and its
 73  
          * contents added to the result set.
 74  
          * <li>Otherwise, the model element itself is added to the result set.
 75  
          * </ul>
 76  
          * 
 77  
          * @param element The model element to process.
 78  
          * @param vars Variables for the evaluation of expressions
 79  
          * @throws SDMetricsException An error occurred evaluating the set
 80  
          *         expressions.
 81  
          */
 82  
         @SuppressWarnings("unchecked")
 83  
         public void add(ModelElement element, Variables vars)
 84  
                         throws SDMetricsException {
 85  193
                 if (valueSetExpression != null) {
 86  18
                         resultSet.add(engine.evalExpression(element, valueSetExpression,
 87  9
                                         vars));
 88  9
                 } else if (cumulativeSetExpression != null) {
 89  4
                         resultSet.addAll(engine.evalSetExpression(element,
 90  2
                                         cumulativeSetExpression, vars));
 91  2
                 } else {
 92  182
                         resultSet.add(element);
 93  
                 }
 94  193
         }
 95  
 
 96  
         /**
 97  
          * Adds the contents of a collection to the result set.
 98  
          * 
 99  
          * @param c Collection to add.
 100  
          */
 101  
         @SuppressWarnings("unchecked")
 102  
         public void add(Collection<?> c) {
 103  44
                 resultSet.addAll(c);
 104  44
         }
 105  
 
 106  
         /**
 107  
          * Removes a model element from the result set if the "exclude_self"
 108  
          * attribute is set to true. If not, calling this method has no effect.
 109  
          * 
 110  
          * @param element Element to remove.
 111  
          */
 112  
         public void excludeSelf(ModelElement element) {
 113  126
                 if (excludeSelf) {
 114  2
                         MetricTools.removeAny(resultSet, element);
 115  
                 }
 116  126
         }
 117  
 
 118  
         /**
 119  
          * Gets the result set.
 120  
          * 
 121  
          * @return The current result set.
 122  
          */
 123  
         public Collection<?> getResultSet() {
 124  143
                 return resultSet;
 125  
         }
 126  
 }