Coverage Report - org.codehaus.plexus.util.MatchPatterns
 
Classes in this File Line Coverage Branch Coverage Complexity
MatchPatterns
74%
20/27
71%
10/14
2.286
 
 1  
 package org.codehaus.plexus.util;
 2  
 
 3  
 import java.io.File;
 4  
 import java.util.ArrayList;
 5  
 import java.util.List;
 6  
 
 7  
 /**
 8  
  * A list of patterns to be matched
 9  
  *
 10  
  * @author Kristian Rosenvold
 11  
  */
 12  
 public class MatchPatterns
 13  
 {
 14  
     private final MatchPattern[] patterns;
 15  
 
 16  
     private MatchPatterns( MatchPattern[] patterns )
 17  27
     {
 18  27
         this.patterns = patterns;
 19  27
     }
 20  
 
 21  
     /**
 22  
      * Checks these MatchPatterns against a specified string.
 23  
      * <p/>
 24  
      * Uses far less string tokenization than any of the alternatives.
 25  
      *
 26  
      * @param name The name to look for
 27  
      * @param isCaseSensitive If the comparison is case sensitive
 28  
      * @return true if any of the supplied patterns match
 29  
      */
 30  
     public boolean matches( String name, boolean isCaseSensitive )
 31  
     {
 32  3
         String[] tokenized = MatchPattern.tokenizePathToString( name, File.separator );
 33  3
         return matches( name, tokenized, isCaseSensitive );
 34  
     }
 35  
 
 36  
     public boolean matches( String name, String[] tokenizedName, boolean isCaseSensitive )
 37  
     {
 38  253
         char[][] tokenizedNameChar = new char[tokenizedName.length][];
 39  797
         for ( int i = 0; i < tokenizedName.length; i++ )
 40  
         {
 41  544
             tokenizedNameChar[i] = tokenizedName[i].toCharArray();
 42  
         }
 43  941
         for ( MatchPattern pattern : patterns )
 44  
         {
 45  808
             if ( pattern.matchPath( name, tokenizedNameChar, isCaseSensitive ) )
 46  
             {
 47  120
                 return true;
 48  
             }
 49  
         }
 50  133
         return false;
 51  
     }
 52  
 
 53  
     public boolean matchesPatternStart( String name, boolean isCaseSensitive )
 54  
     {
 55  58
         for ( MatchPattern includesPattern : patterns )
 56  
         {
 57  58
             if ( includesPattern.matchPatternStart( name, isCaseSensitive ) )
 58  
             {
 59  58
                 return true;
 60  
             }
 61  
         }
 62  0
         return false;
 63  
     }
 64  
 
 65  
     public static MatchPatterns from( String... sources )
 66  
     {
 67  27
         final int length = sources.length;
 68  27
         MatchPattern[] result = new MatchPattern[length];
 69  185
         for ( int i = 0; i < length; i++ )
 70  
         {
 71  158
             result[i] = MatchPattern.fromString( sources[i] );
 72  
         }
 73  27
         return new MatchPatterns( result );
 74  
     }
 75  
 
 76  
     public static MatchPatterns from( Iterable<String> strings )
 77  
     {
 78  0
         return new MatchPatterns( getMatchPatterns( strings ) );
 79  
     }
 80  
 
 81  
     private static MatchPattern[] getMatchPatterns( Iterable<String> items )
 82  
     {
 83  0
         List<MatchPattern> result = new ArrayList<MatchPattern>();
 84  0
         for ( String string : items )
 85  
         {
 86  0
             result.add( MatchPattern.fromString( string ) );
 87  0
         }
 88  0
         return result.toArray( new MatchPattern[result.size()] );
 89  
     }
 90  
 
 91  
 }