gov.sandia.cognition.util
Class ObjectUtil

java.lang.Object
  extended by gov.sandia.cognition.util.ObjectUtil

public class ObjectUtil
extends Object

The ObjectUtil class implements static utility methods for dealing with Objects.

Since:
2.0
Author:
Justin Basilico

Constructor Summary
ObjectUtil()
           
 
Method Summary
static
<T extends CloneableSerializable>
T
cloneSafe(T object)
          Calls the Clone method on the given object of some type that extends CloneableSerializable.
static
<T> T
cloneSmart(T object)
          Attempts to clone a given object.
static
<T> T[]
cloneSmartArrayAndElements(T[] array)
          Clones an array and its elements.
static
<T> ArrayList<T>
cloneSmartElementsAsArrayList(Collection<T> collection)
          Creates a new ArrayList and attempts to copy all of the elements from the given collection into it by calling the cloneSmart method on each of them.
static
<T> LinkedList<T>
cloneSmartElementsAsLinkedList(Iterable<T> collection)
          Creates a new LinkedList and attempts to copy all of the elements from the given collection into it by calling the cloneSmart method on each of them.
static
<T extends Serializable>
T
deepCopy(T object)
          Performs a deep copy of a given object.
static boolean equalsSafe(Object first, Object second)
          Determines if two objects are equals in a way that is safe for dealing with null.
static LinkedList<Field> getAllFields(Class<?> c)
          Returns a list of all of the fields on the given class and any super-class.
static ArrayList<DefaultPair<Field,Object>> getAllFieldValues(Object o)
          Returns an ArrayList of field/value pairs
static byte[] getBytes(Object o)
          Attempts to convert the given Object into a byte array
static ArrayList<Object> getFieldValues(Object o, Collection<Field> fields)
          Gets the values of the given fields for the object (can get public / protected / private field values)
static LinkedList<Method> getMethods(Class<?> c)
          Gets the Collection of all public/private/protected Methods in the given Class (except those only given in the Object class).
static String getShortClassName(Class<?> c)
          Gets a short version of the Class name, so that gov.sandia.cognition.util.ObjectUtil$InternalClass will be returned as "ObjectUtil$InternalClass", for example
static String getShortClassName(Object o)
          Gets a short version of the Class name, so that gov.sandia.cognition.util.ObjectUtil$InternalClass will be returned as "ObjectUtil$InternalClass", for example
static int hashCodeSafe(Object value)
          Determines the hash code of the given value by calling the hashCode method on the given object if it is not null.
static String inspectAPI(Class<?> c)
          Creates a String representing the Constructors and Methods from the given Class, inspects public constructors and all public/private/protected methods (except those from the Object class).
static String inspectFields(Class<?> c)
          Returns a String representing the values of all public/private/protected fields in the given Class, e.g., "protected double myField"
static String inspectFieldValues(Object o)
          Returns a String representing the values of all public/private/protected fields in the given instantiated Object, e.g., "double myField = 3.14"
static String inspector(Object o)
          Prints out the Methods and Fields associated with the argument.
static String toString(Object o)
          Creates a String that contains the names and values of the members, or the default Object toString if that fails.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectUtil

public ObjectUtil()
Method Detail

equalsSafe

public static boolean equalsSafe(Object first,
                                 Object second)
Determines if two objects are equals in a way that is safe for dealing with null. Null is only equal to itself.

Parameters:
first - The first object.
second - The second object.
Returns:
True if the two objects are equal and false otherwise.

hashCodeSafe

public static int hashCodeSafe(Object value)
Determines the hash code of the given value by calling the hashCode method on the given object if it is not null. If it is null, 0 is returned.

Parameters:
value - The value to get the hash code for. May be null.
Returns:
The hash code of the given value if it is not null; otherwise, 0.

cloneSafe

public static <T extends CloneableSerializable> T cloneSafe(T object)
Calls the Clone method on the given object of some type that extends CloneableSerializable. Note: It assumes that the given object's clone method will return the same object of type T.

Type Parameters:
T - The type of the object to clone.
Parameters:
object - The object to clone.
Returns:
The clone.

cloneSmart

public static <T> T cloneSmart(T object)
Attempts to clone a given object. If the passed object is null, then null is returned. Otherwise, it checks to see if the object implements the CloneableSerializable interface. If it does, it calls the cloneSafe method with it. If not, it attempts to see if there is a clone method on the given object, and if so it attempts to call the method. If this fails, then it just returns the given object. Note: It assumes that the given object's clone method will return the same object of type T.

Type Parameters:
T - The type of the object to clone.
Parameters:
object - The object to clone.
Returns:
A clone of the given object, if it possible to clone it. If it is not, then the original object is returned.

cloneSmartElementsAsArrayList

public static <T> ArrayList<T> cloneSmartElementsAsArrayList(Collection<T> collection)
Creates a new ArrayList and attempts to copy all of the elements from the given collection into it by calling the cloneSmart method on each of them.

