public abstract class Utils extends Object
| 限定符和类型 | 类和说明 |
|---|---|
static class |
Utils.MutableArray<T> |
| 构造器和说明 |
|---|
Utils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
assertIsTrue(boolean expression,
Supplier<String> messageSupplier)
Assert a boolean expression, throwing an
IllegalArgumentException
if the expression evaluates to false. |
static void |
assertNotBlank(String text,
String message)
Assert that the given String contains valid text content; that is, it must not
be
null and must contain at least one non-whitespace character. |
static void |
assertNotNull(Object object,
String message)
Assert whether the object is
null. |
static String |
bytesToText(byte[] bytes)
Byte[] about
|
static byte[] |
clone(byte[] array)
Clones an array returning a typecast result and handling
null. |
static Set<String> |
commaDelimitedListToSet(String str)
Convert a comma delimited list (e.g., a row from a CSV file) into a set.
|
static String[] |
commaDelimitedListToStringArray(String str)
Convert a comma delimited list (e.g., a row from a CSV file) into an
array of strings.
|
static String |
deleteAny(String inString,
String charsToDelete)
Delete any character in a given
String. |
static String[] |
delimitedListToStringArray(String str,
String delimiter)
Take a
String that is a delimited list and convert it into a
String array. |
static String[] |
delimitedListToStringArray(String str,
String delimiter,
String charsToDelete)
Take a
String that is a delimited list and convert it into
a String array. |
static Field |
findField(Class<?> clazz,
String name)
|
static Field |
findField(Class<?> clazz,
String name,
Class<?> type)
|
static Method |
findMethod(Class<?> clazz,
String name)
Attempt to find a
Method on the supplied class with the supplied name
and no parameters. |
static Method |
findMethod(Class<?> clazz,
String name,
Class<?>... argTypes)
Attempt to find a
Method on the supplied class with the supplied name
and parameter types. |
static <T> T |
getFieldValue(Object target,
String name,
Class<T> type)
Get the field represented by the supplied
field object on the
specified target object. |
static boolean |
hasLength(String str)
Check that the given
String is neither null nor of length 0. |
static byte[] |
intToBytes(int number)
numeric
long to byte array. |
static Object |
invokeMethod(Object target,
String methodName) |
static Object |
invokeMethod(Object target,
String methodName,
Class<?>[] argTypes,
Object... args) |
static Object |
invokeMethod(Object target,
String methodName,
Class<?> argType,
Object arg) |
static boolean |
isBlank(CharSequence cs)
Whether the CharSequence is blank.
|
static boolean |
isEmpty(byte[] array)
Whether the byte array is empty.
|
static boolean |
isEmpty(byte[][] array)
Whether the byte[] array is empty.
|
static boolean |
isEmpty(CharSequence cs)
Whether the CharSequence is empty.
|
static boolean |
isEmpty(Collection<?> collection)
Return
true if the supplied Collection is null or empty. |
static boolean |
isNotBlank(CharSequence cs)
Whether the CharSequence is not blank.
|
static boolean |
isNotEmpty(CharSequence cs)
Whether the CharSequence is not empty.
|
static byte[] |
longToBytes(long number) |
static void |
makeAccessible(Field field)
Make the given field accessible, explicitly setting it accessible if
necessary.
|
static void |
makeAccessible(Method method) |
static byte[] |
mergeAll(byte[] array1,
byte... array2)
Merges all the elements of the given arrays into a new array.
|
static <T> Utils.MutableArray<T> |
newMutableArray(T... elements) |
static boolean |
nullSafeEquals(Object o1,
Object o2)
Determine if the given objects are equal, returning
true if
both are null or false if only one is null. |
static int |
nullSafeHashCode(boolean[] array)
Return a hash code based on the contents of the specified array.
|
static int |
nullSafeHashCode(byte[] array)
Return a hash code based on the contents of the specified array.
|
static int |
nullSafeHashCode(char[] array)
Return a hash code based on the contents of the specified array.
|
static int |
nullSafeHashCode(double[] array)
Return a hash code based on the contents of the specified array.
|
static int |
nullSafeHashCode(float[] array)
Return a hash code based on the contents of the specified array.
|
static int |
nullSafeHashCode(int[] array)
Return a hash code based on the contents of the specified array.
|
static int |
nullSafeHashCode(long[] array)
Return a hash code based on the contents of the specified array.
|
static int |
nullSafeHashCode(Object obj)
Return as hash code for the given object; typically the value of
Object#hashCode()}. |
static int |
nullSafeHashCode(Object[] array)
Return a hash code based on the contents of the specified array.
|
static int |
nullSafeHashCode(short[] array)
Return a hash code based on the contents of the specified array.
|
static <T> void |
setFieldValue(Object target,
String name,
Class<T> type,
Object value) |
static String[] |
split(String toSplit,
String delimiter)
Split a
String at the first occurrence of the delimiter. |
static byte[] |
subarray(byte[] array,
int startIndexInclusive,
int endIndexExclusive)
Produces a new
byte array containing the elements between the start
and end indices. |
static String[] |
toStringArray(Collection<String> collection)
Copy the given
Collection into a String array. |
public static <T> Utils.MutableArray<T> newMutableArray(T... elements)
public static void assertNotBlank(String text, String message)
null and must contain at least one non-whitespace character.
Utils.assertNotBlank(name, "'name' must not be empty");
public static void assertNotNull(Object object, String message)
null.public static void assertIsTrue(boolean expression,
Supplier<String> messageSupplier)
IllegalArgumentException
if the expression evaluates to false.
Utils.assertIsTrue(i > 0, () -> "The value '" + i + "' must be greater than zero");
public static boolean isEmpty(CharSequence cs)
public static boolean isNotEmpty(CharSequence cs)
public static boolean isBlank(CharSequence cs)
public static boolean isNotBlank(CharSequence cs)
public static String[] split(String toSplit, String delimiter)
String at the first occurrence of the delimiter.
Does not include the delimiter in the result.toSplit - the string to split (potentially null or empty)delimiter - to split the string up with (potentially null or empty)null if the delimiter wasn't found in the given input Stringpublic static Set<String> commaDelimitedListToSet(String str)
Note that this will suppress duplicates, and as of 4.2, the elements in
the returned set will preserve the original order in a LinkedHashSet.
str - the input String (potentially null or empty)String entries in the list#removeDuplicateStrings(String[])public static String[] commaDelimitedListToStringArray(String str)
str - the input String (potentially null or empty)public static String[] delimitedListToStringArray(String str, String delimiter)
String that is a delimited list and convert it into a
String array.
A single delimiter may consist of more than one character,
but it will still be considered as a single delimiter string, rather
than as bunch of potential delimiter characters, in contrast to
#tokenizeToStringArray.
str - the input String (potentially null or empty)delimiter - the delimiter between elements (this is a single delimiter,
rather than a bunch individual delimiter characters)#tokenizeToStringArraypublic static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete)
String that is a delimited list and convert it into
a String array.
A single delimiter may consist of more than one character,
but it will still be considered as a single delimiter string, rather
than as bunch of potential delimiter characters, in contrast to
#tokenizeToStringArray.
str - the input String (potentially null or empty)delimiter - the delimiter between elements (this is a single delimiter,
rather than a bunch individual delimiter characters)charsToDelete - a set of characters to delete; useful for deleting unwanted
line breaks: e.g. "\r\n\f" will delete all new lines and line feeds in a String#tokenizeToStringArraypublic static String[] toStringArray(Collection<String> collection)
collection - the Collection to copy
(potentially null or empty)String arraypublic static boolean isEmpty(Collection<?> collection)
true if the supplied Collection is null or empty.
Otherwise, return false.collection - the Collection to checkpublic static String deleteAny(String inString, String charsToDelete)
String.inString - the original StringcharsToDelete - a set of characters to delete.
E.g. "az\n" will delete 'a's, 'z's and new lines.Stringpublic static boolean hasLength(String str)
String is neither null nor of length 0.
Note: this method returns true for a String that
purely consists of whitespace.
str - the String to check (may be null)true if the String is not null and has length#hasLength(CharSequence),
#hasText(String)public static String bytesToText(byte[] bytes)
public static byte[] intToBytes(int number)
long to byte array.public static byte[] longToBytes(long number)
public static byte[] clone(byte[] array)
null.public static boolean isEmpty(byte[] array)
public static boolean isEmpty(byte[][] array)
public static byte[] mergeAll(byte[] array1,
byte... array2)
public static byte[] subarray(byte[] array,
int startIndexInclusive,
int endIndexExclusive)
byte array containing the elements between the start
and end indices.public static int nullSafeHashCode(Object obj)
Object#hashCode()}. If the object is an array,
this method will delegate to any of the nullSafeHashCode
methods for arrays in this class. If the object is null,
this method returns 0.public static int nullSafeHashCode(Object[] array)
array is null, this method returns 0.public static int nullSafeHashCode(boolean[] array)
array is null, this method returns 0.public static int nullSafeHashCode(byte[] array)
array is null, this method returns 0.public static int nullSafeHashCode(char[] array)
array is null, this method returns 0.public static int nullSafeHashCode(double[] array)
array is null, this method returns 0.public static int nullSafeHashCode(float[] array)
array is null, this method returns 0.public static int nullSafeHashCode(int[] array)
array is null, this method returns 0.public static int nullSafeHashCode(long[] array)
array is null, this method returns 0.public static int nullSafeHashCode(short[] array)
array is null, this method returns 0.public static boolean nullSafeEquals(Object o1, Object o2)
true if
both are null or false if only one is null.
Compares arrays with Arrays.equals, performing an equality
check based on the array elements rather than the array reference.
o1 - first Object to compareo2 - second Object to compareObject.equals(Object),
Arrays.equals(long[], long[])public static Field findField(Class<?> clazz, String name)
field on the supplied Class with the
supplied name. Searches all super-classes up to Object.clazz - the class to introspectname - the name of the fieldnull if not foundpublic static <T> T getFieldValue(Object target, String name, Class<T> type)
field object on the
specified target object. In accordance with Field.get(Object)
semantics, the returned value is automatically wrapped if the underlying field
has a primitive type.T - name - the field name to gettarget - the target object from which to get the fieldpublic static <T> void setFieldValue(Object target, String name, Class<T> type, Object value)
public static void makeAccessible(Field field)
setAccessible(true) method is only called when
actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).field - the field to make accessibleAccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)public static Field findField(Class<?> clazz, String name, Class<?> type)
field on the supplied Class with the
supplied name and/or type. Searches all super-classes up to Object.clazz - the class to introspectname - the name of the field (may be null if type is specified)type - the type of the field (may be null if name is specified)null if not foundpublic static Method findMethod(Class<?> clazz, String name)
Method on the supplied class with the supplied name
and no parameters. Searches all superclasses up to Object.
Returns null if no Method can be found.
clazz - the class to introspectname - the name of the methodnull if none foundpublic static Method findMethod(Class<?> clazz, String name, Class<?>... argTypes)
Method on the supplied class with the supplied name
and parameter types. Searches all superclasses up to Object.
Returns null if no Method can be found.
clazz - the class to introspectname - the name of the methodparamTypes - the parameter types of the method
(may be null to indicate any signature)null if none foundpublic static void makeAccessible(Method method)
public static Object invokeMethod(Object target, String methodName, Class<?> argType, Object arg)
Copyright © 2019. All rights reserved.