org.jcon.data
Class DataUtilities

java.lang.Object
  extended by org.jcon.data.DataUtilities

public class DataUtilities
extends java.lang.Object

This is a utility class, with methods that do various handy data mangling..

Version:
1.0 9/9/96
Author:
Paul Reavis

Field Summary
static java.lang.Class booleanClass
           
static java.lang.Class byteClass
           
static java.lang.Class characterClass
           
static java.lang.Object defaultBoolean
           
static java.lang.Object defaultByte
           
static java.lang.Object defaultCharacter
           
static java.util.Comparator defaultComparator
           
static java.lang.Object defaultDouble
           
static java.lang.Object defaultFloat
           
static java.lang.Object defaultInteger
           
static java.lang.Object defaultLong
           
static java.lang.Object defaultShort
           
static java.lang.Object defaultString
           
static java.lang.Class doubleClass
           
static java.lang.Class[] emptyClassArray
           
static java.lang.Class floatClass
           
static java.lang.Class integerClass
           
static java.lang.Class longClass
           
static java.lang.Class numberClass
           
static java.lang.Class shortClass
           
static java.lang.Class stringClass
           
 
Constructor Summary
DataUtilities()
           
 
Method Summary
static java.lang.Object clone(java.lang.Object victim)
          Tries to clone the object via any public clone() method.
static java.lang.Object coerce(java.lang.Object value, java.lang.Class coerceTo)
          A hard coercion tool.
static java.lang.Number coerceNumber(java.lang.Number value, java.lang.Class coerceTo)
          Attempts to coerce a numeric type to another.
static boolean coerceToBoolean(java.lang.Object value)
           
static double coerceToDouble(java.lang.Object value)
          Coerces to a double, no matter what.
static float coerceToFloat(java.lang.Object value)
          Coerces to a float, no matter what.
static int coerceToInteger(java.lang.Object value)
          Coerces to an integer, no matter what.
static long coerceToLong(java.lang.Object value)
          Coerces to a long, no matter what.
static java.lang.Object defaultIfNull(java.lang.Object tryMe, java.lang.Object defaultValue)
          Totally boneheaded handy thing that gives you the value if its non-null and default if it is.
static java.lang.Object deriveObject(java.lang.Object value, java.lang.Class coerceTo)
          Deep, potentially fey magic.
static java.lang.Object getDefaultPrimitive(java.lang.Class primitive)
          Returns a default value for a primitive class.
static java.lang.Object instantiate(java.lang.Class classy)
          Tries to create an instance of the given class; throws an IllegalArgumentException if it can't.
static java.lang.Object instantiate(java.lang.String classname)
          Tries to create an instance of the named class; throws an IllegalArgumentException if it can't.
static java.lang.Object instantiateOrDie(java.lang.Class classy)
          Tries to create an instance of the given class; dies if it can't.
static java.lang.Object instantiateOrDie(java.lang.String classname)
          Tries to create an instance of the named class; dies if it can't.
static java.util.Iterator iterate(java.lang.Object victim)
          Tries to enumerate the object using iterator() and other methods.
static java.util.Iterator sortIterator(java.util.Iterator e)
          Sorts an iterator using the default comparator (AlphabeticComparator)
static java.util.Iterator sortIterator(java.util.Iterator e, java.util.Comparator comparer)
           
static java.lang.String[] split(java.lang.String victim)
          Breaks a String into a String[] using whitespace (spaces, tabs, returns, etc.).
static java.lang.String[] split(java.lang.String s, char delim)
          Splits the given string using the given delimeter character.
static java.lang.String[] split(java.lang.String victim, java.lang.String delimiters)
          Breaks a String into a String[] based on a delimiter set.
static java.lang.Class wrapperFor(java.lang.Class primitiveClass)
          Returns the wrapper class for the given primitive class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultComparator

public static final java.util.Comparator defaultComparator

defaultCharacter

public static final java.lang.Object defaultCharacter

defaultBoolean

public static final java.lang.Object defaultBoolean

defaultByte

public static final java.lang.Object defaultByte

defaultShort

public static final java.lang.Object defaultShort

defaultInteger

public static final java.lang.Object defaultInteger

defaultLong

public static final java.lang.Object defaultLong

defaultFloat

public static final java.lang.Object defaultFloat

defaultDouble

public static final java.lang.Object defaultDouble

defaultString

public static final java.lang.Object defaultString

characterClass

public static final java.lang.Class characterClass

booleanClass

public static final java.lang.Class booleanClass

integerClass

public static final java.lang.Class integerClass

longClass

public static final java.lang.Class longClass

doubleClass

public static final java.lang.Class doubleClass

floatClass

