Coverage Report - com.sdmetrics.model.ElementFilters - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
ElementFilters
100%
23/23
100%
16/16
4,667
 
 1  
 package com.sdmetrics.model;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 
 5  
 /**
 6  
  * Processes the element filter matching.
 7  
  */
 8  
 class ElementFilters {
 9  
         /** Stores the tokenized element filter strings. */
 10  
         private final String[][] filters;
 11  
         /** Name fragments of a model element to test. */
 12  12
         private final ArrayList<String> nameFragments = new ArrayList<String>();
 13  
 
 14  
         /**
 15  
          * Constructor.
 16  
          * @param filterStrings The list of applicable element filter strings.
 17  
          */
 18  12
         ElementFilters(String[] filterStrings) {
 19  
                 // tokenize the list of filter strings
 20  12
                 filters = new String[filterStrings.length][];
 21  30
                 for (int i = 0; i < filters.length; i++) {
 22  18
                         filters[i] = filterStrings[i].split("\\.");
 23  
                 }
 24  12
         }
 25  
 
 26  
         /**
 27  
          * Tests if a model element matches one of the filter strings.
 28  
          * 
 29  
          * @param element the element to test
 30  
          * @return <code>true</code> if the model element matches at least one of
 31  
          *         the filter strings of this filter, <code>false</code> if it
 32  
          *         matches none of the filter strings.
 33  
          */
 34  
         boolean matches(ModelElement element) {
 35  
                 // collect the fragments of the qualified name of the model
 36  177
                 ModelElement me = element;
 37  177
                 nameFragments.clear();
 38  968
                 while (me != null) {
 39  614
                         nameFragments.add(me.getName());
 40  614
                         me = me.getOwner();
 41  
                 }
 42  
 
 43  
                 // check if any of the filter strings gives a match
 44  359
                 for (String[] filter : filters) {
 45  232
                         if (matches(filter)) {
 46  50
                                 return true;
 47  
                         }
 48  
                 }
 49  127
                 return false;
 50  
         }
 51  
 
 52  
         private boolean matches(String[] filter) {
 53  232
                 int fragmentCount = nameFragments.size();
 54  232
                 if (fragmentCount < filter.length) {
 55  100
                         return false;
 56  
                 }
 57  444
                 for (int i = 0; i < filter.length; i++) {
 58  394
                         if (!"#".equals(filter[i])) {
 59  201
                                 if (!filter[i].equals(nameFragments.get(fragmentCount - i - 1))) {
 60  82
                                         return false;
 61  
                                 }
 62  
                         }
 63  
                 }
 64  50
                 return true;
 65  
         }
 66  
 }