Class Util
- java.lang.Object
-
- com.thomasjensen.checkstyle.addons.util.Util
-
public final class Util extends Object
Utility class.
-
-
Field Summary
Fields Modifier and Type Field Description static Pattern
NEVER_MATCH
A pattern that never matches (and does so efficiently)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static File
canonize(File pFile)
Calls getCanonicalFile() on the given File; if that doesn't work, call getAbsoluteFile() on it.static void
closeQuietly(Closeable pCloseable)
Quietly close the given resource, ignoring any exceptions.static boolean
containsString(Iterable<String> pIterable, String pSearched, boolean pCaseSensitive)
Search for a String in a collection.static DetailAST
findLeftMostTokenInLine(DetailAST pAst)
Find the left-most token in the given AST.static String
getFirstIdent(DetailAST pAst)
Determine the text of the first direct IDENT child node.static String
getFullIdent(DetailAST pAst)
Determine the full identifier of the current element on the AST.static byte[]
readBytes(InputStream pInputStream)
Read all bytes from an InputStream into a byte array.static String
standardizeSlashes(String pPath)
Standardizes all slashes and backslashes in the given String toFile.separatorChar
.static boolean
stringEquals(String pStr1, String pStr2, boolean pCaseSensitive)
Determine the equality of two Strings, optionally ignoring case.static <E> Set<E>
union(Set<E> pColl1, Set<E> pColl2)
Creates a new immutableHashSet
which contains a union of the two given sets.static <E extends Enum<E>>
EvalueOfIgnoreCase(String pValue, Class<E> pEnumClass)
Variant ofEnum.valueOf(java.lang.Class<T>, java.lang.String)
that ignores value case.
-
-
-
Field Detail
-
NEVER_MATCH
public static final Pattern NEVER_MATCH
A pattern that never matches (and does so efficiently)
-
-
Method Detail
-
getFirstIdent
@CheckForNull public static String getFirstIdent(@Nonnull DetailAST pAst)
Determine the text of the first direct IDENT child node.- Parameters:
pAst
- an AST- Returns:
- the first encountered IDENT text, or
null
if none was found
-
getFullIdent
@CheckForNull public static String getFullIdent(@Nonnull DetailAST pAst)
Determine the full identifier of the current element on the AST. The identifier is built from DOT and IDENT elements found directly below the specified element. Other elements encountered are ignored.- Parameters:
pAst
- an AST- Returns:
- the full identifier constructed from either the first encountered IDENT or DOT;
null
if no identifier could be constructed
-
closeQuietly
public static void closeQuietly(@Nullable Closeable pCloseable)
Quietly close the given resource, ignoring any exceptions.- Parameters:
pCloseable
- the resource to close
-
readBytes
public static byte[] readBytes(@Nonnull InputStream pInputStream) throws IOException
Read all bytes from an InputStream into a byte array. Can be replaced withFiles.readAllBytes()
once the code is migrated to Java 7.- Parameters:
pInputStream
- the input stream- Returns:
- the complete contents read from the input stream
- Throws:
IOException
- I/O error
-
canonize
@Nonnull public static File canonize(@Nonnull File pFile)
Calls getCanonicalFile() on the given File; if that doesn't work, call getAbsoluteFile() on it. Thus, the resulting file is not guaranteed to exist. Separator characters are standardized toFile.separatorChar
.- Parameters:
pFile
- a file- Returns:
- the canonical representation, or the absolute representation
-
standardizeSlashes
@Nonnull public static String standardizeSlashes(@Nonnull String pPath)
Standardizes all slashes and backslashes in the given String toFile.separatorChar
.- Parameters:
pPath
- a String- Returns:
- a new String which is
pPath
with standardized path separator characters
-
containsString
public static boolean containsString(@Nullable Iterable<String> pIterable, @Nullable String pSearched, boolean pCaseSensitive)
Search for a String in a collection.- Parameters:
pIterable
- the list of String to be searchedpSearched
- the String to search forpCaseSensitive
- if comparisons should be case sensitive- Returns:
true
if found,false
otherwise
-
stringEquals
public static boolean stringEquals(@Nullable String pStr1, @Nullable String pStr2, boolean pCaseSensitive)
Determine the equality of two Strings, optionally ignoring case.- Parameters:
pStr1
- the first StringpStr2
- the second StringpCaseSensitive
- if the comparison should be case sensitive- Returns:
true
if the String are equal,false
otherwise
-
union
@Nonnull public static <E> Set<E> union(@Nullable Set<E> pColl1, @Nullable Set<E> pColl2)
Creates a new immutableHashSet
which contains a union of the two given sets.- Type Parameters:
E
- type of all contained elements- Parameters:
pColl1
- first setpColl2
- second set- Returns:
- union set
-
findLeftMostTokenInLine
@Nonnull public static DetailAST findLeftMostTokenInLine(@Nonnull DetailAST pAst)
Find the left-most token in the given AST. The left-most token is the token with the smallest column number. Only tokens which are located on the same line as the given AST are considered.- Parameters:
pAst
- the root of a subtree. This token is also considered for the result.- Returns:
- the left-most token
-
valueOfIgnoreCase
@Nonnull public static <E extends Enum<E>> E valueOfIgnoreCase(@Nonnull String pValue, @Nonnull Class<E> pEnumClass)
Variant ofEnum.valueOf(java.lang.Class<T>, java.lang.String)
that ignores value case.- Type Parameters:
E
- the enum type- Parameters:
pValue
- the String valuepEnumClass
- the class object of the enum type- Returns:
- the enum value
- Throws:
IllegalArgumentException
- the given String value does not match a valid enum value
-
-