com.partnersoft.data
Class DataLib

java.lang.Object
  extended by com.partnersoft.data.DataLib

public class DataLib
extends java.lang.Object

Some handy functions for messing with data.

Author:
Paul Reavis Copyright 2003 Partner Software, Inc.

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.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
DataLib()
           
 
Method Summary
static void addToMapFromLists(java.util.List keys, java.util.List values, java.util.Map victim)
          Same as makeMapFromLists except you get to supply your own Map.
static void addToMapFromListsNoQuestions(java.util.List keys, java.util.List values, java.util.Map victim)
          Same as makeMapFromLists except you get to supply your own Map and it does it's best to cram the damn things together.
static boolean areEqual(java.lang.Object a, java.lang.Object b)
          Something that it seems like the language should handle...
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)
          Coerces to a boolean, no matter what.
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 coerceToInt(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 short coerceToShort(java.lang.Object value)
          Coerces to an shorteger, no matter what.
static java.lang.Object deriveObject(java.lang.Object value, java.lang.Class coerceTo)
          Deep, potentially fey magic.
static java.lang.Comparable[][] diff(java.util.Collection a, java.util.Collection b)
          Same as diff(Comparable[] a, Comparable[] b), but takes collections as input.
static java.lang.Comparable[][] diff(java.lang.Comparable[] a, java.lang.Comparable[] b)
          Fast differences generator.
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.util.Iterator iterate(java.lang.Object victim)
          Tries to enumerate the object using iterator() and other methods.
static java.util.HashMap makeMapFromLists(java.util.List keys, java.util.List values)
          Takes two lists of identical length.
static java.util.ArrayList makeValueListFromMap(java.util.Map source, java.util.Collection keyOrder)
          Turns a Map into an ArrayList containing just values, using the given ordered list of keys to determine the order of the values.
static double setPrecision(double victim, int places)
          Reduces the precision of a doubleing-point number by rounding it to a specified number of decimal places.
static float setPrecision(float victim, int places)
          Reduces the precision of a floating-point number by rounding it to a specified number of decimal places.
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

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

DataLib

public DataLib()
Method Detail

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.


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!


coerceToInt

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


coerceToShort

public static short coerceToShort(java.lang.Object value)
Coerces to an shorteger, no matter what.


coerceToBoolean

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


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.


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.


makeMapFromLists

public static java.util.HashMap makeMapFromLists(java.util.List keys,
                                                 java.util.List values)
Takes two lists of identical length. One has keys, one has values. Makes HashMap from them.


addToMapFromLists

public static void addToMapFromLists(java.util.List keys,
                                     java.util.List values,
                                     java.util.Map victim)
Same as makeMapFromLists except you get to supply your own Map. Joy!


addToMapFromListsNoQuestions

public static void addToMapFromListsNoQuestions(java.util.List keys,
                                                java.util.List values,
                                                java.util.Map victim)
Same as makeMapFromLists except you get to supply your own Map and it does it's best to cram the damn things together. Joy!


makeValueListFromMap

public static java.util.ArrayList makeValueListFromMap(java.util.Map source,
                                                       java.util.Collection keyOrder)
Turns a Map into an ArrayList containing just values, using the given ordered list of keys to determine the order of the values. Keys not in the keyOrder list are ignored and thus do not appear in the result.


areEqual

public static boolean areEqual(java.lang.Object a,
                               java.lang.Object b)
Something that it seems like the language should handle... equality testing using identity test, then .equals(), dealing with nulls properly.


diff

public static java.lang.Comparable[][] diff(java.lang.Comparable[] a,
                                            java.lang.Comparable[] b)
Fast differences generator. Input is two Object arrays, whose contents must all implement Comparable. Output is three arrays, containing, respectively, those objects in collection "a" only, those objects in "b" only, and those objects in both "a" and "b". Original order is not preserved, since the algorithm requires sorting. Does modify the inputs; this is actually called by the other diff so we want to keep it efficient and not copy arrays unnecessarily.


diff

public static java.lang.Comparable[][] diff(java.util.Collection a,
                                            java.util.Collection b)
Same as diff(Comparable[] a, Comparable[] b), but takes collections as input. The input collections are not modified.


setPrecision

public static double setPrecision(double victim,
                                  int places)
Reduces the precision of a doubleing-point number by rounding it to a specified number of decimal places.


setPrecision

public static float setPrecision(float victim,
                                 int places)
Reduces the precision of a floating-point number by rounding it to a specified number of decimal places.