View Javadoc

1   package org.apache.maven.shared.utils.io;
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.io.File;
23  
24  import javax.annotation.Nonnull;
25  
26  /**
27   * A list of patterns to be matched
28   *
29   * @author Kristian Rosenvold
30   */
31  public class MatchPatterns
32  {
33      private final MatchPattern[] patterns;
34  
35      private MatchPatterns( @Nonnull MatchPattern... patterns )
36      {
37          this.patterns = patterns;
38      }
39  
40      /**
41       * Checks these MatchPatterns against a specified string.
42       * <p/>
43       * Uses far less string tokenization than any of the alternatives.
44       *
45       * @param name            The name to look for
46       * @param isCaseSensitive If the comparison is case sensitive
47       * @return true if any of the supplied patterns match
48       */
49      public boolean matches( @Nonnull String name, boolean isCaseSensitive )
50      {
51          String[] tokenized = MatchPattern.tokenizePathToString( name, File.separator );
52          for ( MatchPattern pattern : patterns )
53          {
54              if ( pattern.matchPath( name, tokenized, isCaseSensitive ) )
55              {
56                  return true;
57              }
58          }
59          return false;
60      }
61  
62      public boolean matchesPatternStart( @Nonnull String name, boolean isCaseSensitive )
63      {
64          for ( MatchPattern includesPattern : patterns )
65          {
66              if ( includesPattern.matchPatternStart( name, isCaseSensitive ) )
67              {
68                  return true;
69              }
70          }
71          return false;
72      }
73  
74      public static MatchPatterns from( @Nonnull String... sources )
75      {
76          final int length = sources.length;
77          MatchPattern[] result = new MatchPattern[length];
78          for ( int i = 0; i < length; i++ )
79          {
80              result[i] = MatchPattern.fromString( sources[i] );
81          }
82          return new MatchPatterns( result );
83      }
84  
85  }