Test coverage report for MetricProcedureSubelements.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 java.util.Collection;

  24. import com.sdmetrics.model.ModelElement;

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

  29.     @Override
  30.     public Number calculate(ModelElement element, Metric metric)
  31.             throws SDMetricsException {
  32.         ProcedureAttributes attributes = metric.getAttributes();
  33.         FilterAttributeProcessor fap = getFilterAttributeProcessor(attributes);
  34.         SummationHelper sum = new SummationHelper(getMetricsEngine(),
  35.                 attributes);
  36.         gatherSubelements(element, fap, sum, new Variables(element));
  37.         return sum.getTotal();
  38.     }

  39.     /*
  40.      * Recursively collect the elements owned by a model element that satisfy
  41.      * filter attributes.
  42.      */
  43.     private void gatherSubelements(ModelElement element,
  44.             FilterAttributeProcessor fap, SummationHelper sum, Variables vars)
  45.             throws SDMetricsException {
  46.         Collection<ModelElement> ownedElements = element.getOwnedElements();
  47.         if (ownedElements == null) {
  48.             return;
  49.         }
  50.         for (ModelElement projElem : fap.fullIteration(ownedElements, vars)) {
  51.             if (fap.isValid()) {
  52.                 sum.add(projElem, vars);
  53.             }

  54.             gatherSubelements(projElem, fap, sum, vars);
  55.         }
  56.     }
  57. }