Tuesday, June 23, 2009

sql to_date() to_char() removal for java

Oracle to_date()
select db_fieldname from db_table where db_timestamp_field = to_date(?, 'dd-mm-yyyy');

replace the to_date(?, 'dd-mm-yyyy') function with a ?

select db_fieldname from db_table where db_timestamp_field = ?;

then using

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Timestamp result = new Timestamp(sdf.parse(inputdate,new ParsePosition(0)).getTime());

prepareStatement.setTimeStamp(1,result);


Oracle to_char()

select * from table where to_char(db_timestamp_field, 'dd-mm-yyyy') = ?;

replace to_char(db_timestamp_field,'dd-mm-yyyy') with trunc(db_timestamp_field)

select * from table where trunc(db_timestamp_field) = ?;

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date javaDate = sdf.parse(inputdate);
java.sql.Date sqlDate = new java.sql.Date(javaDate.getTime());

prepareStatement.setDate(1,sqlDate);

Tuesday, June 9, 2009

accumulator

I would need to scan a directory, take note of each of the file size, and do a count on how many files there are with a particular size.

file size is long
count is int

so i have one array to keep track of file size
another array to keep the int count

so once i have a file size, i would check through the array of file size, to see if it is the same, if yes, i remember the index and take the value from the count array plus one and put back in. The data structure which i used initially was 2 arraylist.

for sizeArray
  if sizeArray[i] is filesize
    take countArray[i]+1
    addedFlag to false

if addedFlag
  sizeArray[max] is filesize
  countArray[max] is 1

it looks awfully complicated, so i changed the 2 array into a hashtable
the long value will be converted to a string object.

if hashtable has fileSizeString
  currentCount is hashtable get fileSize
  hashtable put fileSizeString, currentCount+1

hence making the code a lot more shorter.

Conditional Complexity

see Conditional Complexity

public function validateObject(object:Object):void {
if (conditionA || conditionB) {
throw new ObjectValidationError(object);
}
if (conditionC || conditionD) {
throw new ObjectValidationError(object);
}
if (conditionE || conditionF) {
throw new ObjectValidationError(object);
}
// and many more if-statements
}


public function validateObject(object:Object):void {
if (conditionA || conditionB) {
invalid(object);
}
if (conditionC || conditionD) {
invalid(object);
}
if (conditionE || conditionF) {
invalid(object);
}
}

protected function invalid(object:Object):void {
throw new ObjectValidationError(object);
}


public function validateObject(object:Object):void {
validate(conditionA && conditionB, object)
validate(conditionC && conditionD, object)
validate(conditionE && conditionF, object)
}

protected function validate(isValid:Boolean, object:Object):void {
if (!isValid) {
throw new ObjectValidationError(object);
}
}

Tuesday, June 2, 2009

Vector to HashMap pattern

motivation: i recently discovered a bug via vector which resulted in the validation incorrectly checked.

Some Action

Vector v = new Vector();

v.add(request.getParameter("name"));
v.add(request.getParameter("address"));
v.add(request.getParameter("telephone"));

Some Manager

v.get(0);
v.get(1);
v.get(2);

can be replaced with

HashMap hm = new HashMap();

hm.add("name",request.getParameter("name"));
hm.add("address",request.getParameter("address"));
hm.add("telephone", request.getParameter("telephone"));

later on

hm.get("name");
hm.get("address");
hm.get("telephone");

this is much less error prone then refering via vector indexes. you can add new parameters without affecting the orders of the parameters also. String should be extracted out into a constants class to obey the principles of DRY.