Coverage Report - com.sdmetrics.metrics.SetProcedureProjection - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
SetProcedureProjection
100%
25/25
100%
16/16
11
 
 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.Collection;
 25  
 import java.util.Collections;
 26  
 import java.util.HashSet;
 27  
 
 28  
 import com.sdmetrics.math.HashMultiSet;
 29  
 import com.sdmetrics.model.ModelElement;
 30  
 
 31  
 /**
 32  
  * Calculates the "projection" set procedure.
 33  
  */
 34  54
 public class SetProcedureProjection extends SetProcedure {
 35  
 
 36  
         /** Empty regular set to return. */
 37  20
         private final static Collection<?> EMPTYSET = Collections
 38  20
                         .unmodifiableSet(new HashSet<Object>(1));
 39  
         /** Empty multiset to return. */
 40  20
         private final static Collection<?> EMPTYMULTISET = Collections
 41  40
                         .unmodifiableCollection(new HashMultiSet<Object>(1));
 42  
 
 43  
         @Override
 44  
         public Collection<?> calculate(ModelElement element, Set set)
 45  
                         throws SDMetricsException {
 46  196
                 ProcedureAttributes attributes = set.getAttributes();
 47  
 
 48  
                 // retrieve set of elements related via the specified relation
 49  196
                 Variables vars = new Variables(element);
 50  392
                 Collection<ModelElement> projection = getRelationOrSet(element,
 51  196
                                 attributes, vars);
 52  195
                 if (projection == null)
 53  97
                         return set.isMultiSet() ? EMPTYMULTISET : EMPTYSET;
 54  
 
 55  
                 // Get remainder of the set projection attributes
 56  98
                 FilterAttributeProcessor fap = getFilterAttributeProcessor(attributes);
 57  196
                 SetSummationHelper summer = new SetSummationHelper(getMetricsEngine(),
 58  98
                                 set, "set");
 59  98
                 boolean recurse = attributes.getBooleanValue("recurse", false);
 60  
 
 61  384
                 for (ModelElement projElem : fap.fullIteration(projection, vars)) {
 62  188
                         if (fap.isValid()) // element is suitable to be considered
 63  168
                                 summer.add(projElem, vars);
 64  
 
 65  
                         // recursively add the sets for compatible elements.
 66  188
                         if (recurse && isCompatible(element, set, projElem))
 67  42
                                 summer.add(getMetricsEngine().getSet(projElem, set));
 68  
                 }
 69  98
                 summer.excludeSelf(element);
 70  
 
 71  98
                 Collection<?> result = summer.getResultSet();
 72  98
                 if (!set.isMultiSet() && projection.equals(result))
 73  16
                         return projection; // reuse project set if it was not changed
 74  82
                 return result;
 75  
         }
 76  
 }