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