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();
}
}
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Monday, August 25, 2008
@Office...
સવારે ઑફિસ પહોંચતા થોડું મોડું થયુ, નવના બદલે સવા નવ થઈ ગયા. પણ એની કોઇ મને ચિંતા ન હતી. આગ્રહ ખરો કે નવ વાગ્યે ડેસ્ક પર પહોંચી જવું. જો કે ઑફિસમાં આઈ.ટી. વિભાગમાં કોઈ નક્કી કરેલા સમયનું બંધન નથી, પણ દરેક એન્જિનિયર પોતાના નક્કી કરેલા સમયે આવી જાય છે. એમાં જો કોઈ મોટો ફેરફાર હોય તો એક ઈ-મેઈલ ટીમના મેઈલ આઈ-ડી પર નાખી દેવાનો રહે છે કે આજે હુ બાર વાગ્યે ઑફિસ પહોંચીશ અથવા તો આજે હું ઘેરથી કામ કરીશ અને 12 થી 2 ઉપ્લબ્ધ નહિ હોવ ત્યારે મારા મોબાઈલ પર મળી શકીશ. ટોપીક ચેઈન્જ હો ગયા... કમિંગ બેક ટુ ધ ટોપિક નાઉ.
અમારા કલાઈન્ટની (આઈ મીન ગ્રાહકની, યુ સી ગુજરાતી પણ આવડે છે!) વેબસાઈટ પર એક પ્રોબ્લેમ રહી ગયેલ છે, અને એમણે આજે સવારે અમને JIRAમાં એક બગ (bug ઉર્ફે ખામી) ફાયલ(file) કરેલ છે, અને આ બગ ઉર્ફે પ્રશ્ન ઉકેલવાનું મને સોંપવામાં આવ્યુ છે ( સવારે સાડા નવ વાગ્યે). JIRAની (પ્રોમિસ, jira વિશે ના જાણતા હો તો કયારેક જણાવીશ.. )ટિકિટ મને મારા ઇમેઈલમાં મોકલવામાં આવી છે. કોઇ લીડર કે મેનેજરને મળવાની જરુર નહીં. ટિકિટ JIRAમાં કોઈને પણ આપવામાં આવે એટલે, એ કામની સંપૂર્ણ જવાબદારી એમની. જો કશુ અસ્પષ્ટ હોય તો યોગ્ય વિભાગની યોગ્ય વ્યકિતને મળી (કેવી રીતે કોઈને મળવું, એ પછી કયારેક ) જાતે જ શોધી લેવાનું રહે. લીડર કે મેનેજરની મદદ જોઈએ ત્યારે મળી રહે, પણ એમનો એવો કોઈ આગ્રહ નહીં કે એમને પૂછી પૂછીને બધુ કરીયે , ના તો એમને કોઈ એવી અપેક્ષા કે રોજ સવારે એમના સભ્યો ( આઈ મીન મેમ્બર્સ!) એમને સલામ મારે. હૂં મારા મનેજરને અઠવાડિયે એકાદ વાર જોતો કે મળતો હોવ છું. કયારેક તો મહિને પણ્.... ના તો એ (Karina Brodsky) કયારેય કામ વગર મારી જોડે આવે. કામ હોય તો કામની priority પ્રમાણે, ઈમેઈલ કે ફોન કરે. જો ઇન્-પરસન (વૅલ, રૂબરૂ ! )મળવું જરુરી હોય તો મારા ડેસ્ક પર આવે અને વાત કરી જાય. કોઈ ઓર્ડર (હમ્મ્મ્મ્મ્મ આદેશ, હુકમ, અને ઘણા કિસ્સાઓમાં જોહુકમી પણ ... ) ના થાય કે દસ મિનિટમાં મારી કેબિનમાં આવી જા, વગેરે.. અગેઈન લેટસ કમ બૅક ટુ મેઈન ટોપિક..
મને જે ટિકિટ મળી છે, એમાં લખ્યુ છે કે એક ચોક્કસ સુવિધા ચાલતી નથી. મારે એ ચેક કરી એનાલાયજ (વિશ્લેષણ) કરવાનું છે, અને ઉકેલ મેળવવા પ્રયત્ન કરવો! વારુ, આનો મતલબ કે મારે એનો ઉકેલ મેળવવાનો છે અને આ માટે મને ૪ કલાક લાગશે, એવો અંદાજ પણ મુકેલ છે! હું એનાલિસિસ ચાલુ કરુ છું. અને મને એક વિઘ્નકારી કોડની લાઈન (line of code) મળી આવી, જેની મને શોધ હતી. આ લાયન છે, dir.mkdir(); આ જાવા કોડ (code) છે. જે કોમ્પ્યુટરમાં નવી ડિરેક્ટરી બનાવવાનું કામ કરે છે, પણ એના માટે પિત્રુ ડિરેક્ટરીઓ હાજર હોવી જરુરી છે. e.g. C:\Huskey\0\ ડિરેક્ટરી બનાવવા માટે, Huskey ડિરેક્ટરી હોવી આવશ્યક છે, નહીં તો આ ફંકશન "૦" નામની ડિરેક્ટરી નહીં બનાવે. મેં ઇન્ટરનેટના ઉપયોગથી શોધી કાઢ્યુ કે Huskey અને ૦ બન્ને ડિરેક્ટરીઓ બનાવવી હોય તો એક બીજુ ફંકશન છે mkdirs(). એટલે મેં dir.mkdir() ને બદલીને dir.mkdirs() કરી દીધુ, અને પેલી બગ ગાયબ ! મારૂ કામ પત્યુ. મને પાક્કો દોઢ કલાક થયો આ એક "s" શોધીને ઉમેરતા! SVN પર મારા change મુકી દીધા ને આ લખવા બેઠો !!
વિચાર આવ્યો કે આ એક "s" લખવાના ચાર કલાક લેખે રુપિયામાં ગણો તો, લગભગ પાંચ આંકડામાં મારી કંપનીને ખર્ચ થયો! અને જો હું ગુજરાતી ભાષાનો લેખક હોત તો ? મહિને દા'ડે માંડ આટલા મળત અને એ પણ "s" ને બદલે આખ્ખી "સોપડી" લખત ત્યારે !
અમારા કલાઈન્ટની (આઈ મીન ગ્રાહકની, યુ સી ગુજરાતી પણ આવડે છે!) વેબસાઈટ પર એક પ્રોબ્લેમ રહી ગયેલ છે, અને એમણે આજે સવારે અમને JIRAમાં એક બગ (bug ઉર્ફે ખામી) ફાયલ(file) કરેલ છે, અને આ બગ ઉર્ફે પ્રશ્ન ઉકેલવાનું મને સોંપવામાં આવ્યુ છે ( સવારે સાડા નવ વાગ્યે). JIRAની (પ્રોમિસ, jira વિશે ના જાણતા હો તો કયારેક જણાવીશ.. )ટિકિટ મને મારા ઇમેઈલમાં મોકલવામાં આવી છે. કોઇ લીડર કે મેનેજરને મળવાની જરુર નહીં. ટિકિટ JIRAમાં કોઈને પણ આપવામાં આવે એટલે, એ કામની સંપૂર્ણ જવાબદારી એમની. જો કશુ અસ્પષ્ટ હોય તો યોગ્ય વિભાગની યોગ્ય વ્યકિતને મળી (કેવી રીતે કોઈને મળવું, એ પછી કયારેક ) જાતે જ શોધી લેવાનું રહે. લીડર કે મેનેજરની મદદ જોઈએ ત્યારે મળી રહે, પણ એમનો એવો કોઈ આગ્રહ નહીં કે એમને પૂછી પૂછીને બધુ કરીયે , ના તો એમને કોઈ એવી અપેક્ષા કે રોજ સવારે એમના સભ્યો ( આઈ મીન મેમ્બર્સ!) એમને સલામ મારે. હૂં મારા મનેજરને અઠવાડિયે એકાદ વાર જોતો કે મળતો હોવ છું. કયારેક તો મહિને પણ્.... ના તો એ (Karina Brodsky) કયારેય કામ વગર મારી જોડે આવે. કામ હોય તો કામની priority પ્રમાણે, ઈમેઈલ કે ફોન કરે. જો ઇન્-પરસન (વૅલ, રૂબરૂ ! )મળવું જરુરી હોય તો મારા ડેસ્ક પર આવે અને વાત કરી જાય. કોઈ ઓર્ડર (હમ્મ્મ્મ્મ્મ આદેશ, હુકમ, અને ઘણા કિસ્સાઓમાં જોહુકમી પણ ... ) ના થાય કે દસ મિનિટમાં મારી કેબિનમાં આવી જા, વગેરે.. અગેઈન લેટસ કમ બૅક ટુ મેઈન ટોપિક..
મને જે ટિકિટ મળી છે, એમાં લખ્યુ છે કે એક ચોક્કસ સુવિધા ચાલતી નથી. મારે એ ચેક કરી એનાલાયજ (વિશ્લેષણ) કરવાનું છે, અને ઉકેલ મેળવવા પ્રયત્ન કરવો! વારુ, આનો મતલબ કે મારે એનો ઉકેલ મેળવવાનો છે અને આ માટે મને ૪ કલાક લાગશે, એવો અંદાજ પણ મુકેલ છે! હું એનાલિસિસ ચાલુ કરુ છું. અને મને એક વિઘ્નકારી કોડની લાઈન (line of code) મળી આવી, જેની મને શોધ હતી. આ લાયન છે, dir.mkdir(); આ જાવા કોડ (code) છે. જે કોમ્પ્યુટરમાં નવી ડિરેક્ટરી બનાવવાનું કામ કરે છે, પણ એના માટે પિત્રુ ડિરેક્ટરીઓ હાજર હોવી જરુરી છે. e.g. C:\Huskey\0\ ડિરેક્ટરી બનાવવા માટે, Huskey ડિરેક્ટરી હોવી આવશ્યક છે, નહીં તો આ ફંકશન "૦" નામની ડિરેક્ટરી નહીં બનાવે. મેં ઇન્ટરનેટના ઉપયોગથી શોધી કાઢ્યુ કે Huskey અને ૦ બન્ને ડિરેક્ટરીઓ બનાવવી હોય તો એક બીજુ ફંકશન છે mkdirs(). એટલે મેં dir.mkdir() ને બદલીને dir.mkdirs() કરી દીધુ, અને પેલી બગ ગાયબ ! મારૂ કામ પત્યુ. મને પાક્કો દોઢ કલાક થયો આ એક "s" શોધીને ઉમેરતા! SVN પર મારા change મુકી દીધા ને આ લખવા બેઠો !!
વિચાર આવ્યો કે આ એક "s" લખવાના ચાર કલાક લેખે રુપિયામાં ગણો તો, લગભગ પાંચ આંકડામાં મારી કંપનીને ખર્ચ થયો! અને જો હું ગુજરાતી ભાષાનો લેખક હોત તો ? મહિને દા'ડે માંડ આટલા મળત અને એ પણ "s" ને બદલે આખ્ખી "સોપડી" લખત ત્યારે !
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)i.next();
/* 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();
}
}
Subscribe to:
Posts (Atom)