Test coverage report for MetricProcedureNesting.java - www.sdmetrics.com

  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. import com.sdmetrics.math.ExpressionNode;
  24. import com.sdmetrics.model.ModelElement;

  25. /**
  26.  * Calculates a "nesting" metric procedure.
  27.  */
  28. public class MetricProcedureNesting extends MetricProcedure {

  29.     /**
  30.      * {@inheritDoc}
  31.      * @return Nesting level as defined by the metric.
  32.      */
  33.     @Override
  34.     public Integer calculate(ModelElement element, Metric metric)
  35.             throws SDMetricsException {
  36.         ProcedureAttributes attributes = metric.getAttributes();
  37.         ExpressionNode rel = attributes.getRequiredExpression("relation");
  38.         int result = 0;

  39.         // Get parent in the relation that defines the nesting
  40.         ModelElement parent = getMetricsEngine().evalModelElementExpression(
  41.                 element, rel, new Variables(element));
  42.         if (parent != null) {
  43.             if (isCompatible(element, metric, parent)) {
  44.                 // Compatible parent, nesting is parent's nesting level plus 1
  45.                 result = 1 + ((Integer) getMetricsEngine().getMetricValue(
  46.                         parent, metric)).intValue();
  47.             }
  48.         }
  49.         return Integer.valueOf(result);
  50.     }
  51. }