org.apache.wicket.util.lang
Class Objects

java.lang.Object
  extended by org.apache.wicket.util.lang.Objects

public final class Objects
extends java.lang.Object

Object utilities.

Author:
Jonathan Locke

Nested Class Summary
static interface Objects.IObjectSizeOfStrategy
          Interface that enables users to plugin the way object sizes are calculated with Wicket.
static class Objects.SerializingObjectSizeOfStrategy
          Objects.IObjectSizeOfStrategy that works by serializing the object to an instance of ByteCountingOutputStream, which records the number of bytes written to it.
 
Method Summary
static java.math.BigDecimal bigDecValue(java.lang.Object value)
          Evaluates the given object as a BigDecimal.
static java.math.BigInteger bigIntValue(java.lang.Object value)
          Evaluates the given object as a BigInteger.
static boolean booleanValue(java.lang.Object value)
          Evaluates the given object as a boolean: if it is a Boolean object, it's easy; if it's a Number or a Character, returns true for non-zero objects; and otherwise returns true for non-null objects.
static java.lang.Object byteArrayToObject(byte[] data)
          De-serializes an object from a byte array.
static java.lang.Object cloneModel(java.lang.Object object)
          Makes a deep clone of an object by serializing and deserializing it.
static java.lang.Object cloneObject(java.lang.Object object)
          Makes a deep clone of an object by serializing and deserializing it.
static int compareWithConversion(java.lang.Object v1, java.lang.Object v2)
          Compares two objects for equality, even if it has to convert one of them to the other type.
static java.lang.Object convertValue(java.lang.Object value, java.lang.Class toType)
          Returns the value converted numerically to the given class type This method also detects when arrays are being converted and converts the components of one array to the type of the other.
static double doubleValue(java.lang.Object value)
          Evaluates the given object as a double-precision floating-point number.
static boolean equal(java.lang.Object a, java.lang.Object b)
          Returns true if a and b are equal.
static int getNumericType(int t1, int t2, boolean canBeNonNumeric)
          Returns the constant from the NumericTypes interface that best expresses the type of an operation, which can be either numeric or not, on the two given types.
static int getNumericType(java.lang.Object value)
          Returns a constant from the NumericTypes interface that represents the numeric type of the given object.
static int getNumericType(java.lang.Object v1, java.lang.Object v2)
          Returns the constant from the NumericTypes interface that best expresses the type of a numeric operation on the two given objects.
static int getNumericType(java.lang.Object v1, java.lang.Object v2, boolean canBeNonNumeric)
          Returns the constant from the NumericTypes interface that best expresses the type of an operation, which can be either numeric or not, on the two given objects.
static int hashCode(java.lang.Object[] obj)
          returns hashcode of the objects by calling obj.hashcode().
static boolean isEqual(java.lang.Object object1, java.lang.Object object2)
          Returns true if object1 is equal to object2 in either the sense that they are the same object or, if both are non-null if they are equal in the equals() sense.
static long longValue(java.lang.Object value)
          Evaluates the given object as a long integer.
static java.lang.Object newInstance(java.lang.String className)
          Creates a new instance using the current application's class resolver.
static java.lang.Number newInteger(int type, long value)
          Returns a new Number object of an appropriate type to hold the given integer value.
static byte[] objectToByteArray(java.lang.Object object)
          Serializes an object into a byte array.
static void setObjectSizeOfStrategy(Objects.IObjectSizeOfStrategy objectSizeOfStrategy)
          Sets the strategy for determining the sizes of objects.
static void setObjectStreamFactory(IObjectStreamFactory objectStreamFactory)
          Configure this utility class to use the provided IObjectStreamFactory instance.
static long sizeof(java.lang.Object object)
          Computes the size of an object.
static java.lang.String stringValue(java.lang.Object value)
          Evaluates the given object as a String.
static java.lang.String stringValue(java.lang.Object value, boolean trim)
          Evaluates the given object as a String and trims it if the trim flag is true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

bigDecValue

public static java.math.BigDecimal bigDecValue(java.lang.Object value)
                                        throws java.lang.NumberFormatException
