Coverage Report - com.sdmetrics.model.Model - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
Model
100%
38/38
100%
24/24
2,5
 
 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.model;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.HashMap;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 
 29  
 import com.sdmetrics.math.MappedCollectionsIterator;
 30  
 
 31  
 /**
 32  
  * Container for the model elements of the UML design to be analyzed. Provides
 33  
  * access to the model elements and element filtering based on qualified names.
 34  
  */
 35  
 public class Model implements Iterable<ModelElement> {
 36  
         /** Metamodel on which the elements of this model are based. */
 37  
         private final MetaModel metaModel;
 38  
 
 39  
         /**
 40  
          * Data structure to hold all model elements. Elements of each type are
 41  
          * stored in a random access list.
 42  
          */
 43  
         private final HashMap<MetaModelElement, ArrayList<ModelElement>> elementsByType;
 44  
 
 45  
         /**
 46  
          * Data structure to hold the model elements that have been accepted for
 47  
          * output as per the element filter settings.
 48  
          */
 49  
         private HashMap<MetaModelElement, ArrayList<ModelElement>> acceptedElementsByType;
 50  
 
 51  
         /**
 52  
          * Creates a new, empty model.
 53  
          * 
 54  
          * @param metaModel Metamodel that defines the element types and attributes.
 55  
          */
 56  151
         public Model(MetaModel metaModel) {
 57  151
                 this.metaModel = metaModel;
 58  302
                 elementsByType = new HashMap<MetaModelElement, ArrayList<ModelElement>>(
 59  151
                                 metaModel.getNumberOfTypes());
 60  2216
                 for (MetaModelElement type : metaModel) {
 61  1914
                         elementsByType.put(type, new ArrayList<ModelElement>());
 62  
                 }
 63  151
         }
 64  
 
 65  
         /**
 66  
          * Applies filter settings to the elements of this model.
 67  
          * 
 68  
          * @param filterStrings The list of element filters to apply. Can be
 69  
          *        <code>null</code> or empty to disable filtering.
 70  
          * @param acceptMatchingElements Set to <code>true</code> to accept elements
 71  
          *        matching at least on of the element filters. Set to
 72  
          *        <code>false</code> to accept only elements that matching none of
 73  
          *        the element filters.
 74  
          * @param ignoreRelationsToRejectedElements Set to <code>true</code> to
 75  
          *        ignore links to rejected elements for metrics calculation, set to
 76  
          *        <code>false</code> to include links to rejected elements for
 77  
          *        metrics calculation.
 78  
          */
 79  
         public void setFilter(String[] filterStrings,
 80  
                         boolean acceptMatchingElements,
 81  
                         boolean ignoreRelationsToRejectedElements) {
 82  
 
 83  11
                 if (filterStrings == null || filterStrings.length == 0) {
 84  
                         // disable filtering; mark
 85  30
                         for (ModelElement elem : this) {
 86  26
                                 elem.setLinksIgnored(false);
 87  
                         }
 88  2
                         acceptedElementsByType = null; // means no filter is set
 89  2
                         return;
 90  
                 }
 91  
 
 92  9
                 ElementFilters elementFilters = new ElementFilters(filterStrings);
 93  18
                 acceptedElementsByType = new HashMap<MetaModelElement, ArrayList<ModelElement>>(
 94  9
                                 metaModel.getNumberOfTypes());
 95  
 
 96  
                 // iterate over all elements and determine filter status
 97  71
                 for (MetaModelElement type : metaModel) {
 98  53
                         ArrayList<ModelElement> acceptedElementList = new ArrayList<ModelElement>();
 99  53
                         acceptedElementsByType.put(type, acceptedElementList);
 100  
 
 101  280
                         for (ModelElement elem : elementsByType.get(type)) {
 102  
 
 103  
                                 boolean elementAccepted;
 104  174
                                 if (elementFilters.matches(elem)) {
 105  49
                                         elementAccepted = acceptMatchingElements;
 106  49
                                 } else {
 107  125
                                         elementAccepted = !acceptMatchingElements;
 108  
                                 }
 109  
 
 110  174
                                 if (elementAccepted) {
 111  46
                                         acceptedElementList.add(elem);
 112  
                                 }
 113  
 
 114  348
                                 elem.setLinksIgnored(ignoreRelationsToRejectedElements
 115  148
                                                 && !elementAccepted);
 116  
                         }
 117  
                 }
 118  9
         }
 119  
 
 120  
         /**
 121  
          * Returns the list of all elements of a given type. For example, a list of
 122  
          * all classes, all packages, etc. This method ignores filter settings, and
 123  
          * always returns all model elements.
 124  
          * 
 125  
          * @param type The type ID of the elements to return.
 126  
          * @return A random access list of all elements of the specified type.
 127  
          */
 128  
         public List<ModelElement> getElements(MetaModelElement type) {
 129  5056
                 return elementsByType.get(type);
 130  
         }
 131  
 
 132  
         /**
 133  
          * Returns the list of accepted elements of a given type. If the element
 134  
          * filter is active, this method only returns the elements that should
 135  
          * appear in the output data tables, as per the filter settings.
 136  
          * 
 137  
          * @param type The type of the elements to return.
 138  
          * @return A random access list of all accepted elements of the specified
 139  
          *         type.
 140  
          */
 141  
         public List<ModelElement> getAcceptedElements(MetaModelElement type) {
 142  22
                 if (acceptedElementsByType == null) {
 143  
                         // filters not set or disabled => return all elements.
 144  14
                         return elementsByType.get(type);
 145  
                 }
 146  8
                 return acceptedElementsByType.get(type);
 147  
         }
 148  
 
 149  
         /**
 150  
          * Returns an iterator over all model elements of the model, ignoring any
 151  
          * filters settings.
 152  
          * 
 153  
          * @return Iterator over all model elements of all types.
 154  
          */
 155  
         @Override
 156  
         public Iterator<ModelElement> iterator() {
 157  3
                 return new MappedCollectionsIterator<ModelElement>(elementsByType);
 158  
         }
 159  
 
 160  
         /**
 161  
          * Retrieves the metamodel on which this model is based.
 162  
          * 
 163  
          * @return This model's metamodel.
 164  
          */
 165  
         public MetaModel getMetaModel() {
 166  408
                 return metaModel;
 167  
         }
 168  
 
 169  
         /**
 170  
          * Adds a model element to this model.
 171  
          * 
 172  
          * @param el The model element to add.
 173  
          */
 174  
         void addElement(ModelElement el) {
 175  13715
                 elementsByType.get(el.getType()).add(el);
 176  13715
         }
 177  
 
 178  
         /**
 179  
          * Removes a model element from this model.
 180  
          * @param el The model element to remove.
 181  
          */
 182  
         void removeElement(ModelElement el) {
 183  3
                 elementsByType.get(el.getType()).remove(el);
 184  3
         }
 185  
 }