Test coverage report for SetProcedureProjection.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 java.util.Collections;
  25. import java.util.HashSet;

  26. import com.sdmetrics.math.HashMultiSet;
  27. import com.sdmetrics.model.ModelElement;

  28. /**
  29.  * Calculates the "projection" set procedure.
  30.  */
  31. public class SetProcedureProjection extends SetProcedure {

  32.     /** Empty regular set to return. */
  33.     private static final Collection<?> EMPTYSET = Collections
  34.             .unmodifiableSet(new HashSet<>(1));
  35.     /** Empty multiset to return. */
  36.     private static final Collection<?> EMPTYMULTISET = Collections
  37.             .unmodifiableCollection(new HashMultiSet<>(1));

  38.     @Override
  39.     public Collection<?> calculate(ModelElement element, Set set)
  40.             throws SDMetricsException {
  41.         ProcedureAttributes attributes = set.getAttributes();

  42.         // retrieve set of elements related via the specified relation
  43.         Variables vars = new Variables(element);
  44.         Collection<ModelElement> projection = getRelationOrSet(element,
  45.                 attributes, vars);
  46.         if (projection == null) {
  47.             return set.isMultiSet() ? EMPTYMULTISET : EMPTYSET;
  48.         }

  49.         // Get remainder of the set projection attributes
  50.         FilterAttributeProcessor fap = getFilterAttributeProcessor(attributes);
  51.         SetSummationHelper summer = new SetSummationHelper(getMetricsEngine(),
  52.                 set, "set");
  53.         boolean recurse = attributes.getBooleanValue("recurse", false);

  54.         for (ModelElement projElem : fap.fullIteration(projection, vars)) {
  55.             if (fap.isValid()) { // element is suitable to be considered
  56.                 summer.add(projElem, vars);
  57.             }

  58.             // recursively add the sets for compatible elements.
  59.             if (recurse && isCompatible(element, set, projElem)) {
  60.                 summer.add(getMetricsEngine().getSet(projElem, set));
  61.             }
  62.         }
  63.         summer.excludeSelf(element);

  64.         Collection<?> result = summer.getResultSet();
  65.         if (!set.isMultiSet() && projection.equals(result)) {
  66.             return projection; // reuse project set if it was not changed
  67.         }
  68.         return result;
  69.     }
  70. }