public final class CommonUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
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 |
CommonUtils()
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 <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.
|
static java.net.URI |
getUriByFilename(java.lang.String filename)
Resolve the specified filename 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 <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 |
isIdentifier(java.lang.String str)
Checks whether the given string is a valid identifier.
|
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 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
private static final java.lang.String UNABLE_TO_FIND_EXCEPTION_PREFIX
public static java.util.regex.Pattern createPattern(java.lang.String pattern)
pattern
- the pattern to matchorg.apache.commons.beanutils.ConversionException
- 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 constructorIllegalStateException
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 constructorIllegalStateException
if any exception occursConstructor.newInstance(Object...)
public static void close(java.io.Closeable closeable)
closeable
- Closeable objectpublic static java.net.URI getUriByFilename(java.lang.String filename) throws CheckstyleException
filename
- name os the fileCheckstyleException
- on failurepublic 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.