View Javadoc
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      {
18          this.patterns = patterns;
19      }
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          String[] tokenized = MatchPattern.tokenizePathToString( name, File.separator );
33          return matches( name, tokenized, isCaseSensitive );
34      }
35  
36      public boolean matches( String name, String[] tokenizedName, boolean isCaseSensitive )
37      {
38          char[][] tokenizedNameChar = new char[tokenizedName.length][];
39          for ( int i = 0; i < tokenizedName.length; i++ )
40          {
41              tokenizedNameChar[i] = tokenizedName[i].toCharArray();
42          }
43          for ( MatchPattern pattern : patterns )
44          {
45              if ( pattern.matchPath( name, tokenizedNameChar, isCaseSensitive ) )
46              {
47                  return true;
48              }
49          }
50          return false;
51      }
52  
53      public boolean matchesPatternStart( String name, boolean isCaseSensitive )
54      {
55          for ( MatchPattern includesPattern : patterns )
56          {
57              if ( includesPattern.matchPatternStart( name, isCaseSensitive ) )
58              {
59                  return true;
60              }
61          }
62          return false;
63      }
64  
65      public static MatchPatterns from( String... sources )
66      {
67          final int length = sources.length;
68          MatchPattern[] result = new MatchPattern[length];
69          for ( int i = 0; i < length; i++ )
70          {
71              result[i] = MatchPattern.fromString( sources[i] );
72          }
73          return new MatchPatterns( result );
74      }
75  
76      public static MatchPatterns from( Iterable<String> strings )
77      {
78          return new MatchPatterns( getMatchPatterns( strings ) );
79      }
80  
81      private static MatchPattern[] getMatchPatterns( Iterable<String> items )
82      {
83          List<MatchPattern> result = new ArrayList<MatchPattern>();
84          for ( String string : items )
85          {
86              result.add( MatchPattern.fromString( string ) );
87          }
88          return result.toArray( new MatchPattern[result.size()] );
89      }
90  
91  }