I have a slightly theoretical question that I want to clear up before I start programing my solution any further...
Background. I need to perform comparisons between 2 (or possible more) MS Access files.
Each file should contain data that is found in one of the others.
Due to the 'limitation' I have encountered with JDBC and connecting to Access result sets (they are only forward scrollable!) I have created 'java objects' (classes) that models the structure of a result set.
in essence I have an object that models a single record line in the result set (lets call it a rowSet) A resSet object then has an 'array' of rowSets.
However to 'speed things up' I catch the values in the key and index columns and create a hashMap of 'key_Index' this to the relevant rowSet object.
Me comparison then does the following.
take the first resSet object (to use as the master), from this collect the individual the Key_Index hashmaps (lets call it 'aKey'....
Now use this 'aKey' object to search into the other avalailable resSet objects and see if any contain a value of a key_Index that matches the values in 'aKey'.
However I have just had a rather annoying thought.
If I use the code resSet.get(aKey) on one of my other resSet objects will I have problems as the 'aKey' object is obviously not the same object - although it's contents should be the same (ie comparable).
I have to say that as I read my question I think it is not well worded... so I think I will include a copy of the class that I have created...
The important parts:
members: challenge - an arrayList of 'result set' type objects.
method: the runChallenge()
package KWhite;
/**
* the RScomparator is designed for the instance of comparing a double entry database system, as
* often occurs in localy run medical trials data.
*
* The double entry is to ensure that there are no errors made during input, the entry is performed
* in separate independant instances. The RScolmparator object is specifically able to take any
* number of result sets as its input (ie queries from multiple independantly created databases)
*
* It should be recognised that this object should probably be called as part of a DBcomparator
* object.
*
*/
//imports here
//import of logger class and required dependencies...
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.log4j.PropertyConfigurator;
import MrBlue.DB_Table;
import TawuaiLogger.tawuaiLogger;
public class RScomparator {
//Static variables
//the logger instance
private static final TawuaiLogger.tawuaiLogger errLog = new tawuaiLogger("RScomparator");
//class member variables
/** this is a selection of ResSet objects that are going to have thier data challenged */
private ArrayList<ResSet_KW> challenge;
/**the name of the current table being challenged*/
private String tableName;
/**a 'table' object for meta data reference purposes, this can be used for getting column types etc */
private DB_Table table;
//These are our report objects
/**a report array for challenge failures */
private ArrayList<report_KW> fail;
/**a report array for errors, ie no challenger able to be made, no equivalent value found */
private ArrayList<report_KW> errors;
/**a report array for the good values */
private ArrayList<report_KW> success;
/** this is either the main class or the constructor
*
* If it is a constructor rename to reflect the name of the class
* @param args
*/
public RScomparator(DB_Table t) //TODO add arguments as required
{
PropertyConfigurator.configure("Log4j.properties");
// TODO Auto-generated method stub
challenge = new ArrayList<ResSet_KW>();
//initialise our report objects for this challenge scenario
fail = new ArrayList<report_KW>();
errors = new ArrayList<report_KW>();
success = new ArrayList<report_KW>();
table = t;
tableName = t.getTblName();
}
//class methods go here
/**
* add a result set object into this comparator
*
* @param r the result set object being inserted into this challenge.
*
*/
public void addChallenger(ResSet_KW r)
{
this.challenge.add(r);
}
/**
* this runs the comparison process...
* Although no details in of itself are returned it calls other methods that do return a value
*
*/
public void runChallenge()
{
//TODO finish this method, creating a report object on the way
//these are the 2 result set objects that will be compared
ResSet_KW gold = new ResSet_KW ();
ResSet_KW silver = new ResSet_KW ();
//ensure the challenger list has objects in it.
if(challenge.size() < 2)
{
//it must have 2 objects..
errLog.add(3, "there are no results available for comparison of table " + this.tableName);
//either way we should create report object.
this.errors.add( new report_KW(tableName));
//break out of the method.
return;
}
//get the first row of data
gold = challenge.get(0);//the first result set.
//for each column in the result set, perform a search for the same key in the others..
for(HashMap<String, String> c : gold.getRS().keySet())
{//c is the key value in the map
//cycle over the challenge object
for (int i=1; i<challenge.size(); i++)//we don't want to use the first element, so start from 1 not zero
{
silver = challenge.get(i);
if (silver.hasKey(c))
{
//a temp object for meta data referencing
//only get the actual result values if there is a match
Column_KW a = gold.getRS().get(c);
Column_KW b = silver.getRS().get(c);
//make the comparison
a.compareTo(b, this.table);
//get the reports from the comparison
for(report_KW k :a.getFailure())
{
this.fail.add(k);
}
for(report_KW k :a.getPassed())
{
this.success.add(k);
}
for(report_KW k :a.getPassed())
{
this.errors.add(k);
}
}
else
{
break;//return to the next item in the for loop
}
}
}
}
/**
*a helper method to create the error message creator
*@ param m the extra message if any,
*@return s the full message
*/
private String getErrMessage(String m) {
//the third element in the current stact trace should be the calling method
StackTraceElement caller = Thread.currentThread().getStackTrace()[3];
String s = m + "\ncalled from line " + caller.getLineNumber()
+ "\nmethod: " + this.getClass() + "." + caller.getMethodName();
return s;
}
}//end class
ps. any coments on my code or more that welcome
Thanks in advance
David
edit: I just found this question, Nested Maps or combined keys in java is this going to be my solution, create a'custom' key_Index object and then define a hashcode() and equals object for it. However I am allready using a 'hashMap' for my key_index object, so does this perform this automatically somewhere that I haven' noticed?
You said :
You have to implement
equals()
andhashcode()
respectively. It is not enough to implementequals()
,hashcode()
must be the same if the objects are equal.Example: