Coverage Report - com.sdmetrics.metrics.MetricProcedureAttributeValue - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
MetricProcedureAttributeValue
100%
28/28
100%
12/12
13
 
 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.model.ModelElement;
 25  
 
 26  
 /**
 27  
  * Calculates an "attributevalue" metric procedure.
 28  
  */
 29  3
 public class MetricProcedureAttributeValue extends MetricProcedure {
 30  
 
 31  
         /**
 32  
          * @return Value of the specified attribute for the specified model element.
 33  
          *         If the attribute is a cross-reference, the referenced
 34  
          *         {@link ModelElement} object is returned.
 35  
          * @throws SDMetricsException if required attributes are missing or have the
 36  
          *         wrong type.
 37  
          */
 38  
         @Override
 39  
         protected Object calculate(ModelElement element, Metric metric)
 40  
                         throws SDMetricsException {
 41  8
                 ProcedureAttributes attributes = metric.getAttributes();
 42  8
                 String attrName = attributes.getRequiredStringValue("attr");
 43  8
                 FilterAttributeProcessor fap = getFilterAttributeProcessor(attributes);
 44  16
                 ModelElement filtered = fap.applyFilters(null, element, new Variables(
 45  8
                                 element));
 46  8
                 if (filtered == null)
 47  1
                         return "";
 48  7
                 if (!fap.isValid())
 49  1
                         return "";
 50  
 
 51  6
                 if (!filtered.getType().hasAttribute(attrName))
 52  2
                         throw new SDMetricsException(element, metric, "Unknown attribute '"
 53  1
                                         + attrName + "' for elements of type '"
 54  1
                                         + filtered.getType().getName() + "'.");
 55  5
                 if (filtered.getType().isSetAttribute(attrName))
 56  2
                         throw new SDMetricsException(
 57  1
                                         element,
 58  1
                                         metric,
 59  2
                                         "Attribute '"
 60  1
                                                         + attrName
 61  1
                                                         + "' is not a single-valued attribute for elements of type '"
 62  1
                                                         + filtered.getType().getName() + "'.");
 63  
 
 64  4
                 if (filtered.getType().isRefAttribute(attrName)) {
 65  
                         // handle cross-reference attribute: get referenced element
 66  2
                         ModelElement result = filtered.getRefAttribute(attrName);
 67  2
                         if (result == null)
 68  1
                                 return ""; // no element referenced -> empty string
 69  1
                         return result;
 70  
                 }
 71  
                 // plain data attribute, return its value
 72  2
                 return filtered.getPlainAttribute(attrName);
 73  
         }
 74  
 }