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      private final MatchPattern[] patterns;
14  
15      private MatchPatterns(MatchPattern[] patterns) {
16          this.patterns = patterns;
17      }
18  
19      /**
20       * <p>Checks these MatchPatterns against a specified string.</p>
21       *
22       * <p>Uses far less string tokenization than any of the alternatives.</p>
23       *
24       * @param name The name to look for
25       * @param isCaseSensitive If the comparison is case sensitive
26       * @return true if any of the supplied patterns match
27       */
28      public boolean matches(String name, boolean isCaseSensitive) {
29          String[] tokenized = MatchPattern.tokenizePathToString(name, File.separator);
30          return matches(name, tokenized, isCaseSensitive);
31      }
32  
33      public boolean matches(String name, String[] tokenizedName, boolean isCaseSensitive) {
34          char[][] tokenizedNameChar = new char[tokenizedName.length][];
35          for (int i = 0; i < tokenizedName.length; i++) {
36              tokenizedNameChar[i] = tokenizedName[i].toCharArray();
37          }
38          return matches(name, tokenizedNameChar, isCaseSensitive);
39      }
40  
41      public boolean matches(String name, char[][] tokenizedNameChar, boolean isCaseSensitive) {
42          for (MatchPattern pattern : patterns) {
43              if (pattern.matchPath(name, tokenizedNameChar, isCaseSensitive)) {
44                  return true;
45              }
46          }
47          return false;
48      }
49  
50      public boolean matchesPatternStart(String name, boolean isCaseSensitive) {
51          for (MatchPattern includesPattern : patterns) {
52              if (includesPattern.matchPatternStart(name, isCaseSensitive)) {
53                  return true;
54              }
55          }
56          return false;
57      }
58  
59      public static MatchPatterns from(String... sources) {
60          final int length = sources.length;
61          MatchPattern[] result = new MatchPattern[length];
62          for (int i = 0; i < length; i++) {
63              result[i] = MatchPattern.fromString(sources[i]);
64          }
65          return new MatchPatterns(result);
66      }
67  
68      public static MatchPatterns from(Iterable<String> strings) {
69          return new MatchPatterns(getMatchPatterns(strings));
70      }
71  
72      private static MatchPattern[] getMatchPatterns(Iterable<String> items) {
73          List<MatchPattern> result = new ArrayList<MatchPattern>();
74          for (String string : items) {
75              result.add(MatchPattern.fromString(string));
76          }
77          return result.toArray(new MatchPattern[0]);
78      }
79  }