Class WildcardUtil

java.lang.Object
eu.cloudnetservice.node.impl.util.WildcardUtil

public final class WildcardUtil extends Object
Small utility class which supports pattern based searching through given input collections and tries to fix mistakes in the given pattern input. Mostly used to match stuff based on (sometimes) invalid user supplied pattern input.
Since:
4.0
  • Field Details

    • GROUP_CHARS

      private static final char[] GROUP_CHARS
  • Constructor Details

    • WildcardUtil

      private WildcardUtil()
  • Method Details

    • filterWildcard

      @NonNull public static <T extends eu.cloudnetservice.driver.base.Named> @NonNull @Unmodifiable Collection<T> filterWildcard(@NonNull @NonNull Collection<T> inputValues, @NonNull @NonNull String regex)
      Filters all values out of the given inputValues which are matching the given pattern.
      Type Parameters:
      T - the type of the input values.
      Parameters:
      inputValues - the input values to search trough.
      regex - the regex to use for searching.
      Returns:
      all input values matching the given pattern.
      Throws:
      NullPointerException - if the given input collection or regex string is null.
    • anyMatch

      public static boolean anyMatch(@NonNull @NonNull Collection<? extends eu.cloudnetservice.driver.base.Named> values, @NonNull @NonNull String regex)
      Checks if any of the given values matches the given regex.
      Parameters:
      values - the values to search trough.
      regex - the regex to use for searching.
      Returns:
      true if any of the values matches the given regex, false otherwise.
      Throws:
      NullPointerException - if the given value collection or regex string is null.
    • filterWildcard

      @NonNull public static <T extends eu.cloudnetservice.driver.base.Named> @NonNull @Unmodifiable Collection<T> filterWildcard(@NonNull @NonNull Collection<T> inputValues, @NonNull @NonNull String regex, boolean caseSensitive)
      Filters all values out of the given inputValues which are matching the given pattern. If the given regex input cannot be parsed the given literal string will be used for matching instead.
      Type Parameters:
      T - the type of the input values.
      Parameters:
      inputValues - the input values to search trough.
      regex - the regex to use for searching.
      caseSensitive - if the search should be case-sensitive.
      Returns:
      all input values matching the given pattern.
      Throws:
      NullPointerException - if the given input collection or regex string is null.
    • anyMatch

      public static boolean anyMatch(@NonNull @NonNull Collection<? extends eu.cloudnetservice.driver.base.Named> values, @NonNull @NonNull String regex, boolean caseSensitive)
      Checks if any of the given values matches the given regex. If the given regex input cannot be parsed the given literal string will be used for matching instead.
      Parameters:
      values - the values to search trough.
      regex - the regex to use for searching.
      caseSensitive - if the search should be case-sensitive.
      Returns:
      true if any of the values matches the given regex, false otherwise.
      Throws:
      NullPointerException - if the given input collection or regex string is null.
    • fixPattern

      @Nullable public static @Nullable Pattern fixPattern(@NonNull @NonNull String regex, boolean caseSensitive)
      Prepares the given regex input by replacing all wildcard characters with pattern-syntax wildcard chars. This method will then try to compile the regex and automatically fix syntax errors in it. Null is returned when the given input can neither be compiled nor fixed.
      Parameters:
      regex - the regex string to prepare and compile.
      caseSensitive - if the pattern should be case-insensitive.
      Returns:
      the compiled pattern or null if the compilation failed.
      Throws:
      NullPointerException - if the given regex string is null.
    • tryCompile

      @Nullable private static @Nullable Pattern tryCompile(@NonNull @NonNull String pattern, boolean caseSensitive)
      Compiles the given pattern string and (when needed) tries to automatically fix syntax errors in it. This method returns null if the given input could not be parsed nor fixed.
      Parameters:
      pattern - the pattern string to compile.
      caseSensitive - if the pattern should be case-insensitive.
      Returns:
      the compiled pattern or null if the compilation failed.
      Throws:
      NullPointerException - if the given pattern string is null.
    • tryFixPattern

      @Nullable private static @Nullable Pattern tryFixPattern(@NonNull @NonNull PatternSyntaxException exception, boolean caseSensitive)
      Tries to automatically fix a pattern based on the given exception.
      Parameters:
      exception - the exception occurred during compilation of the pattern string.
      caseSensitive - if the pattern check should be case-insensitive.
      Returns:
      a fixed, compiled version of the pattern or null if the given exception is unclear.
      Throws:
      NullPointerException - if the given syntax exception is null.
    • fixUnclosedGroups

      @VisibleForTesting @Nullable static @Nullable String fixUnclosedGroups(@NonNull @NonNull String patternInput, int idx)
      Searches for unclosed group chars based on the given error description.
      Parameters:
      patternInput - the pattern to check.
      Returns:
      the same pattern as given but with fixed groups, null if fixing the group input was not possible.
      Throws:
      NullPointerException - if the given pattern input or error description is null.
    • isPartOfPattern

      private static boolean isPartOfPattern(char[] content, int index)
      Checks if the current content index is part of the pattern or escaped.
      Parameters:
      content - the whole content of the pattern as char array.
      index - the current reader index of the char to check.
      Returns:
      true if the char is part of the pattern or false if escaped.
    • matches

      private static boolean matches(@NonNull @NonNull String patternInput, @Nullable @Nullable Pattern compiled, @NonNull @NonNull eu.cloudnetservice.driver.base.Named data, boolean caseSensitive)
      Tries to match the given data name using the compiled pattern, falling back to a direct equal check if the given compiled pattern is null.
      Parameters:
      patternInput - the input to create the pattern, used for raw checking if needed.
      compiled - the compiled pattern from the given input, null if not able to compile.
      data - the data to check if it matches the given regex.
      caseSensitive - if the match check should be case-sensitive.
      Returns:
      true if the given data name matches the given regex, false otherwise.
      Throws:
      NullPointerException - if the given pattern input or data is null.