Coverage Report - com.sdmetrics.metrics.ScalarOperation - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
ScalarOperation
100%
7/7
100%
2/2
2
 
 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.math.ExpressionNode;
 25  
 import com.sdmetrics.model.ModelElement;
 26  
 
 27  
 /**
 28  
  * Base class for scalar operations in expressions. Scalar operations return
 29  
  * strings, numbers (Float or Integer), or model elements.
 30  
  * 
 31  
  * @since 2.3
 32  
  */
 33  208
 public abstract class ScalarOperation extends AbstractProcedure {
 34  
 
 35  
         /**
 36  
          * Calculates the value for the scalar operation.
 37  
          * 
 38  
          * @param element Model element for which to calculate the operation
 39  
          * @param node The expression node with the operands for the scalar
 40  
          *        operation.
 41  
          * @param vars The variables for the calculation.
 42  
          * 
 43  
          * @return Result of the operation.
 44  
          * @throws SDMetricsException Problems while calculating the operation
 45  
          *         value.
 46  
          */
 47  
         public abstract Object calculateValue(ModelElement element,
 48  
                         ExpressionNode node, Variables vars) throws SDMetricsException;
 49  
 
 50  
         /**
 51  
          * Evaluates an expression that yields a float value.
 52  
          * 
 53  
          * @param element The model element to evaluate the expression for.
 54  
          * @param node Root node of the expression to evaluate
 55  
          * @param vars Variables for the expression evaluation
 56  
          * @return The value of the expression
 57  
          * @throws SDMetricsException Problems while evaluating the expression.
 58  
          */
 59  
         protected float getFloatValue(ModelElement element, ExpressionNode node,
 60  
                         Variables vars) throws SDMetricsException {
 61  28
                 Object value = evalExpression(element, node, vars);
 62  28
                 return getFloatValue(value, element);
 63  
         }
 64  
 
 65  
         /**
 66  
          * Converts a number value to float.
 67  
          * 
 68  
          * @param value Value to convert (must be an instance of
 69  
          *        <code>java.lang.Number</code>).
 70  
          * @param element Model element for which to calculate the operation
 71  
          * @return floatValue of the Number
 72  
          * @throws SDMetricsException Value is not a number
 73  
          */
 74  
         protected float getFloatValue(Object value, ModelElement element)
 75  
                         throws SDMetricsException {
 76  30
                 if (value instanceof Number) {
 77  29
                         return ((Number) value).floatValue();
 78  
                 }
 79  2
                 throw new SDMetricsException(element, null, "Operator '" + getName()
 80  1
                                 + "' requires numerical operand.");
 81  
         }
 82  
 }