public static final java.lang.Class floatClass

byteClass

public static final java.lang.Class byteClass

shortClass

public static final java.lang.Class shortClass

numberClass

public static final java.lang.Class numberClass

stringClass

public static final java.lang.Class stringClass

emptyClassArray

public static final java.lang.Class[] emptyClassArray
Constructor Detail

DataUtilities

public DataUtilities()
Method Detail

wrapperFor

public static java.lang.Class wrapperFor(java.lang.Class primitiveClass)
Returns the wrapper class for the given primitive class.


coerce

public static java.lang.Object coerce(java.lang.Object value,
                                      java.lang.Class coerceTo)
A hard coercion tool. Does its best to force a value of one class into a value of another. Makes some assumptions that could be dangerous, but allows the kind of loose typing that is often necessary when moving data back and forth between text files, databases, and java classes.

If the value is an instance of the coerceTo class, simply returns the value.

If the coerceTo class is a primitive, will return an object of the corresponding wrapper class. If the value given is null, then it is turned into the default primitive (0 for numbers, false for booleans). All kinds of unsafe coercion will also happen to shoehorn one numeric type into another.

Most magically, IF value is not an instance of the coerceTo class, BUT an instance of the coerceTo class can be created using a constructor that takes the value's class as its sole parameter, THEN it returns an object created using that constructor and using the given value. Neato! But hold onto your butt!

Returns null if the value or class given are null. Throws an IllegalArgumentException if the coercion isn't possible.


getDefaultPrimitive

public static java.lang.Object getDefaultPrimitive(java.lang.Class primitive)
Returns a default value for a primitive class. The value is zero for all numerics, the null character for char, and false for booleans. The returned value will be of the corresponding wrapper class for the requested primitive.


coerceNumber

public static java.lang.Number coerceNumber(java.lang.Number value,
                                            java.lang.Class coerceTo)
Attempts to coerce a numeric type to another. Can result in data loss, so be careful!


coerceToInteger

public static int coerceToInteger(java.lang.Object value)
Coerces to an integer, no matter what.


coerceToBoolean

public static boolean coerceToBoolean(java.lang.Object value)

coerceToLong

public static long coerceToLong(java.lang.Object value)
Coerces to a long, no matter what.


coerceToFloat

public static float coerceToFloat(java.lang.Object value)
Coerces to a float, no matter what.


coerceToDouble

public static double coerceToDouble(java.lang.Object value)
Coerces to a double, no matter what.


deriveObject

public static java.lang.Object deriveObject(java.lang.Object value,
                                            java.lang.Class coerceTo)
Deep, potentially fey magic. Tries to create an object of the coerceTo class using a constructor whose sole argument is the provided value. If anything goes wrong, throws an IllegalArgumentException.


defaultIfNull

public static java.lang.Object defaultIfNull(java.lang.Object tryMe,
                                             java.lang.Object defaultValue)
Totally boneheaded handy thing that gives you the value if its non-null and default if it is.


split

public static java.lang.String[] split(java.lang.String victim,
                                       java.lang.String delimiters)
Breaks a String into a String[] based on a delimiter set.

Inspired by the perl split() function.


split

public static java.lang.String[] split(java.lang.String s,
                                       char delim)
Splits the given string using the given delimeter character.


split

public static java.lang.String[] split(java.lang.String victim)
Breaks a String into a String[] using whitespace (spaces, tabs, returns, etc.).


instantiate

public static java.lang.Object instantiate(java.lang.Class classy)
Tries to create an instance of the given class; throws an IllegalArgumentException if it can't. Convenient.


instantiate

public static java.lang.Object instantiate(java.lang.String classname)
Tries to create an instance of the named class; throws an IllegalArgumentException if it can't. Convenient.


instantiateOrDie

public static java.lang.Object instantiateOrDie(java.lang.Class classy)
Tries to create an instance of the given class; dies if it can't. Convenient, yet deadly.


instantiateOrDie

public static java.lang.Object instantiateOrDie(java.lang.String classname)
Tries to create an instance of the named class; dies if it can't. Convenient, yet deadly.


iterate

public static java.util.Iterator iterate(java.lang.Object victim)
Tries to enumerate the object using iterator() and other methods. If the object does not support this method, or there are other problems, throws an IllegalArgumentException.


clone

public static java.lang.Object clone(java.lang.Object victim)
Tries to clone the object via any public clone() method.


sortIterator

public static java.util.Iterator sortIterator(java.util.Iterator e)
Sorts an iterator using the default comparator (AlphabeticComparator)


sortIterator

public static java.util.Iterator sortIterator(java.util.Iterator e,
                                              java.util.Comparator comparer)