net.m2technologies.open_arm.utilities.generic_object_functions
Class GenericObjectFunctions

java.lang.Object
  extended bynet.m2technologies.open_arm.utilities.generic_object_functions.GenericObjectFunctions

public class GenericObjectFunctions
extends java.lang.Object

Copyright 2005 Mark Masterson

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Description:

Version:
0.009
Author:
Mark Masterson

Constructor Summary
GenericObjectFunctions()
           
 
Method Summary
 boolean equals(java.lang.Object objectA, java.lang.Object objectB)
          Will compare the two Object parameters, to determine if they are a) instances of the same Class, or type, and b) if yes, then if the values of their declared fields are all identical.
 java.util.Comparator getGenericComparator()
          Returns an implementation of the java.util.Comparator interface that uses the Reflection API to determine the sort order.
 int hashCode(java.lang.Object objectA)
          Will compute a hash code for the Object passed as a parameter, using the same basic technique as the equals() method of this class.
 java.lang.String toString(java.lang.Object object)
          Returns a String containing the name of the Class of the Object paramter, the name of the super class (if any), and the names and values of all declared fields found in the Object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericObjectFunctions

public GenericObjectFunctions()
Method Detail

toString

public java.lang.String toString(java.lang.Object object)
Returns a String containing the name of the Class of the Object paramter, the name of the super class (if any), and the names and values of all declared fields found in the Object. If passed a null as parameter, returns an empty String.

If a SecurityException is caught, the method will simply print the stack trace to System.out and abort. If there is a problem determining the value of any field found, the method will print "???" as the value. The "|" is used as a delimiter. A "||" delimits the jump from a class to a superclass.
Sample usage:
public String toString() {
return GenericObjectFunctions.toString(this);
}


equals

public boolean equals(java.lang.Object objectA,
                      java.lang.Object objectB)
Will compare the two Object parameters, to determine if they are a) instances of the same Class, or type, and b) if yes, then if the values of their declared fields are all identical. If both conditions are met, the method will return "true", otherwise "false". If a SecurityException is caught, the method will simply print the stack trace to System.out and abort.
Sample usage:
public boolean equals(Object objectA) {
return GenericObjectFunctions.equals(objectA, this);
}


hashCode

public int hashCode(java.lang.Object objectA)
Will compute a hash code for the Object passed as a parameter, using the same basic technique as the equals() method of this class. A hashCode() method and an equals() method for a given Object must be compatible; that is, if two objects are equal, they must also return identical hash codes. For that reason, the same usage of the Reflection API that is utilized in this class's equals() method is used here to calculate a hash code. This may be overkill for many cases -- you should carefully weigh the benefits of using this method with those of rolling your own... If a SecurityException is caught, the method will simply print the stack trace to System.out and abort. If there is a problem determining the value of any field found, the method will use "???" as the value.
Sample usage:
public int hashCode() {
return GenericObjectFunctions.hashCode(this);
}


getGenericComparator

public java.util.Comparator getGenericComparator()
Returns an implementation of the java.util.Comparator interface that uses the Reflection API to determine the sort order. Specifically, the Comparator thus obtained will use this class's hashCode() method to determine the sort order: if the hash code of one object is greater than the other, the one with the greater hash code will come after the other object in the sort order. By the same token, the object with the lesser hash code will come before the other object in the sort order.
The Comparator object that one obtains from this method must then be passed as an argument to the constructor of one of the sorted Collections. The instance of that Collection thus obtained will use this Comparator to sort the objects -- an example of the Decorator design pattern.
For example, to use this method with a TreeSet Collection, one would write the following:
java.util.TreeSet baum = new java.util.TreeSet(GenericObjectFunctions.getGenericComparator());
baum.add(a2);
baum.add(a1);
Assuming that a1 is intended to be before a2 in the sort order, and assuming that the Reflection API generated hash-code actually produced such a sort order, then the objects should be found in the TreeSet so sorted. Generally, one would use this technique rather than writing a compareTo() method in each of your classes.
NOTE: This is not a very effecient, or even a very reliable method of sorting your objects. It IS very "generic", hence the method name. However, like all very generic things, it is also very inefficient, and slow. Use this method with caution, therefore. In many cases, it will be worthwhile to consider rolling your own compareTo(Object objectA) method in any class that you intend to store in a sorted Collection.