IMatcher.java

  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. import org.eclipse.jgit.annotations.NonNull;
  12. import org.eclipse.jgit.errors.InvalidPatternException;
  13. import org.eclipse.jgit.ignore.internal.PathMatcher;

  14. /**
  15.  * Generic path matcher.
  16.  *
  17.  * @since 5.7
  18.  */
  19. public interface IMatcher {

  20.     /**
  21.      * Matcher that does not match any pattern.
  22.      */
  23.     public static final IMatcher NO_MATCH = new IMatcher() {

  24.         @Override
  25.         public boolean matches(String path, boolean assumeDirectory,
  26.                 boolean pathMatch) {
  27.             return false;
  28.         }

  29.         @Override
  30.         public boolean matches(String segment, int startIncl, int endExcl) {
  31.             return false;
  32.         }
  33.     };

  34.     /**
  35.      * Creates a path matcher for the given pattern. A pattern may contain the
  36.      * wildcards "?", "*", and "**". The directory separator is '/'.
  37.      *
  38.      * @param pattern
  39.      *            to match
  40.      * @param dirOnly
  41.      *            whether to match only directories
  42.      * @return a matcher for the given pattern
  43.      * @throws InvalidPatternException
  44.      *             if the pattern is invalid
  45.      */
  46.     @NonNull
  47.     public static IMatcher createPathMatcher(@NonNull String pattern,
  48.             boolean dirOnly) throws InvalidPatternException {
  49.         return PathMatcher.createPathMatcher(pattern,
  50.                 Character.valueOf(FastIgnoreRule.PATH_SEPARATOR), dirOnly);
  51.     }

  52.     /**
  53.      * Matches entire given string
  54.      *
  55.      * @param path
  56.      *            string which is not null, but might be empty
  57.      * @param assumeDirectory
  58.      *            true to assume this path as directory (even if it doesn't end
  59.      *            with a slash)
  60.      * @param pathMatch
  61.      *            {@code true} if the match is for the full path: prefix-only
  62.      *            matches are not allowed
  63.      * @return true if this matcher pattern matches given string
  64.      */
  65.     boolean matches(String path, boolean assumeDirectory, boolean pathMatch);

  66.     /**
  67.      * Matches only part of given string
  68.      *
  69.      * @param segment
  70.      *            string which is not null, but might be empty
  71.      * @param startIncl
  72.      *            start index, inclusive
  73.      * @param endExcl
  74.      *            end index, exclusive
  75.      * @return true if this matcher pattern matches given string
  76.      */
  77.     boolean matches(String segment, int startIncl, int endExcl);
  78. }