Class WildcardPattern


  • @ThreadSafe
    public class WildcardPattern
    extends java.lang.Object
    Implementation of Ant-style matching patterns. Contrary to other implementations (like AntPathMatcher from Spring Framework) it is based on Java Regular Expressions. To increase performance it holds an internal cache of all processed patterns.

    Following rules are applied:

    • ? matches single character
    • * matches zero or more characters
    • ** matches zero or more 'directories'

    Some examples of patterns:

    • org/T?st.java - matches org/Test.java and also org/Tost.java
    • org/*.java - matches all .java files in the org directory, e.g. org/Foo.java or org/Bar.java
    • org/** - matches all files underneath the org directory, e.g. org/Foo.java or org/foo/bar.jsp
    • org/**/Test.java - matches all Test.java files underneath the org directory, e.g. org/Test.java or org/foo/Test.java or org/foo/bar/Test.java
    • org/**/*.java - matches all .java files underneath the org directory, e.g. org/Foo.java or org/foo/Bar.java or org/foo/bar/Baz.java

    Another implementation, which is also based on Java Regular Expressions, can be found in FileUtil from IntelliJ OpenAPI.

    Since:
    1.10
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected WildcardPattern​(java.lang.String pattern, java.lang.String directorySeparator)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static WildcardPattern create​(java.lang.String pattern)
      Creates pattern with "/" as a directory separator.
      static WildcardPattern[] create​(java.lang.String[] patterns)
      Creates array of patterns with "/" as a directory separator.
      static WildcardPattern create​(java.lang.String pattern, java.lang.String directorySeparator)
      Creates pattern with specified separator for directories.
      boolean match​(java.lang.String value)
      Returns true if specified value matches this pattern.
      static boolean match​(WildcardPattern[] patterns, java.lang.String value)
      Returns true if specified value matches one of specified patterns.
      java.lang.String toString()
      Returns string representation of this pattern.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • WildcardPattern

        protected WildcardPattern​(java.lang.String pattern,
                                  java.lang.String directorySeparator)
    • Method Detail

      • toString

        public java.lang.String toString()
        Returns string representation of this pattern.
        Overrides:
        toString in class java.lang.Object
        Since:
        2.5
      • match

        public boolean match​(java.lang.String value)
        Returns true if specified value matches this pattern.
      • match

        public static boolean match​(WildcardPattern[] patterns,
                                    java.lang.String value)
        Returns true if specified value matches one of specified patterns.
        Since:
        2.4
      • create

        public static WildcardPattern[] create​(@Nullable
                                               java.lang.String[] patterns)
        Creates array of patterns with "/" as a directory separator.
        See Also:
        create(String, String)
      • create

        public static WildcardPattern create​(java.lang.String pattern,
                                             java.lang.String directorySeparator)
        Creates pattern with specified separator for directories.

        This is used to match Java-classes, i.e. org.foo.Bar against org/**. However usage of character other than "/" as a directory separator is misleading and should be avoided, so method create(String) is preferred over this one.

        Also note that no matter whether forward or backward slashes were used in the antPattern the returned pattern will use directorySeparator. Thus to match Windows-style path "dir\file.ext" against pattern "dir/file.ext" normalization should be performed.