Coverage Report - com.sdmetrics.metrics.ProcedureAttributes - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
ProcedureAttributes
100%
26/26
100%
10/10
2,667
 
 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.HashMap;
 25  
 
 26  
 import com.sdmetrics.math.ExpressionNode;
 27  
 
 28  
 /**
 29  
  * Stores the attributes of the calculation procedure definition of a metric,
 30  
  * set, matrix, or rule in the metric definition file.
 31  
  */
 32  17078
 public class ProcedureAttributes {
 33  
         /**
 34  
          * The attributes of the procedure. Key is the attribute name, values is the
 35  
          * root node of the operator tree of the attribute.
 36  
          */
 37  8539
         private final HashMap<String, ExpressionNode> attributes = 
 38  8539
                         new HashMap<String, ExpressionNode>(4);
 39  
 
 40  
         /**
 41  
          * Retrieves the operator tree for an optional attribute holding an
 42  
          * expression.
 43  
          * 
 44  
          * @param attrName Name of the attribute.
 45  
          * @return Root node of the operator tree of the specified attribute, or
 46  
          *         <code>null</code> if the attribute is not set.
 47  
          */
 48  
         public ExpressionNode getExpression(String attrName) {
 49  1953
                 return attributes.get(attrName);
 50  
         }
 51  
 
 52  
         /**
 53  
          * Retrieves the operator tree for a required attribute holding an
 54  
          * expression.
 55  
          * 
 56  
          * @param attrName Name of the attribute.
 57  
          * @return Root node of the operator tree of the specified attribute
 58  
          * @throws SDMetricsException The specified attribute is not set.
 59  
          */
 60  
         public ExpressionNode getRequiredExpression(String attrName)
 61  
                         throws SDMetricsException {
 62  99
                 ExpressionNode result = attributes.get(attrName);
 63  99
                 if (result == null)
 64  6
                         throw new SDMetricsException(null, null,
 65  3
                                         "Missing required attribute '" + attrName + "'.");
 66  96
                 return result;
 67  
         }
 68  
 
 69  
         /**
 70  
          * Retrieves the value of an optional attribute holding a simple string.
 71  
          * 
 72  
          * @param attrName Name of the attribute.
 73  
          * @return String value of the specified attribute, or <code>null</code> if
 74  
          *         the attribute is not set.
 75  
          * @throws SDMetricsException the specified attribute contains operators
 76  
          *         rather than a simple string value.
 77  
          */
 78  
         public String getStringValue(String attrName) throws SDMetricsException {
 79  1131
                 ExpressionNode node = attributes.get(attrName);
 80  1131
                 if (node == null)
 81  693
                         return null;
 82  438
                 if (node.isOperation())
 83  2
                         throw new SDMetricsException(null, null, "Attribute '" + attrName
 84  1
                                         + "' may not contain operators.");
 85  437
                 return node.getValue();
 86  
         }
 87  
 
 88  
         /**
 89  
          * Retrieves the value of a required attribute holding a simple string.
 90  
          * 
 91  
          * @param attrName Name of the attribute.
 92  
          * @return String value of the specified attribute.
 93  
          * @throws SDMetricsException the specified attribute is not set or contains
 94  
          *         operators rather than a simple string value.
 95  
          */
 96  
         public String getRequiredStringValue(String attrName)
 97  
                         throws SDMetricsException {
 98  10
                 if (!attributes.containsKey(attrName))
 99  2
                         throw new SDMetricsException(null, null,
 100  1
                                         "Missing required attribute '" + attrName + "'.");
 101  9
                 return getStringValue(attrName);
 102  
         }
 103  
 
 104  
         /**
 105  
          * Retrieves the value of a boolean attribute.
 106  
          * 
 107  
          * @param attrName Name of the attribute.
 108  
          * @param defaultValue Value to return if the attribute is not set at all.
 109  
          * @return <code>true</code> if the attribute is set to "true",
 110  
          *         <code>false</code> if the attribute is set to anything else than
 111  
          *         "true", <code>defaultValue</code> if the attribute is not set at
 112  
          *         all.
 113  
          * @throws SDMetricsException the specified attribute contains operators
 114  
          *         rather than a simple string value.
 115  
          */
 116  
         public boolean getBooleanValue(String attrName, boolean defaultValue)
 117  
                         throws SDMetricsException {
 118  404
                 String test = getStringValue(attrName);
 119  404
                 if (test == null)
 120  347
                         return defaultValue;
 121  57
                 return test.equals("true");
 122  
         }
 123  
 
 124  
         /**
 125  
          * Sets the expression node of an attribute.
 126  
          * 
 127  
          * @param attrName Name of the attribute.
 128  
          * @param value Root node of the operator tree of the expression.
 129  
          */
 130  
         void setExpressionNode(String attrName, ExpressionNode value) {
 131  20972
                 attributes.put(attrName, value);
 132  20972
         }
 133  
 }