Evaluates the given object as a BigDecimal.

Parameters:
value - an object to interpret as a BigDecimal
Returns:
the BigDecimal value implied by the given object
Throws:
java.lang.NumberFormatException - if the given object can't be understood as a BigDecimal

bigIntValue

public static java.math.BigInteger bigIntValue(java.lang.Object value)
                                        throws java.lang.NumberFormatException
Evaluates the given object as a BigInteger.

Parameters:
value - an object to interpret as a BigInteger
Returns:
the BigInteger value implied by the given object
Throws:
java.lang.NumberFormatException - if the given object can't be understood as a BigInteger

booleanValue

public static boolean booleanValue(java.lang.Object value)
Evaluates the given object as a boolean: if it is a Boolean object, it's easy; if it's a Number or a Character, returns true for non-zero objects; and otherwise returns true for non-null objects.

Parameters:
value - an object to interpret as a boolean
Returns:
the boolean value implied by the given object

byteArrayToObject

public static java.lang.Object byteArrayToObject(byte[] data)
De-serializes an object from a byte array.

Parameters:
data - The serialized object
Returns:
The object

cloneModel

public static java.lang.Object cloneModel(java.lang.Object object)
Makes a deep clone of an object by serializing and deserializing it. The object must be fully serializable to be cloned. This method will not clone wicket Components, it will just reuse those instances so that the complete component tree is not copied over only the model data.

Parameters:
object - The object to clone
Returns:
A deep copy of the object

cloneObject

public static java.lang.Object cloneObject(java.lang.Object object)
Makes a deep clone of an object by serializing and deserializing it. The object must be fully serializable to be cloned. No extra debug info is gathered.

Parameters:
object - The object to clone
Returns:
A deep copy of the object
See Also:
cloneModel(Object)

compareWithConversion

public static int compareWithConversion(java.lang.Object v1,
                                        java.lang.Object v2)
