Monday, August 25, 2008

Follow Up Post : Java Function

Last week I wrote a Java Function and I also shown this function in my blog. Now, my 2 teammates sat together to understand the function and I got 4 hours task to simplify the function with this comment: "Code was cute, if we were writing in C. Modify code to be easily read and altered." In response to this, I convinced them to provide all logic in comments, and this is the new function now!

public String execute() {

String returnType=null;
if (StringUtils.isBlank(_orderType)) {
List privileges = (List) _session.get(HuskyConstants.PRIVILEGE_LIST);
if (privileges != null ) {
int _privilegeType=0;
for (Iterator i = privileges.iterator(); i.hasNext();) {
LhpPrivilege thisPriv = (LhpPrivilege)i.next();
/* determine what order privileges a user has. if they only have one, forward them to that option. */

/*Code Logic: We will generate a N digit binary number where each digit is one of the privilege. so N is no of privileges we are checking
* At this point, we are checking 4 privileges, so we will generate 4 digit binary.
* */
switch ( thisPriv.getPrivilegeId().intValue()){
case 67 : _privilegeType+=4;break; // self address , second digit from left ( as we are adding 4 = 0100 )
case 65 : _privilegeType+=2;break; // field sales rep , third digit from left ( as we are adding 2 = 0010 )
case 2006 : _privilegeType+=8;break; //physicians , first digit from left ( as we are adding 8 = 1000 )
case 68 : _privilegeType+=1;break; // enter address , fourth digit from left ( as we are adding 1 = 0001 )
}
/*In this loop we are keep adding these numbers ( 1000, or 0100 or 0010 or 0001 )
* If there are more than one privileges assigned to any role, we will have final binary with multiple 1s
* e.g. if a role has privileges 65 and 68, meaning binary (magic!) number will be 0010+0001=0011
* */
}
//Yet we had _privilegeType as integer, will be converting it to a binary string now to get the magic number.
orderPrivilegeType = Integer.toBinaryString(_privilegeType);

//When we convert an int to binary, it might not return us 4 digits binary as it avoids preceeding 0s. Lets add those preceeding 0s.
int lStr = orderPrivilegeType.length();
for (int l=0; l<4-lstr; orderprivilegetype ="0"> 001_success, for order type 2 its 002_success and so on.
*
* Goal is to find which single bit is on, and what is its order type.
* e.g. self address is order type =2,
* highestOnebit function gives us value of (highest) a bit ( as we have only 1 bit on, thats the highest or lowest )
* for self address case, final binary string will be 0100 and highestOnebit will return us 4.
* To map it to order type 2, we need to take log base 2.
*
* Hence all mapping based on order type, if we add new order type = 5, we should have 2^5=32 meaning first digit in a 5 digit binary number.
* we need to add privilege like this for the new order type
* case PRIVILEGE_NUMBER : _privilegeType+=32;break; // new order type , first digit from left ( as we are adding 32 = 10000 )
*
* Note, we dont have direct finction for log base 2, so need to take log base e and divide by log base 2. ( log (a) b = log (e) b / log (e) a )
* */
returnType= "00"+(int)((Math.log(Integer.highestOneBit(_privilegeType))/Math.log(2)))+"_success";
else returnType = INPUT; // if this role has multiple privileges.
} return returnType;
} else {
return setType();
}
}