Coverage Report - org.apache.maven.plugins.shade.mojo.ArtifactSelector
 
Classes in this File Line Coverage Branch Coverage Complexity
ArtifactSelector
80%
20/25
64%
18/28
2,167
 
 1  
 package org.apache.maven.plugins.shade.mojo;
 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.Collection;
 23  
 import java.util.HashSet;
 24  
 import java.util.Iterator;
 25  
 
 26  
 import org.apache.maven.artifact.Artifact;
 27  
 
 28  
 /**
 29  
  * @author Benjamin Bentmann
 30  
  */
 31  
 class ArtifactSelector
 32  
 {
 33  
 
 34  
     private Collection includes;
 35  
 
 36  
     private Collection excludes;
 37  
 
 38  
     public ArtifactSelector( Artifact projectArtifact, ArtifactSet artifactSet, String groupPrefix )
 39  
     {
 40  0
         this( ( artifactSet != null ) ? artifactSet.getIncludes() : null,
 41  
               ( artifactSet != null ) ? artifactSet.getExcludes() : null, groupPrefix );
 42  
 
 43  0
         if ( projectArtifact != null && !this.includes.isEmpty() )
 44  
         {
 45  0
             this.includes.add( new ArtifactId( projectArtifact ) );
 46  
         }
 47  0
     }
 48  
 
 49  
     public ArtifactSelector( Collection includes, Collection excludes, String groupPrefix )
 50  7
     {
 51  7
         this.includes = toIds( includes );
 52  7
         this.excludes = toIds( excludes );
 53  
 
 54  7
         if ( groupPrefix != null && groupPrefix.length() > 0 )
 55  
         {
 56  1
             this.includes.add( new ArtifactId( groupPrefix + "*", "*", "*", "*" ) );
 57  
         }
 58  7
     }
 59  
 
 60  
     private static Collection toIds( Collection patterns )
 61  
     {
 62  14
         Collection result = new HashSet();
 63  
 
 64  14
         if ( patterns != null )
 65  
         {
 66  8
             for ( Iterator it = patterns.iterator(); it.hasNext(); )
 67  
             {
 68  4
                 String pattern = (String) it.next();
 69  4
                 result.add( new ArtifactId( pattern ) );
 70  4
             }
 71  
         }
 72  
 
 73  14
         return result;
 74  
     }
 75  
 
 76  
     public boolean isSelected( Artifact artifact )
 77  
     {
 78  0
         return ( artifact != null ) ? isSelected( new ArtifactId( artifact ) ) : false;
 79  
     }
 80  
 
 81  
     boolean isSelected( ArtifactId id )
 82  
     {
 83  13
         return ( includes.isEmpty() || matches( includes, id ) ) && !matches( excludes, id );
 84  
     }
 85  
 
 86  
     private boolean matches( Collection patterns, ArtifactId id )
 87  
     {
 88  18
         for ( Iterator it = patterns.iterator(); it.hasNext(); )
 89  
         {
 90  12
             ArtifactId pattern = (ArtifactId) it.next();
 91  12
             if ( id.matches( pattern ) )
 92  
             {
 93  7
                 return true;
 94  
             }
 95  5
         }
 96  11
         return false;
 97  
     }
 98  
 
 99  
 }