Type Parameters:
T - The type of the elements in the list.
Parameters:
collection - The collection of elements to clone.
Returns:
A new ArrayList with a clone of each element in it.

cloneSmartElementsAsLinkedList

public static <T> LinkedList<T> cloneSmartElementsAsLinkedList(Iterable<T> collection)
Creates a new LinkedList and attempts to copy all of the elements from the given collection into it by calling the cloneSmart method on each of them.

Type Parameters:
T - The type of the elements in the list.
Parameters:
collection - The collection of elements to clone.
Returns:
A new LinkedList with a clone of each element in it.

cloneSmartArrayAndElements

public static <T> T[] cloneSmartArrayAndElements(T[] array)
Clones an array and its elements.

Type Parameters:
T - The type of object in the array.
Parameters:
array - The array to copy, along with its elements.
Returns:
A new smart copy of the array and its elements.

deepCopy

public static <T extends Serializable> T deepCopy(T object)
Performs a deep copy of a given object. Works by using serialization to write out an object and read it back in. It should only be used if you are really sure you want a deep copy of an object.

Type Parameters:
T - The type of the object to copy.
Parameters:
object - The object to copy.
Returns:
A deep copy of the given object or null, if the copy fails.

inspector

public static String inspector(Object o)
Prints out the Methods and Fields associated with the argument. If the argument is a type of "Class", then the referenced Class is accessed. If the argument is a String, then the String is examined to extract the name of the Class: e.g., "gov.sandia.cognition.util.DefaultPair". Otherwise, the argument is taken to be an instantiated Object, and we inspect the methods and fields of the object itself.

Parameters:
o - Object/Class/String to inspect from
Returns:
String representing a list of the Methods and Fields of the given argument. If a ClassNotFoundException gets thrown, this method intercepts the Exception and simply returns the Exception String as the return value (thus, no Exceptions are thrown).

inspectAPI

public static String inspectAPI(Class<?> c)
Creates a String representing the Constructors and Methods from the given Class, inspects public constructors and all public/private/protected methods (except those from the Object class).

Parameters:
c - Class to determine the API of
Returns:
String representing the Constructors and Methods

getMethods

public static LinkedList<Method> getMethods(Class<?> c)
Gets the Collection of all public/private/protected Methods in the given Class (except those only given in the Object class). If the argument represents an interface, then the interface methods are listed. Otherwise, only nonvolatile (i.e., not abstract) methods are returned.

Parameters:
c - Class from which to pull the Methods
Returns:
Collection of all public/private/protected Methods in the given Class

toString

public static String toString(Object o)
Creates a String that contains the names and values of the members, or the default Object toString if that fails.

Parameters:
o - Object to convert to a String
Returns:
String with member names and values of the members, or the default Object toString if that fails.

inspectFieldValues

public static String inspectFieldValues(Object o)
Returns a String representing the values of all public/private/protected fields in the given instantiated Object, e.g., "double myField = 3.14"

Parameters:
o - Instantiated Object to inspect
Returns:
String representing the values of all public/private/protected fields

inspectFields

public static String inspectFields(Class<?> c)
Returns a String representing the values of all public/private/protected fields in the given Class, e.g., "protected double myField"

Parameters:
c - Class from which to inspect the fields
Returns:
The String containing the name, type, and modifier of all fields.

getAllFields

public static LinkedList<Field> getAllFields(Class<?> c)
Returns a list of all of the fields on the given class and any super-class. It includes fields of all access modifiers.

Parameters:
c - The class to get all the fields from.
Returns:
The list of fields on the given class or any of its super classes.

getAllFieldValues

public static ArrayList<DefaultPair<Field,Object>> getAllFieldValues(Object o)
Returns an ArrayList of field/value pairs

Parameters:
o - Object to get the field/value pairs
Returns:
ArrayList of field/value pairs. The value component of the DefaultPair will be null if the field is protected by the security manager

getFieldValues

public static ArrayList<Object> getFieldValues(Object o,
                                               Collection<Field> fields)
Gets the values of the given fields for the object (can get public / protected / private field values)

Parameters:
o - Object to get the field values of
fields - Fields to get the values of
Returns:
ArrayList for the field values, null if not found of protected

getShortClassName

public static String getShortClassName(Object o)
Gets a short version of the Class name, so that gov.sandia.cognition.util.ObjectUtil$InternalClass will be returned as "ObjectUtil$InternalClass", for example

Parameters:
o - Object from which to pull the short Class name
Returns:
Short String representing the class name

getShortClassName

public static String getShortClassName(Class<?> c)
Gets a short version of the Class name, so that gov.sandia.cognition.util.ObjectUtil$InternalClass will be returned as "ObjectUtil$InternalClass", for example

Parameters:
c - Object from which to pull the short Class name
Returns:
Short String representing the class name

getBytes

public static byte[] getBytes(Object o)
Attempts to convert the given Object into a byte array

Parameters:
o - Object to convert
Returns:
bytes of the object, null if none