Coverage Report - org.apache.maven.shared.artifact.filter.collection.AbstractArtifactFeatureFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractArtifactFeatureFilter
100%
48/48
83%
25/30
2.4
 
 1  
 package org.apache.maven.shared.artifact.filter.collection;
 2  
 
 3  
 /* 
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.    
 20  
  */
 21  
 
 22  
 import java.util.Arrays;
 23  
 import java.util.HashSet;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Set;
 27  
 
 28  
 import org.apache.maven.artifact.Artifact;
 29  
 import org.codehaus.plexus.util.StringUtils;
 30  
 
 31  
 /**
 32  
  * This is the common base class of ClassifierFilter and TypeFilter
 33  
  * 
 34  
  * @author <a href="richardv@mxtelecom.com">Richard van der Hoff</a>
 35  
  * @version $Id: AbstractArtifactFeatureFilter.java 766157 2009-04-17 21:09:16Z pgier $
 36  
  */
 37  
 public abstract class AbstractArtifactFeatureFilter
 38  
     extends AbstractArtifactsFilter
 39  
 {
 40  
     /** The list of types or classifiers to include */
 41  
     private List includes;
 42  
 
 43  
     /**
 44  
      * The list of types or classifiers to exclude (ignored if includes != null)
 45  
      */
 46  
     private List excludes;
 47  
 
 48  
     public AbstractArtifactFeatureFilter( String include, String exclude)
 49  25
     {
 50  25
         setExcludes( exclude );
 51  25
         setIncludes( include );
 52  25
     }
 53  
 
 54  
     /**
 55  
      * This function determines if filtering needs to be performed. Includes are processed before Excludes.
 56  
      * 
 57  
      * @param dependencies the set of dependencies to filter.
 58  
      * @return a Set of filtered dependencies.
 59  
      */
 60  
     public Set filter( Set artifacts )
 61  
     {
 62  21
         Set results = artifacts;
 63  
 
 64  21
         if ( this.includes != null && !this.includes.isEmpty() )
 65  
         {
 66  9
             results = filterIncludes( results, this.includes );
 67  
         }
 68  
 
 69  21
         if ( this.excludes != null && !this.excludes.isEmpty() )
 70  
         {
 71  14
             results = filterExcludes( results, this.excludes );
 72  
         }
 73  
 
 74  21
         return results;
 75  
     }
 76  
 
 77  
     /**
 78  
      * Processes the dependencies list and includes the dependencies that match a filter in the list.
 79  
      * 
 80  
      * @param depends List of dependencies.
 81  
      * @param includes List of types or classifiers to include.
 82  
      * @return a set of filtered artifacts.
 83  
      */
 84  
     private Set filterIncludes( Set artifacts, List theIncludes )
 85  
     {
 86  9
         Set result = new HashSet();
 87  
 
 88  9
         Iterator includeIter = theIncludes.iterator();
 89  26
         while ( includeIter.hasNext() )
 90  
         {
 91  17
             String include = (String) includeIter.next();
 92  17
             Iterator iter = artifacts.iterator();
 93  82
             while ( iter.hasNext() )
 94  
             {
 95  65
                 Artifact artifact = (Artifact) iter.next();
 96  
 
 97  
                 // if the classifier or type of the artifact
 98  
                 // matches the feature
 99  
                 // to include, add to the
 100  
                 // results
 101  65
                 if ( compareFeatures( getArtifactFeature( artifact ), include ) )
 102  
                 {
 103  16
                     result.add( artifact );
 104  
                 }
 105  65
             }
 106  17
         }
 107  9
         return result;
 108  
     }
 109  
 
 110  
     /**
 111  
      * Processes the dependencies list and excludes the dependencies that match a filter in the list.
 112  
      * 
 113  
      * @param depends List of dependencies.
 114  
      * @param excludes List of types or classifiers to exclude.
 115  
      * @return a set of filtered artifacts.
 116  
      */
 117  
     private Set filterExcludes( Set artifacts, List theExcludes )
 118  
     {
 119  14
         Set result = new HashSet();
 120  
 
 121  14
         Iterator iter = artifacts.iterator();
 122  56
         while ( iter.hasNext() )
 123  
         {
 124  42
             boolean exclude = false;
 125  42
             Artifact artifact = (Artifact) iter.next();
 126  42
             String artifactFeature = getArtifactFeature( artifact );
 127  
 
 128  
             // look through all types or classifiers. If no
 129  
             // matches are found
 130  
             // then it can be added to the results.
 131  42
             Iterator excludeIter = theExcludes.iterator();
 132  88
             while ( excludeIter.hasNext() )
 133  
             {
 134  64
                 String excludeFeature = (String) excludeIter.next();
 135  64
                 if ( compareFeatures( artifactFeature, excludeFeature ) )
 136  
                 {
 137  18
                     exclude = true;
 138  18
                     break;
 139  
                 }
 140  46
             }
 141  
 
 142  42
             if ( !exclude )
 143  
             {
 144  24
                 result.add( artifact );
 145  
             }
 146  42
         }
 147  
 
 148  14
         return result;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Should return the type or classifier of the given artifact, so that we can filter it
 153  
      * 
 154  
      * @param artifact artifact to return type or classifier of
 155  
      * @return type or classifier
 156  
      */
 157  
     protected abstract String getArtifactFeature( Artifact artifact );
 158  
 
 159  
     public void setExcludes( String excludeString )
 160  
     {
 161  25
         if ( StringUtils.isNotEmpty( excludeString ) )
 162  
         {
 163  18
             this.excludes = Arrays.asList( StringUtils.split( excludeString, "," ) );
 164  
         }
 165  25
     }
 166  
 
 167  
     public void setIncludes( String includeString )
 168  
     {
 169  25
         if ( StringUtils.isNotEmpty( includeString ) )
 170  
         {
 171  14
             this.includes = Arrays.asList( StringUtils.split( includeString, "," ) );
 172  
         }
 173  25
     }
 174  
 
 175  
     /**
 176  
      * @return Returns the excludes.
 177  
      */
 178  
     public List getExcludes()
 179  
     {
 180  4
         return this.excludes;
 181  
     }
 182  
 
 183  
     /**
 184  
      * @return Returns the includes.
 185  
      */
 186  
     public List getIncludes()
 187  
     {
 188  4
         return this.includes;
 189  
     }
 190  
 
 191  
     /**
 192  
      * Allows Feature comparison to be customized
 193  
      * 
 194  
      * @param lhs String artifact's feature
 195  
      * @param rhs String feature from exclude or include list
 196  
      * @return boolean true if features match
 197  
      */
 198  
     protected boolean compareFeatures( String lhs, String rhs )
 199  
     {
 200  
         // If lhs is null, check that rhs is null. Otherwise check if strings are equal.
 201  72
         return ( lhs == null ? rhs == null : lhs.equals( rhs ) );
 202  
     }
 203  
 }