Coverage Report - com.sdmetrics.metrics.MetricProcedureSignature - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
MetricProcedureSignature
100%
40/40
93%
15/16
9
 
 1  4
 /*
 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  
 import java.util.Iterator;
 26  
 import java.util.TreeSet;
 27  
 
 28  
 import com.sdmetrics.math.ExpressionNode;
 29  
 import com.sdmetrics.model.ModelElement;
 30  
 
 31  
 /**
 32  
  * Calculates a "signature" metric procedure.
 33  
  */
 34  2
 public class MetricProcedureSignature extends MetricProcedure {
 35  
 
 36  
         @Override
 37  
         public String calculate(ModelElement element, Metric metric)
 38  
                         throws SDMetricsException {
 39  
 
 40  4
                 ProcedureAttributes attributes = metric.getAttributes();
 41  4
                 Variables vars = new Variables(element);
 42  
 
 43  4
                 StringBuilder signature = new StringBuilder();
 44  4
                 ExpressionNode prefix = attributes.getExpression("prologue");
 45  4
                 if (prefix != null) {
 46  2
                         signature.append(evalExpression(element, prefix, vars));
 47  2
                 } else {
 48  2
                         signature.append(element.getName());
 49  2
                         signature.append('(');
 50  
                 }
 51  
 
 52  
                 // retrieve set of elements related to e via specified relation
 53  8
                 Collection<ModelElement> set = evalElementSetExpression(element,
 54  4
                                 attributes.getRequiredExpression("set"), vars);
 55  4
                 if (set != null) {
 56  
                         // Gather the elements that define the signature
 57  4
                         FilterAttributeProcessor fap = getFilterAttributeProcessor(attributes);
 58  8
                         TreeSet<ModelElement> paramList = new TreeSet<ModelElement>(
 59  4
                                         ModelElement.getComparator());
 60  12
                         for (ModelElement projElem : fap.validIteration(set, vars)) {
 61  4
                                 paramList.add(projElem);
 62  
                         }
 63  
 
 64  
                         // Get separator definition
 65  4
                         String separatorString = ",";
 66  8
                         ExpressionNode separatorNode = attributes
 67  4
                                         .getExpression("separator");
 68  4
                         if (separatorNode != null)
 69  4
                                 separatorString = String.valueOf(evalExpression(element,
 70  2
                                                 separatorNode, vars));
 71  
 
 72  
                         // add each element to the signature
 73  4
                         ExpressionNode valueNode = attributes.getExpression("value");
 74  4
                         Iterator<ModelElement> it = paramList.iterator();
 75  12
                         while (it.hasNext()) {
 76  4
                                 if (valueNode != null)
 77  2
                                         signature
 78  2
                                                         .append(evalExpression(it.next(), valueNode, vars));
 79  
                                 else
 80  2
                                         signature.append(it.next().getXMIID());
 81  4
                                 if (it.hasNext())
 82  2
                                         signature.append(separatorString);
 83  
                         }
 84  
                 }
 85  
 
 86  4
                 ExpressionNode postfix = attributes.getExpression("epilogue");
 87  4
                 if (postfix != null) {
 88  2
                         signature.append(evalExpression(element, postfix, vars));
 89  2
                 } else {
 90  2
                         signature.append(')');
 91  
                 }
 92  
 
 93  4
                 return signature.toString();
 94  
         }
 95  
 }