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 /** 63 * @param name The name. 64 * @param isCaseSensitive being case sensetive. 65 * @return true if any of the supplied patterns match start. 66 */ 67 public boolean matchesPatternStart( @Nonnull String name, boolean isCaseSensitive ) 68 { 69 for ( MatchPattern includesPattern : patterns ) 70 { 71 if ( includesPattern.matchPatternStart( name, isCaseSensitive ) ) 72 { 73 return true; 74 } 75 } 76 return false; 77 } 78 79 /** 80 * @param sources The sources 81 * @return Converted match patterns. 82 */ 83 public static MatchPatterns from( @Nonnull String... sources ) 84 { 85 final int length = sources.length; 86 MatchPattern[] result = new MatchPattern[length]; 87 for ( int i = 0; i < length; i++ ) 88 { 89 result[i] = MatchPattern.fromString( sources[i] ); 90 } 91 return new MatchPatterns( result ); 92 } 93 94 }