May 13, 2013
June 15, 2012
Why Software Engineers are Grumpy?
My friend Paul from East Coast, forwarded this article to me in the morning. While I was reading it, I became, happy, sad was laughing around loudly in the BART train to office and also got thinking. Too many things in it from the experience of an engineer.
Here is the article – The care and feeding of software engineers (or, why engineers are grumpy)
November 25, 2011
Finding Union and Intersection of 2 Large Lists
One of my colleague who works on performance tracking and tuning of applications asked for some helped around how to find the union and intersection of large lists. He works mostly on Python and Perl. Being a Java guy, I prepared a sample program for him to do this allowing to determine the size of the lists by himself. He was happy to get the program and once I explained him a bit about the retailAll(), addAll() and removeAll() API of Java and how I used those to determine the union and intersection, it was very clear to him. I am giving that program here in case it helps you as a reference implementation.
package com.salesforce.test;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
/**
* To compiple: javac -d . ListPerformanceTest.java
* To run: java com.salesforce.test.ListPerformanceTest
*
* @author ashik
*/
public class ListPerformanceTest {
private int LOOP_COUNT = 50000;
private List firstList;
private List secondList;
public ListPerformanceTest() {
firstList = new ArrayList();
secondList = new ArrayList();
for(int i = 0; i < LOOP_COUNT; i++) {
if(i % 3 != 0 || i % 5 != 0) {
firstList.add("ashik - " + i);
}
if(i % 9 != 0) {
secondList.add("ashik - " + i);
}
}
}
public static void main(String[] args) {
System.out.println("\nListPerformanceTest starts.....\n");
ListPerformanceTest perf = new ListPerformanceTest();
List intersection = new ArrayList();
List union = new ArrayList();
Date d1 = new Date(System.currentTimeMillis());
System.out.println("d1 = " + d1);
for(String value : perf.firstList) {
System.out.println("value for firstList = " + value);
}
Date d2 = new Date(System.currentTimeMillis());
System.out.println("d2 = " + d2);
for(String value : perf.secondList) {
System.out.println("value for secondList = " + value);
}
Date d3 = new Date(System.currentTimeMillis());
System.out.println("d3 = " + d3);
System.out.println("perf.firstList.size() = " + perf.firstList.size() + " and perf.secondList.size() = " + perf.secondList.size());
if(perf.firstList.size() >= perf.secondList.size()) {
intersection.addAll(perf.firstList);
intersection.retainAll(perf.secondList);
} else {
intersection.addAll(perf.secondList);
intersection.retainAll(perf.firstList);
}
Date d4 = new Date(System.currentTimeMillis());
System.out.println("d4 = " + d4);
System.out.println("intersection.size() = " + intersection.size());
if(perf.firstList.size() >= perf.secondList.size()) {
union.addAll(perf.firstList);
union.removeAll(perf.secondList);
union.addAll(perf.secondList);
} else {
union.addAll(perf.secondList);
union.removeAll(perf.firstList);
union.addAll(perf.firstList);
}
Date d5 = new Date(System.currentTimeMillis());
System.out.println("d5 = " + d5);
System.out.println("union.size() = " + union.size());
System.out.println("\nListPerformanceTest ends.....\n");
}
}
The significant part from the output when you run the program is given below.
d3 = Fri Nov 25 10:03:36 PST 2011
perf.firstList.size() = 46666 and perf.secondList.size() = 44444
d4 = Fri Nov 25 10:03:55 PST 2011
intersection.size() = 42222
d5 = Fri Nov 25 10:04:14 PST 2011
union.size() = 48888
August 11, 2011
Removing an element from a Collection while iterating over it
Can you remove an element from a collection while iterating over it? Answer: No, you can’t.
What kind of error will you get in Java if you make an attempt? Compile time or Runtime? Answer: Runtime.
The details of the exception will be as below. Check this along with a sample program that I wrote. You can try to compile and run it yourself to see the result.
Exception in thread “main” java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
package com.salesforce.test;
import java.util.List;
import java.util.ArrayList;
/**
* To compiple: javac -d . RemoveListTest.java
* To run: java com.salesforce.test.RemoveListTest
*
* @author ashik
*/
public class RemoveListTest {
public List counts = new ArrayList();
public RemoveListTest() {
counts.add(0);
counts.add(1);
counts.add(2);
counts.add(3);
counts.add(4);
counts.add(5);
counts.add(6);
counts.add(7);
counts.add(8);
counts.add(9);
counts.add(10);
counts.add(11);
counts.add(12);
counts.add(13);
counts.add(14);
}
public static void main(String[] args) {
System.out.println("\nRemoveListTest starts.....\n");
RemoveListTest rlt = new RemoveListTest();
List results = new ArrayList();
for(Integer count : rlt.counts) {
System.out.println("count before removing = " + count);
if(count % 3 == 1) {
results.add(count);
// rlt.counts.remove(count); // you will get runtime error if you uncomment it
}
}
for(Integer result : results) {
System.out.println("result = " + result);
rlt.counts.remove(result);
}
for(Integer count : rlt.counts) {
System.out.println("count after removing = " + count);
}
System.out.println("\nRemoveListTest ends.....\n");
}
}
February 11, 2011
Waiting for NoteSlate
A few days back my Google Reader RSS Feed took me to http://www.noteslate.com and from then onwards NoteSlate is roaming around my mind. I believe Kindle solved my problem of reading books, iPad solved the problem of a personal organizer cum web surfing but none of them really addressed the issue properly of writing or taking notes. NoteSlate will do that if I understood correctly what there current ad shows. Ah, I have to wait 5 more months to verify it!
January 25, 2011
I won the first round of the Chess Tournament in Salesforce.com
I played the first round of the four round chess tournament in Salesforce.com. I won with White, although I felt I am out of touch by a good margin due to the lack of practice. My opponent was Didier Prophete who is PMTS and sits in 3rd floor in the same building as I do. Also I took lot more time than my opponent to move.
However, I will do some practice before next rounds so that I don’t make simple miscalculations.
http://www.chess.com/emboard.html?id=589523
Soon after this, I exchanged the queens to make sure there is nothing left in the board except my Rook and Didier’s Bishop while I have a bunch of queens side passed pawns to march for promotion. So he resigned here while I had some 3 minutes left in the clock as opposed to his 9 minutes.
Last few days I finished Himu Rimande and Kichukhkhon by Humayun Ahmed.
December 19, 2010
Computers With Attitude
I thought it’s funny but I believe all the major O/S vendors are already capable of creating something this now.
Small Java Program To Convert Unicode Character Filled Sentences To Native or UTF-8 Character Filled Sentences
My colleague John asked for a tool from me last week so that he can finish the translations / localization works that he was doing. To solve it in a lazy way, I did some google around a bit but couldn’t figure out any easy tool that will convert Unicode characters \uxxxx to native locales. There are plenty of tools available to convert natives to UTF-8 and even the below simple command works in java for native to utf-8 :
native2ascii.exe -encoding UTF-8 your-input-file-name.properties your-output-file-name.properties
So what I have done is just written a small java program that will help you to achieve the reverse of it i.e. converting from unicode to native / utf-8. Here are the steps you will need to perform.
package com.salesforce.test;
import java.io.*;
/**
* To compile: javac -d . SfdcUnicodeToNativeConverter.java
* To run: java com.salesforce.test.SfdcUnicodeToNativeConverter
*
* @author ashik
*/
public class SfdcUnicodeToNativeConverter {
private static String fileName = "output.properties";
private static String sentenceToConvert = "cmgt_configurator/Sfdc_common_summary_32=\u304a\u652f\u6255\u3044\u60c5\u5831\u306e\u5165\u529b";
/*
\u304a\u652f\u6255\u3044\u60c5\u5831\u306e\u5165\u529b
\u304a\u652f\u6255\u3044\u6761\u4ef6 & \u5951\u7d04\u671f\u9593\u306e\u7de8\u96c6
\u6b64\u8ba2\u5355\u7684\u603b\u4f63\u91d1+
\u82e5\u8981\u7e7c\u7e8c\u7d50\u5e33\uff0c\u60a8\u5fc5\u9808\u8a73\u95b1\u8b80\u4e26\u63a5\u53d7\u4ee5\u4e0b\u6240\u5217\u6bcf\u500b\u7522\u54c1\u7684\u689d\u6b3e\u3002
*/
public static void writeOutput(String str, String fileName) {
System.out.println("Unicode to Native Conversion Starts...");
try {
FileOutputStream fos = new FileOutputStream(fileName);
Writer out = new OutputStreamWriter(fos, "UTF8");
out.write(str);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Unicode to Native Conversion Successful!");
}
public static void main(String[] args) {
writeOutput(sentenceToConvert, fileName);
}
}
1. Make sure sure you have JDK installed in your system. Set the environment variable JAVA_HOME in to point to JDK (alternatively there is a shorter way that I can show you).
2. Copy the source java file from the following network location into your machine – \\moorea\departments\AppStore\Comergent\Programs\SfdcUnicodeToNativeConverter.java
3. Open the source file in TextPad or Crimson Editor and replace the value for the variable sentenceToConvert to the actual sentence that you are trying to translate / convert.
4. Compile the java file that I wrote using the command javac -d . SfdcUnicodeToNativeConverter.java
5. Run the program to generate the output file using the command java com.salesforce.test.SfdcUnicodeToNativeConverter
6. A new file named output.properties should be generated in the same folder from where you ran the program.
7. Open this file with Altova Xml Spy or Notepad with UTF-8 Encoding to preserve the file format correctly. You may see some characters that look like junk. Don’t worry, those are not junks.
8. Copy the content from the file in Xml Spy or Notepad and paste those into a new MS Excel Spreadsheet. Now you should see the native characters properly. You can use those for translation purposes I your models.
I have tried to write down the steps above clearly as you will need to perform those repeatedly for each line of translations for each locale. Being a little innovative, you can use the steps above for multi-line translations as well by adding proper \n” + ” to hold many properties / values in the sentenceToConvert variables.
October 19, 2010
Salesforce Chatter Desktop and Salesforce Mobile
I just installed Salesforce Chatter Desktop and Salesforce Mobile. Interesting. If you want to download or know more, follow the link below.
http://www.salesforce.com/chatter/desktop-mobile-collaboration/
March 15, 2010
Convert Http URL to Https URL – A Short Sample Code
Here is an example of how you convert an Http URL to a secured URL (Https). Here I am assuming the URL will be fully standard i.e. even the default port 80 will be written after colon as :80 and same for default https port :443.
package com.google.test;
/**
* To compiple: javac -d . ConvertHttpToHttps.java
* To run: java com.google.test.ConvertHttpToHttps
*
* @author ashik
*/
public final class ConvertHttpToHttps {
private static String url = "http://configure.google.com:80/AppStore/en/US/enterpriseMgr/AppStore";
public static void main (String[] args) {
System.out.println("url before replacing = " + url);
System.out.println("url after replacing = " + convert(url));
}
public static String convert(String url) {
StringBuffer result = new StringBuffer();
if(url.startsWith("http://")) {
String strColon = url.substring(7);
result.append("https://");
int colonIndex = strColon.indexOf(":");
String portNumber = strColon.substring(0, colonIndex);
result.append(portNumber);
result.append(":443" + strColon.substring(colonIndex+3));
}
return result.toString();
}
}

