1 /* 2 * Copyright (C) 2014, 2020 Andrey Loskutov <loskutov@gmx.de> and others 3 * 4 * This program and the accompanying materials are made available under the 5 * terms of the Eclipse Distribution License v. 1.0 which is available at 6 * https://www.eclipse.org/org/documents/edl-v10.php. 7 * 8 * SPDX-License-Identifier: BSD-3-Clause 9 */ 10 package org.eclipse.jgit.ignore; 11 12 import org.eclipse.jgit.annotations.NonNull; 13 import org.eclipse.jgit.errors.InvalidPatternException; 14 import org.eclipse.jgit.ignore.internal.PathMatcher; 15 16 /** 17 * Generic path matcher. 18 * 19 * @since 5.7 20 */ 21 public interface IMatcher { 22 23 /** 24 * Matcher that does not match any pattern. 25 */ 26 public static final IMatcher NO_MATCH = new IMatcher() { 27 28 @Override 29 public boolean matches(String path, boolean assumeDirectory, 30 boolean pathMatch) { 31 return false; 32 } 33 34 @Override 35 public boolean matches(String segment, int startIncl, int endExcl) { 36 return false; 37 } 38 }; 39 40 /** 41 * Creates a path matcher for the given pattern. A pattern may contain the 42 * wildcards "?", "*", and "**". The directory separator is '/'. 43 * 44 * @param pattern 45 * to match 46 * @param dirOnly 47 * whether to match only directories 48 * @return a matcher for the given pattern 49 * @throws InvalidPatternException 50 * if the pattern is invalid 51 */ 52 @NonNull 53 public static IMatcher createPathMatcher(@NonNull String pattern, 54 boolean dirOnly) throws InvalidPatternException { 55 return PathMatcher.createPathMatcher(pattern, 56 Character.valueOf(FastIgnoreRule.PATH_SEPARATOR), dirOnly); 57 } 58 59 /** 60 * Matches entire given string 61 * 62 * @param path 63 * string which is not null, but might be empty 64 * @param assumeDirectory 65 * true to assume this path as directory (even if it doesn't end 66 * with a slash) 67 * @param pathMatch 68 * {@code true} if the match is for the full path: prefix-only 69 * matches are not allowed 70 * @return true if this matcher pattern matches given string 71 */ 72 boolean matches(String path, boolean assumeDirectory, boolean pathMatch); 73 74 /** 75 * Matches only part of given string 76 * 77 * @param segment 78 * string which is not null, but might be empty 79 * @param startIncl 80 * start index, inclusive 81 * @param endExcl 82 * end index, exclusive 83 * @return true if this matcher pattern matches given string 84 */ 85 boolean matches(String segment, int startIncl, int endExcl); 86 }