Compares two objects for equality, even if it has to convert one of them to the other type. If both objects are numeric they are converted to the widest type and compared. If one is non-numeric and one is numeric the non-numeric is converted to double and compared to the double numeric value. If both are non-numeric and Comparable and the types are compatible (i.e. v1 is of the same or superclass of v2's type) they are compared with Comparable.compareTo(). If both values are non-numeric and not Comparable or of incompatible classes this will throw and IllegalArgumentException.

Parameters:
v1 - First value to compare
v2 - second value to compare
Returns:
integer describing the comparison between the two objects. A negative number indicates that v1 < v2. Positive indicates that v1 > v2. Zero indicates v1 == v2.
Throws:
java.lang.IllegalArgumentException - if the objects are both non-numeric yet of incompatible types or do not implement Comparable.

convertValue

public static java.lang.Object convertValue(java.lang.Object value,
                                            java.lang.Class toType)
Returns the value converted numerically to the given class type This method also detects when arrays are being converted and converts the components of one array to the type of the other.

Parameters:
value - an object to be converted to the given type
toType - class type to be converted to
Returns:
converted value of the type given, or value if the value cannot be converted to the given type.

doubleValue

public static double doubleValue(java.lang.Object value)
                          throws java.lang.NumberFormatException
Evaluates the given object as a double-precision floating-point number.

Parameters:
value - an object to interpret as a double
Returns:
the double value implied by the given object
Throws:
java.lang.NumberFormatException - if the given object can't be understood as a double

equal

public static boolean equal(java.lang.Object a,
                            java.lang.Object b)
Returns true if a and b are equal. Either object may be null.

Parameters:
a - Object a
b - Object b
Returns:
True if the objects are equal

getNumericType

public static int getNumericType(int t1,
                                 int t2,
                                 boolean canBeNonNumeric)
Returns the constant from the NumericTypes interface that best expresses the type of an operation, which can be either numeric or not, on the two given types.

Parameters:
t1 - type of one argument to an operator
t2 - type of the other argument
canBeNonNumeric - whether the operator can be interpreted as non-numeric
Returns:
the appropriate constant from the NumericTypes interface

getNumericType

public static int getNumericType(java.lang.Object value)
Returns a constant from the NumericTypes interface that represents the numeric type of the given object.

Parameters:
value - an object that needs to be interpreted as a number
Returns:
the appropriate constant from the NumericTypes interface

getNumericType

public static int getNumericType(java.lang.Object v1,
                                 java.lang.Object v2)
Returns the constant from the NumericTypes interface that best expresses the type of a numeric operation on the two given objects.

Parameters:
v1 - one argument to a numeric operator
v2 - the other argument
Returns:
the appropriate constant from the NumericTypes interface

getNumericType

public static int getNumericType(java.lang.Object v1,
                                 java.lang.Object v2,
                                 boolean canBeNonNumeric)
Returns the constant from the NumericTypes interface that best expresses the type of an operation, which can be either numeric or not, on the two given objects.

Parameters:
v1 - one argument to an operator
v2 - the other argument
canBeNonNumeric - whether the operator can be interpreted as non-numeric
Returns:
the appropriate constant from the NumericTypes interface

isEqual

public static boolean isEqual(java.lang.Object object1,
                              java.lang.Object object2)
Returns true if object1 is equal to object2 in either the sense that they are the same object or, if both are non-null if they are equal in the equals() sense.

Parameters:
object1 - First object to compare
object2 - Second object to compare
Returns:
true if v1 == v2

longValue

public static long longValue(java.lang.Object value)
                      throws java.lang.NumberFormatException
Evaluates the given object as a long integer.

Parameters:
value - an object to interpret as a long integer
Returns:
the long integer value implied by the given object
Throws:
java.lang.NumberFormatException - if the given object can't be understood as a long integer

newInstance

public static java.lang.Object newInstance(java.lang.String className)
Creates a new instance using the current application's class resolver. Returns null if className is null.

Parameters:
className - The full class name
Returns:
The new object instance

newInteger

public static java.lang.Number newInteger(int type,
                                          long value)
Returns a new Number object of an appropriate type to hold the given integer value. The type of the returned object is consistent with the given type argument, which is a constant from the NumericTypes interface.

Parameters:
type - the nominal numeric type of the result, a constant from the NumericTypes interface
value - the integer value to convert to a Number object
Returns:
a Number object with the given value, of type implied by the type argument

objectToByteArray

public static byte[] objectToByteArray(java.lang.Object object)
Serializes an object into a byte array.

Parameters:
object - The object
Returns:
The serialized object

setObjectSizeOfStrategy

public static void setObjectSizeOfStrategy(Objects.IObjectSizeOfStrategy objectSizeOfStrategy)
Sets the strategy for determining the sizes of objects.

Parameters:
objectSizeOfStrategy - the strategy. Pass null to reset to the default.

setObjectStreamFactory

public static void setObjectStreamFactory(IObjectStreamFactory objectStreamFactory)
Configure this utility class to use the provided IObjectStreamFactory instance.

Parameters:
objectStreamFactory - The factory instance to use. If you pass in null, the default will be set (again). Pass null to reset to the default.

sizeof

public static long sizeof(java.lang.Object object)
Computes the size of an object. Note that this is an estimation, never an absolute accurate size.

Parameters:
object - Object to compute size of
Returns:
The size of the object in bytes

stringValue

public static java.lang.String stringValue(java.lang.Object value)
Evaluates the given object as a String.

Parameters:
value - an object to interpret as a String
Returns:
the String value implied by the given object as returned by the toString() method, or "null" if the object is null.

hashCode

public static int hashCode(java.lang.Object[] obj)
returns hashcode of the objects by calling obj.hashcode(). safe to use when obj is null.

Parameters:
obj -
Returns:
hashcode of the object or 0 if obj is null

stringValue

public static java.lang.String stringValue(java.lang.Object value,
                                           boolean trim)
Evaluates the given object as a String and trims it if the trim flag is true.

Parameters:
value - an object to interpret as a String
trim - whether to trim the string
Returns:
the String value implied by the given object as returned by the toString() method, or "null" if the object is null.


Copyright © 2004-2010 Apache Software Foundation. All Rights Reserved.