Friday, August 22, 2008

An interesting java function

Today I wrote a java function which I enjoyed while writing. It took 2 hours to write this small looking function. In fact I saw similar concept in Shashank Khare's code of Nextgen ( if remember the product name correctly). Though it was in C code. Handling multiple flags in single string type variable, each bit of it will decide flag value for different variable!

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);
/* determine what order privileges a user has. if they only have one, forward them to that option. */
switch ( thisPriv.getPrivilegeId().intValue()){
case 67 : _privilegeType+=4;break; // self address
case 65 : _privilegeType+=2;break; // field sales rep
case 2006 : _privilegeType+=8;break; //physicians
case 68 : _privilegeType+=1;break; // enter address
orderPrivilegeType = Integer.toBinaryString(_privilegeType);
//add preceeding 0s.
int lStr = orderPrivilegeType.length();
for (int l=0; l<4-lStr; l++)orderPrivilegeType ="0"+orderPrivilegeType;

//if user has only one type of order placing privilege
if ( Integer.bitCount(_privilegeType)==1)
returnType= "00"+(int)((Math.log(Integer.highestOneBit(_privilegeType))/Math.log(2)))+"_success";
else returnType = INPUT;
} return returnType;
} else {
return setType();

1 comment: said...

very useful code for my need...
thank you