Class CommonUtil

    • Constructor Detail

      • CommonUtil

        private CommonUtil()
        Stop instances being created.
    • Method Detail

      • createPattern

        public static Pattern createPattern​(String pattern)
        Helper method to create a regular expression.
        Parameters:
        pattern - the pattern to match
        Returns:
        a created regexp object
        Throws:
        IllegalArgumentException - if unable to create Pattern object.
      • createPattern

        public static Pattern createPattern​(String pattern,
                                            int flags)
        Helper method to create a regular expression with a specific flags.
        Parameters:
        pattern - the pattern to match
        flags - the flags to set
        Returns:
        a created regexp object
        Throws:
        IllegalArgumentException - if unable to create Pattern object.
      • matchesFileExtension

        public static boolean matchesFileExtension​(File file,
                                                   String... fileExtensions)
        Returns whether the file extension matches what we are meant to process.
        Parameters:
        file - the file to be checked.
        fileExtensions - files extensions, empty property in config makes it matches to all.
        Returns:
        whether there is a match.
      • hasWhitespaceBefore

        public static boolean hasWhitespaceBefore​(int index,
                                                  String line)
        Returns whether the specified string contains only whitespace up to the specified index.
        Parameters:
        index - index to check up to
        line - the line to check
        Returns:
        whether there is only whitespace
      • lengthMinusTrailingWhitespace

        public static int lengthMinusTrailingWhitespace​(String line)
        Returns the length of a string ignoring all trailing whitespace. It is a pity that there is not a trim() like method that only removed the trailing whitespace.
        Parameters:
        line - the string to process
        Returns:
        the length of the string ignoring all trailing whitespace
      • lengthExpandedTabs

        public static int lengthExpandedTabs​(String inputString,
                                             int toIdx,
                                             int tabWidth)
        Returns the length of a String prefix with tabs expanded. Each tab is counted as the number of characters is takes to jump to the next tab stop.
        Parameters:
        inputString - the input String
        toIdx - index in string (exclusive) where the calculation stops
        tabWidth - the distance between tab stop position.
        Returns:
        the length of string.substring(0, toIdx) with tabs expanded.
      • isPatternValid

        public static boolean isPatternValid​(String pattern)
        Validates whether passed string is a valid pattern or not.
        Parameters:
        pattern - string to validate
        Returns:
        true if the pattern is valid false otherwise
      • baseClassName

        public static String baseClassName​(String type)
        Returns base class name from qualified name.
        Parameters:
        type - the fully qualified name. Cannot be null
        Returns:
        the base class name from a fully qualified name
      • relativizeAndNormalizePath

        public static String relativizeAndNormalizePath​(String baseDirectory,
                                                        String path)
        Constructs a normalized relative path between base directory and a given path.
        Parameters:
        baseDirectory - the base path to which given path is relativized
        path - the path to relativize against base directory
        Returns:
        the relative normalized path between base directory and path or path if base directory is null.
      • startsWithChar

        public static boolean startsWithChar​(String value,
                                             char prefix)
        Tests if this string starts with the specified prefix.

        It is faster version of String.startsWith(String) optimized for one-character prefixes at the expense of some readability. Suggested by SimplifyStartsWith PMD rule: http://pmd.sourceforge.net/pmd-5.3.1/pmd-java/rules/java/optimizations.html#SimplifyStartsWith

        Parameters:
        value - the String to check
        prefix - the prefix to find
        Returns:
        true if the char is a prefix of the given String; false otherwise.
      • endsWithChar

        public static boolean endsWithChar​(String value,
                                           char suffix)
        Tests if this string ends with the specified suffix.

        It is faster version of String.endsWith(String) optimized for one-character suffixes at the expense of some readability. Suggested by SimplifyStartsWith PMD rule: http://pmd.sourceforge.net/pmd-5.3.1/pmd-java/rules/java/optimizations.html#SimplifyStartsWith

        Parameters:
        value - the String to check
        suffix - the suffix to find
        Returns:
        true if the char is a suffix of the given String; false otherwise.
      • getConstructor

        public static <T> Constructor<T> getConstructor​(Class<T> targetClass,
                                                        Class<?>... parameterTypes)
        Gets constructor of targetClass.
        Type Parameters:
        T - type of the target class object.
        Parameters:
        targetClass - from which constructor is returned
        parameterTypes - of constructor
        Returns:
        constructor of targetClass
        Throws:
        IllegalStateException - if any exception occurs
        See Also:
        Class.getConstructor(Class[])
      • close

        public static void close​(Closeable closeable)
        Closes a stream re-throwing IOException as IllegalStateException.
        Parameters:
        closeable - Closeable object
        Throws:
        IllegalStateException - when any IOException occurs
      • getWebOrFileProtocolUri

        public static URI getWebOrFileProtocolUri​(String filename)
        Resolves the specified filename containing 'http', 'https', 'ftp', and 'file' protocols (or any RFC 2396 compliant URL) to a URI.
        Parameters:
        filename - name of the file
        Returns:
        resolved file URI or null if URL is malformed or non-existent
      • getFilepathOrClasspathUri

        private static URI getFilepathOrClasspathUri​(String filename)
                                              throws CheckstyleException
        Resolves the specified local filename, possibly with 'classpath:' protocol, to a URI. First we attempt to create a new file with given filename, then attempt to load file from class path.
        Parameters:
        filename - name of the file
        Returns:
        resolved file URI
        Throws:
        CheckstyleException - on failure
      • getCheckstyleResource

        public static URL getCheckstyleResource​(String name)
        Finds a resource with a given name in the Checkstyle resource bundle. This method is intended only for internal use in Checkstyle tests for easy mocking to gain 100% coverage.
        Parameters:
        name - name of the desired resource
        Returns:
        URI of the resource
      • fillTemplateWithStringsByRegexp

        public static String fillTemplateWithStringsByRegexp​(String template,
                                                             String lineToPlaceInTemplate,
                                                             Pattern regexp)
        Puts part of line, which matches regexp into given template on positions $n where 'n' is number of matched part in line.
        Parameters:
        template - the string to expand.
        lineToPlaceInTemplate - contains expression which should be placed into string.
        regexp - expression to find in comment.
        Returns:
        the string, based on template filled with given lines
      • getFileNameWithoutExtension

        public static String getFileNameWithoutExtension​(String fullFilename)
        Returns file name without extension. We do not use the method from Guava library to reduce Checkstyle's dependencies on external libraries.
        Parameters:
        fullFilename - file name with extension.
        Returns:
        file name without extension.
      • getFileExtension

        public static String getFileExtension​(String fileNameWithExtension)
        Returns file extension for the given file name or empty string if file does not have an extension. We do not use the method from Guava library to reduce Checkstyle's dependencies on external libraries.
        Parameters:
        fileNameWithExtension - file name with extension.
        Returns:
        file extension for the given file name or empty string if file does not have an extension.
      • isIdentifier

        public static boolean isIdentifier​(String str)
        Checks whether the given string is a valid identifier.
        Parameters:
        str - A string to check.
        Returns:
        true when the given string contains valid identifier.
      • isName

        public static boolean isName​(String str)
        Checks whether the given string is a valid name.
        Parameters:
        str - A string to check.
        Returns:
        true when the given string contains valid name.
      • isBlank

        public static boolean isBlank​(String value)
        Checks if the value arg is blank by either being null, empty, or contains only whitespace characters.
        Parameters:
        value - A string to check.
        Returns:
        true if the arg is blank.
      • indexOfNonWhitespace

        public static int indexOfNonWhitespace​(String value)
        Method to find the index of the first non-whitespace character in a string.
        Parameters:
        value - the string to find the first index of a non-whitespace character for.
        Returns:
        the index of the first non-whitespace character.
      • isInt

        public static boolean isInt​(String str)
        Checks whether the string contains an integer value.
        Parameters:
        str - a string to check
        Returns:
        true if the given string is an integer, false otherwise.
      • isCodePointWhitespace

        public static boolean isCodePointWhitespace​(int[] codePoints,
                                                    int index)
        Converts the Unicode code point at index index to it's UTF-16 representation, then checks if the character is whitespace. Note that the given index index should correspond to the location of the character to check in the string, not in code points.
        Parameters:
        codePoints - the array of Unicode code points
        index - the index of the character to check
        Returns:
        true if character at index is whitespace