public final class CommonUtil extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
CLASSPATH_URL_PROTOCOL
Pseudo URL protocol for loading from the class path.
|
static int |
DEFAULT_TAB_WIDTH
Default tab width for column reporting.
|
static byte[] |
EMPTY_BYTE_ARRAY
Copied from org.apache.commons.lang3.ArrayUtils.
|
static double[] |
EMPTY_DOUBLE_ARRAY
Copied from org.apache.commons.lang3.ArrayUtils.
|
static int[] |
EMPTY_INT_ARRAY
Copied from org.apache.commons.lang3.ArrayUtils.
|
static java.lang.Integer[] |
EMPTY_INTEGER_OBJECT_ARRAY
Copied from org.apache.commons.lang3.ArrayUtils.
|
static java.lang.Object[] |
EMPTY_OBJECT_ARRAY
Copied from org.apache.commons.lang3.ArrayUtils.
|
static java.lang.String[] |
EMPTY_STRING_ARRAY
Copied from org.apache.commons.lang3.ArrayUtils.
|
private static java.lang.String |
UNABLE_TO_FIND_EXCEPTION_PREFIX
Prefix for the exception when unable to find resource.
|
| Modifier | Constructor and Description |
|---|---|
private |
CommonUtil()
Stop instances being created.
|
| Modifier and Type | Method and Description |
|---|---|
static java.lang.String |
baseClassName(java.lang.String type)
Returns base class name from qualified name.
|
static void |
close(java.io.Closeable closeable)
Closes a stream re-throwing IOException as IllegalStateException.
|
static java.util.regex.Pattern |
createPattern(java.lang.String pattern)
Helper method to create a regular expression.
|
static java.util.regex.Pattern |
createPattern(java.lang.String pattern,
int flags)
Helper method to create a regular expression with a specific flags.
|
static boolean |
endsWithChar(java.lang.String value,
char suffix)
Tests if this string ends with the specified suffix.
|
static java.lang.String |
fillTemplateWithStringsByRegexp(java.lang.String template,
java.lang.String lineToPlaceInTemplate,
java.util.regex.Pattern regexp)
Puts part of line, which matches regexp into given template
on positions $n where 'n' is number of matched part in line.
|
static java.net.URL |
getCheckstyleResource(java.lang.String name)
Finds a resource with a given name in the Checkstyle resource bundle.
|
static <T> java.lang.reflect.Constructor<T> |
getConstructor(java.lang.Class<T> targetClass,
java.lang.Class<?>... parameterTypes)
Gets constructor of targetClass.
|
static java.lang.String |
getFileExtension(java.lang.String fileNameWithExtension)
Returns file extension for the given file name
or empty string if file does not have an extension.
|
static java.lang.String |
getFileNameWithoutExtension(java.lang.String fullFilename)
Returns file name without extension.
|
private static java.net.URI |
getFilepathOrClasspathUri(java.lang.String filename)
Resolves the specified local filename, possibly with 'classpath:'
protocol, to a URI.
|
static java.net.URI |
getResourceFromClassPath(java.lang.String filename)
Gets a resource from the classpath.
|
static java.net.URI |
getUriByFilename(java.lang.String filename)
Resolve the specified filename to a URI.
|
static java.net.URI |
getWebOrFileProtocolUri(java.lang.String filename)
Resolves the specified filename containing 'http', 'https', 'ftp',
and 'file' protocols (or any RFC 2396 compliant URL) to a URI.
|
static boolean |
hasWhitespaceBefore(int index,
java.lang.String line)
Returns whether the specified string contains only whitespace up to the specified index.
|
static int |
indexOfNonWhitespace(java.lang.String value)
Method to find the index of the first non-whitespace character in a string.
|
static <T> T |
invokeConstructor(java.lang.reflect.Constructor<T> constructor,
java.lang.Object... parameters)
Returns new instance of a class.
|
static boolean |
isBlank(java.lang.String value)
Checks if the value arg is blank by either being null,
empty, or contains only whitespace characters.
|
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. |
static boolean |
isIdentifier(java.lang.String str)
Checks whether the given string is a valid identifier.
|
static boolean |
isInt(java.lang.String str)
Checks whether the string contains an integer value.
|
static boolean |
isName(java.lang.String str)
Checks whether the given string is a valid name.
|
static boolean |
isPatternValid(java.lang.String pattern)
Validates whether passed string is a valid pattern or not.
|
static int |
lengthExpandedTabs(java.lang.String inputString,
int toIdx,
int tabWidth)
Returns the length of a String prefix with tabs expanded.
|
static int |
lengthMinusTrailingWhitespace(java.lang.String line)
Returns the length of a string ignoring all trailing whitespace.
|
static boolean |
matchesFileExtension(java.io.File file,
java.lang.String... fileExtensions)
Returns whether the file extension matches what we are meant to process.
|
static java.lang.String |
relativizeAndNormalizePath(java.lang.String baseDirectory,
java.lang.String path)
Constructs a normalized relative path between base directory and a given path.
|
static boolean |
startsWithChar(java.lang.String value,
char prefix)
Tests if this string starts with the specified prefix.
|
public static final int DEFAULT_TAB_WIDTH
public static final java.lang.String[] EMPTY_STRING_ARRAY
public static final java.lang.Integer[] EMPTY_INTEGER_OBJECT_ARRAY
public static final java.lang.Object[] EMPTY_OBJECT_ARRAY
public static final int[] EMPTY_INT_ARRAY
public static final byte[] EMPTY_BYTE_ARRAY
public static final double[] EMPTY_DOUBLE_ARRAY
public static final java.lang.String CLASSPATH_URL_PROTOCOL
private static final java.lang.String UNABLE_TO_FIND_EXCEPTION_PREFIX
private CommonUtil()
public static java.util.regex.Pattern createPattern(java.lang.String pattern)
pattern - the pattern to matchjava.lang.IllegalArgumentException - if unable to create Pattern object.public static java.util.regex.Pattern createPattern(java.lang.String pattern, int flags)
pattern - the pattern to matchflags - the flags to setjava.lang.IllegalArgumentException - if unable to create Pattern object.public static boolean matchesFileExtension(java.io.File file, java.lang.String... fileExtensions)
file - the file to be checked.fileExtensions - files extensions, empty property in config makes it matches to all.public static boolean hasWhitespaceBefore(int index, java.lang.String line)
index - index to check up toline - the line to checkpublic static int lengthMinusTrailingWhitespace(java.lang.String line)
line - the string to processpublic static int lengthExpandedTabs(java.lang.String inputString, int toIdx, int tabWidth)
inputString - the input StringtoIdx - index in string (exclusive) where the calculation stopstabWidth - the distance between tab stop position.public static boolean isPatternValid(java.lang.String pattern)
pattern - string to validatepublic static java.lang.String baseClassName(java.lang.String type)
type - the fully qualified name. Cannot be nullpublic static java.lang.String relativizeAndNormalizePath(java.lang.String baseDirectory, java.lang.String path)
baseDirectory - the base path to which given path is relativizedpath - the path to relativize against base directorypublic static boolean startsWithChar(java.lang.String value, char 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
value - the String to checkprefix - the prefix to findtrue if the char is a prefix of the given String;
false otherwise.public static boolean endsWithChar(java.lang.String value, char 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
value - the String to checksuffix - the suffix to findtrue if the char is a suffix of the given String;
false otherwise.public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> targetClass, java.lang.Class<?>... parameterTypes)
T - type of the target class object.targetClass - from which constructor is returnedparameterTypes - of constructorjava.lang.IllegalStateException - if any exception occursClass.getConstructor(Class[])public static <T> T invokeConstructor(java.lang.reflect.Constructor<T> constructor, java.lang.Object... parameters)
T - type of constructorconstructor - to invokeparameters - to pass to constructorjava.lang.IllegalStateException - if any exception occursConstructor.newInstance(Object...)public static void close(java.io.Closeable closeable)
closeable - Closeable objectjava.lang.IllegalStateException - when any IOException occurspublic static java.net.URI getUriByFilename(java.lang.String filename) throws CheckstyleException
filename - name of the fileCheckstyleException - on failurepublic static java.net.URI getWebOrFileProtocolUri(java.lang.String filename)
filename - name of the fileprivate static java.net.URI getFilepathOrClasspathUri(java.lang.String filename) throws CheckstyleException
filename - name of the fileCheckstyleException - on failurepublic static java.net.URI getResourceFromClassPath(java.lang.String filename) throws CheckstyleException
filename - name of fileCheckstyleException - on failurepublic static java.net.URL getCheckstyleResource(java.lang.String name)
name - name of the desired resourcepublic static java.lang.String fillTemplateWithStringsByRegexp(java.lang.String template, java.lang.String lineToPlaceInTemplate, java.util.regex.Pattern regexp)
template - the string to expand.lineToPlaceInTemplate - contains expression which should be placed into string.regexp - expression to find in comment.public static java.lang.String getFileNameWithoutExtension(java.lang.String fullFilename)
fullFilename - file name with extension.public static java.lang.String getFileExtension(java.lang.String fileNameWithExtension)
fileNameWithExtension - file name with extension.public static boolean isIdentifier(java.lang.String str)
str - A string to check.public static boolean isName(java.lang.String str)
str - A string to check.public static boolean isBlank(java.lang.String value)
value - A string to check.public static int indexOfNonWhitespace(java.lang.String value)
value - the string to find the first index of a non-whitespace character for.public static boolean isInt(java.lang.String str)
str - a string to checkpublic static boolean isCodePointWhitespace(int[] codePoints, int 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.codePoints - the array of Unicode code pointsindex - the index of the character to checkindex is whitespaceCopyright © 2001-2022. All Rights Reserved.