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  
          * @param engine metrics engine for expression evaluation
 48  
          * @param set The definition of the set to process
 49  
          * @param setAttributeName The name of the attribute defining the sets to
 50  
          *        accumulate.
 51  
          * @throws SDMetricsException Summation attributes have illegal values
 52  
          */
 53  130
         public SetSummationHelper(MetricsEngine engine, Set set,
 54  
                         String setAttributeName) throws SDMetricsException {
 55  130
                 this.engine = engine;
 56  130
                 ProcedureAttributes attributes = set.getAttributes();
 57  130
                 cumulativeSetExpression = attributes.getExpression(setAttributeName);
 58  130
                 valueSetExpression = attributes.getExpression("valueset");
 59  130
                 excludeSelf = attributes.getBooleanValue("exclude_self", false);
 60  
 
 61  130
                 resultSet = MetricTools.createHashSet(set.isMultiSet());
 62  130
         }
 63  
 
 64  
         /**
 65  
          * Processes the cumulative or valueset expression for a model element and
 66  
          * adds its contents to the result set.
 67  
          * <p>
 68  
          * <ul>
 69  
          * <li>If the "valueset" attribute is set, it is evaluated for the model
 70  
          * element and the expression result added to the result set.</li>
 71  
          * <li>If the cumulative set expression is set, it is evaluated and its
 72  
          * contents added to the result set.
 73  
          * <li>Otherwise, the model element itself is added to the result set.
 74  
          * </ul>
 75  
          * 
 76  
          * @param element The model element to process.
 77  
          * @param vars Variables for the evaluation of expressions
 78  
          * @throws SDMetricsException An error occurred evaluating the set
 79  
          *         expressions.
 80  
          */
 81  
         @SuppressWarnings("unchecked")
 82  
         public void add(ModelElement element, Variables vars)
 83  
                         throws SDMetricsException {
 84  193
                 if (valueSetExpression != null) {
 85  18
                         resultSet.add(engine.evalExpression(element, valueSetExpression,
 86  9
                                         vars));
 87  9
                 } else if (cumulativeSetExpression != null) {
 88  4
                         resultSet.addAll(engine.evalSetExpression(element,
 89  2
                                         cumulativeSetExpression, vars));
 90  2
                 } else {
 91  182
                         resultSet.add(element);
 92  
                 }
 93  193
         }
 94  
 
 95  
         /**
 96  
          * Adds the contents of a collection to the result set.
 97  
          * 
 98  
          * @param c Collection to add.
 99  
          */
 100  
         @SuppressWarnings("unchecked")
 101  
         public void add(Collection<?> c) {
 102  44
                 resultSet.addAll(c);
 103  44
         }
 104  
 
 105  
         /**
 106  
          * Removes a model element from the result set if the "exclude_self"
 107  
          * attribute is set to true. If not, calling this method has no effect.
 108  
          * 
 109  
          * @param element Element to remove.
 110  
          */
 111  
         public void excludeSelf(ModelElement element) {
 112  126
                 if (excludeSelf)
 113  2
                         MetricTools.removeAny(resultSet, element);
 114  126
         }
 115  
 
 116  
         /**
 117  
          * Gets the result set.
 118  
          * 
 119  
          * @return The current result set.
 120  
          */
 121  
         public Collection<?> getResultSet() {
 122  143
                 return resultSet;
 123  
         }
 124  
 }