com.bristle.javalib.util
Class ObjUtil

java.lang.Object
  extended by com.bristle.javalib.util.ObjUtil

public class ObjUtil
extends Object

This class contains utility routines for manipulating Java Objects.

Usage:
   - Typical scenarios for using this class are:
       String str1 = ObjUtil.castToString(obj);
       String str1 = ObjUtil.getClassName(obj);
       String str1 = ObjUtil.getShortClassName(obj);
       String str1 = ObjUtil.getPackageName(obj);
       String str1 = ObjUtil.generateUniqueId(obj);

   - See the source code of the inner Tester class for more examples.
  
Assumptions:
Effects:
       - None.
Anticipated Changes:
Notes:
Implementation Notes:
Portability Issues:
Revision History:
   $Log$


Nested Class Summary
static class ObjUtil.Tester
          Each class contains a Tester inner class with a main() for easier unit testing.
 
Constructor Summary
ObjUtil()
           
 
Method Summary
static String castToString(Object obj)
          Return the specified object cast to a String, or null if no object was specified.
static boolean equalsOrBothNull(Object obj1, Object obj2)
          Return true if the specified objects are equal or both null; false otherwise.
static String generateUniqueId(Object obj)
          Generate a unique id based on an Object and the current date and time.
static String getClassName(Object obj)
          Return a string that is the class name of the specified object, or null if no object was specified.
static String getPackageName(Object obj)
          Return a string that is the package name of the specified object, with a trailing dot ("."), or the empty string if there is no package, or null if the specified object is null.
static String getShortClassName(Object obj)
          Return a string that is the short class name of the specified object (the name without the package prefixes), or null if no object was specified.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjUtil

public ObjUtil()
Method Detail

equalsOrBothNull

public static boolean equalsOrBothNull(Object obj1,
                                       Object obj2)
Return true if the specified objects are equal or both null; false otherwise.

Parameters:
obj1 - Object to compare.
obj2 - Object to compare.
Returns:
true if equal or both null; false otherwise.

castToString

public static String castToString(Object obj)
Return the specified object cast to a String, or null if no object was specified.
 This method avoids the following problems that occur at runtime:
 1. Can't cast Object to String, if it is something like an Integer:
          Object obj1 = new Integer(1);
          String dummy1 = (String)obj1;
 2. Can't call its toString() method if it is null:
          Object obj2 = null;
          String dummy2 = obj2.toString()
 Instead, for any Object, do:
          String dummy3 = ObjUtils.castToString(obj1);
          String dummy4 = ObjUtils.castToString(obj2);
 

Parameters:
obj - Object to cast to String.
Returns:
String version of the object.

getClassName

public static String getClassName(Object obj)
Return a string that is the class name of the specified object, or null if no object was specified.

Parameters:
obj - Object to get the class name of.
Returns:
Class name or null.

getShortClassName

public static String getShortClassName(Object obj)
Return a string that is the short class name of the specified object (the name without the package prefixes), or null if no object was specified.

Parameters:
obj - Object to get the short class name of
Returns:
Short class name or null.

getPackageName

public static String getPackageName(Object obj)
Return a string that is the package name of the specified object, with a trailing dot ("."), or the empty string if there is no package, or null if the specified object is null.

Parameters:
obj - Object to get the package name of.
Returns:
Package name with trailing dot, or "".

generateUniqueId

public static String generateUniqueId(Object obj)
Generate a unique id based on an Object and the current date and time. The generated format includes a unique number, and the current date and time as: unique_DD_MON_YYYY_HH_MM_SS

Parameters:
obj - Object used to generate a unique number (its hashcode).
Returns:
Unique id.