View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.duplicatefinder.classpath;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  import static java.lang.String.format;
19  
20  import java.util.Collection;
21  import java.util.regex.Pattern;
22  
23  import com.google.common.base.Predicate;
24  import com.google.common.collect.ImmutableList;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  class MatchPatternPredicate implements Predicate<String> {
29  
30      private static final Logger LOG = LoggerFactory.getLogger(MatchPatternPredicate.class);
31  
32      private final ImmutableList<Pattern> patterns;
33  
34      MatchPatternPredicate(final Collection<String> patternStrings) {
35          checkNotNull(patternStrings, "patternStrings is null");
36  
37          final ImmutableList.Builder<Pattern> builder = ImmutableList.builder();
38          for (final String patternString : patternStrings) {
39              builder.add(Pattern.compile(patternString, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE));
40          }
41  
42          this.patterns = builder.build();
43      }
44  
45      public ImmutableList<Pattern> getPatterns() {
46          return patterns;
47      }
48  
49      @Override
50      public boolean apply(final String input) {
51          if (input != null) {
52              for (final Pattern pattern : patterns) {
53                  if (pattern.matcher(input).matches()) {
54                      LOG.debug(format("Ignoring '%s' (matches %s)", input, pattern.pattern()));
55                      return true;
56                  }
57              }
58          }
59          return false;
60      }
61  }