public final class Utils
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
CR |
| Modifier and Type | Method and Description |
|---|---|
static void |
closeQuietly(java.io.Closeable closeable) |
static int |
countMatches(java.lang.CharSequence str,
java.lang.CharSequence sub)
Counts how many times the substring appears in the larger string.
|
static java.util.List<java.lang.Integer> |
createRange(int start,
int end)
Creates a range of integers from start (inclusive) to end (exclusive)
|
static java.lang.Object |
deserialize(byte[] objectData)
Deserializes a single
Object from an array of bytes. |
static java.lang.Object |
deserialize(java.io.InputStream inputStream)
Deserializes an
Object from the specified stream. |
static boolean |
isEmpty(java.lang.CharSequence cs)
Checks if a CharSequence is empty ("") or null.
|
static boolean |
isInt(java.lang.String str) |
static boolean |
isNumeric(java.lang.String str) |
static void |
isTrue(boolean expression,
java.lang.String message)
Validate that the argument condition is
true; otherwise
throwing an exception with the specified message. |
static java.lang.String |
join(java.lang.String delimiter,
java.lang.Iterable<? extends java.lang.Object> objs) |
static java.lang.String |
join(java.lang.String delimiter,
java.lang.String wrap,
java.lang.Iterable<? extends java.lang.Object> objs) |
static <T extends java.lang.CharSequence> |
notEmpty(T chars,
java.lang.String message,
java.lang.Object... values)
Validate that the specified argument character sequence is
neither
null nor a length of zero (no characters);
otherwise throwing an exception with the specified message. |
static <T> T |
notNull(T object,
java.lang.String message,
java.lang.Object... values)
Validate that the specified argument is not
null;
otherwise throwing an exception with the specified message. |
static byte[] |
serialize(java.io.Serializable obj)
Serializes an
Object to a byte array for
storage/serialization. |
static void |
serialize(java.io.Serializable obj,
java.io.OutputStream outputStream)
Serializes an
Object to the specified stream. |
static java.lang.String |
toString(java.lang.Object o) |
public static java.util.List<java.lang.Integer> createRange(int start,
int end)
start - end - public static java.lang.String join(java.lang.String delimiter,
java.lang.String wrap,
java.lang.Iterable<? extends java.lang.Object> objs)
public static java.lang.String join(java.lang.String delimiter,
java.lang.Iterable<? extends java.lang.Object> objs)
public static void closeQuietly(java.io.Closeable closeable)
public static boolean isInt(java.lang.String str)
public static boolean isNumeric(java.lang.String str)
public static boolean isEmpty(java.lang.CharSequence cs)
Checks if a CharSequence is empty ("") or null.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
cs - the CharSequence to check, may be nulltrue if the CharSequence is empty or nullpublic static int countMatches(java.lang.CharSequence str,
java.lang.CharSequence sub)
Counts how many times the substring appears in the larger string.
A null or empty ("") String input returns 0.
StringUtils.countMatches(null, *) = 0
StringUtils.countMatches("", *) = 0
StringUtils.countMatches("abba", null) = 0
StringUtils.countMatches("abba", "") = 0
StringUtils.countMatches("abba", "a") = 2
StringUtils.countMatches("abba", "ab") = 1
StringUtils.countMatches("abba", "xxx") = 0
str - the CharSequence to check, may be nullsub - the substring to count, may be nullnullpublic static <T> T notNull(T object,
java.lang.String message,
java.lang.Object... values)
Validate that the specified argument is not null;
otherwise throwing an exception with the specified message.
Validate.notNull(myObject, "The object must not be null");
T - the object typeobject - the object to checkmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception messagenull for method chaining)java.lang.NullPointerException - if the object is nullpublic static void isTrue(boolean expression,
java.lang.String message)
Validate that the argument condition is true; otherwise
throwing an exception with the specified message. This method is useful when
validating according to an arbitrary boolean expression, such as validating a
primitive number or using your own custom validation expression.
Validate.isTrue(i > 0.0, "The value must be greater than zero: %d", i);
For performance reasons, the long value is passed as a separate parameter and appended to the exception message only in the case of an error.
expression - the boolean expression to checkmessage - java.lang.IllegalArgumentException - if expression is falsepublic static <T extends java.lang.CharSequence> T notEmpty(T chars,
java.lang.String message,
java.lang.Object... values)
Validate that the specified argument character sequence is
neither null nor a length of zero (no characters);
otherwise throwing an exception with the specified message.
Validate.notEmpty(myString, "The string must not be empty");
T - the character sequence typechars - the character sequence to check, validated not null by this methodmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendednull method for chaining)java.lang.NullPointerException - if the character sequence is nulljava.lang.IllegalArgumentException - if the character sequence is emptypublic static java.lang.String toString(java.lang.Object o)
public static void serialize(java.io.Serializable obj,
java.io.OutputStream outputStream)
Serializes an Object to the specified stream.
The stream will be closed once the object is written. This avoids the need for a finally clause, and maybe also exception handling, in the application code.
The stream passed in is not buffered internally within this method. This is the responsibility of your application if desired.
obj - the object to serialize to bytes, may be nulloutputStream - the stream to write to, must not be nulljava.lang.IllegalArgumentException - if outputStream is nulljava.lang.RuntimeException - (runtime) if the serialization failspublic static byte[] serialize(java.io.Serializable obj)
Serializes an Object to a byte array for
storage/serialization.
obj - the object to serialize to bytesjava.lang.RuntimeException - (runtime) if the serialization failspublic static java.lang.Object deserialize(java.io.InputStream inputStream)
Deserializes an Object from the specified stream.
The stream will be closed once the object is written. This avoids the need for a finally clause, and maybe also exception handling, in the application code.
The stream passed in is not buffered internally within this method. This is the responsibility of your application if desired.
inputStream - the serialized object input stream, must not be nulljava.lang.IllegalArgumentException - if inputStream is nulljava.lang.RuntimeException - (runtime) if the serialization failspublic static java.lang.Object deserialize(byte[] objectData)
Deserializes a single Object from an array of bytes.
objectData - the serialized object, must not be nulljava.lang.IllegalArgumentException - if objectData is nulljava.lang.RuntimeException - (runtime) if the serialization fails