public class Utils extends Object
Constructor and Description |
---|
Utils() |
Modifier and Type | Method and Description |
---|---|
static <T extends Serializable> |
clone(T object)
Deep clone an
Object using serialization. |
static void |
closeQuietly(Closeable closeable) |
static int |
countMatches(CharSequence str,
CharSequence sub)
Counts how many times the substring appears in the larger string.
|
static Object |
deserialize(byte[] objectData)
Deserializes a single
Object from an array of bytes. |
static Object |
deserialize(InputStream inputStream)
Deserializes an
Object from the specified stream. |
static boolean |
isEmpty(CharSequence cs)
Checks if a CharSequence is empty ("") or null.
|
static void |
isTrue(boolean expression,
String message)
Validate that the argument condition is
true ; otherwise
throwing an exception with the specified message. |
static <T extends CharSequence> |
notEmpty(T chars,
String message,
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,
String message,
Object... values)
Validate that the specified argument is not
null ;
otherwise throwing an exception with the specified message. |
static byte[] |
serialize(Serializable obj)
Serializes an
Object to a byte array for
storage/serialization. |
static void |
serialize(Serializable obj,
OutputStream outputStream)
Serializes an
Object to the specified stream. |
static Double |
toDouble(Object o)
converts to Double with radix 10
|
static Integer |
toInt(Object o)
converts to Integer with radix 10
|
static Long |
toLong(Object o)
converts to Long with radix 10
|
static String |
toString(Object o) |
public static void closeQuietly(Closeable closeable)
public static boolean isEmpty(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(CharSequence str, 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 nullnull
public static <T> T notNull(T object, String message, 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)NullPointerException
- if the object is null
public static void isTrue(boolean expression, 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
- IllegalArgumentException
- if expression is false
public static <T extends CharSequence> T notEmpty(T chars, String message, 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)NullPointerException
- if the character sequence is null
IllegalArgumentException
- if the character sequence is emptypublic static Integer toInt(Object o)
o
- object to convertpublic static Long toLong(Object o)
o
- object to convertpublic static Double toDouble(Object o)
o
- object to convertpublic static <T extends Serializable> T clone(T object)
Deep clone an Object
using serialization.
This is many times slower than writing clone methods by hand
on all objects in your object graph. However, for complex object
graphs, or for those that don't support deep cloning this can
be a simple alternative implementation. Of course all the objects
must be Serializable
.
T
- the type of the object involvedobject
- the Serializable
object to clonepublic static void serialize(Serializable obj, 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 nullIllegalArgumentException
- if outputStream
is null
RuntimeException
- (runtime) if the serialization failspublic static byte[] serialize(Serializable obj)
Serializes an Object
to a byte array for
storage/serialization.
obj
- the object to serialize to bytesRuntimeException
- (runtime) if the serialization failspublic static Object deserialize(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 nullIllegalArgumentException
- if inputStream
is null
RuntimeException
- (runtime) if the serialization failspublic static Object deserialize(byte[] objectData)
Deserializes a single Object
from an array of bytes.
objectData
- the serialized object, must not be nullIllegalArgumentException
- if objectData
is null
RuntimeException
- (runtime) if the serialization failsCopyright © 2011-2013. All Rights Reserved.