Thursday, December 29, 2011

WEBrick Server -- Note to self

This is the absolute last time I am telling this to myself before popping the cyanide pill (which I don't have)

"YOU HAVE TO BE INSIDE YOUR APPLICATION FOLDER IN ORDER TO BE ABLE TO START THE WEBrick SERVER"

Also, if the browser's not showing what's it supposed to...
JUST RESTART THE DAMN THING!

Wednesday, December 28, 2011

The new smartphone

Just watched the video review of Nokia Lumia 800 on youtube and I must say I was quite impressed with what Nokia managed to roll out in such a short period of time. That said, however, the internet world thinks that it's the "doomed" phone. Rightly so, because it hasn't attracted that much attention anyway. Must be heartbreaking, specially for their engineers, to see their efforts almost go down the gutter. But I guess this is how the consumer market is. Apple could come up with a small tweak and still sell millions of copies and folks like Nokia could come up with a whole new product lineup and still sell only a handful.
However, after all said and done, one thing that every handset manufacturer seems to be missing is that they're not giving their customers anything new. The Apples made a huge paradigm shift in the handset market when they came up with a touchscreen phone and it's been like that for like forever now. Obviously, it's not everyday that you can roll out a new operating system and a pool of half a million apps  but there are bunch of other things you can actually go creative with...

New hardware design: I agree that there isn't much scope for getting creative with the design of handset, but then the same is true for the cars and the automotive industry has been able to surprise people every year for decades now. I strongly believe that a creative handset design that's got good aesthetics stands a great chance unless there are other showstoppers. For example, Sony Ericsson's  Xperia X10 was a real eye candy when they revealed it. I and quite a few people I know were pretty much looking forward to buy it right away. Unfortunately, it sported Android 1.6 as against Samsung's Galaxy S which sported Android 2.0 at that time. A real showstopper, specially with the lousy touchscreen response. Bottomline - a great hardware design with latest OS version of whatever platform.

Camera: It's no secret that people dig phones with good camera. Not only that, recent studies show that point and shoot camera market has perished because of the smartphone cameras. So, clearly, give people the best main camera and a good front facing camera and that must give consumers a strong incentive.

Content: This pretty much covers everything. Games, Audio, Video, Books and Apps. Now, surprisingly, no one has been able to integrate smartphone, PC and a tablet all that well. There's a lot of scope for apps to get more intelligent, hardware permitting. Siri being a quintessential example. Siri, however now is a thing of the past. With hundreds of apps flooding the market each day, there's no doubt whatsoever that we have enough developers. All companies have to do, is start an ambitious open source project that's interesting enough and has some incentives for the developers.
Media:
Smartphone as a music player has a lot of potential and the capabilities need to be integrated deep into the OS itself. Android has that capability but it's quite rudimentary. Players need to be smarter, like Apple's genius (although I haven't had much of a luck with it, but I believe with proper content, it must work). Smartphones haven't done a lot in terms of online gaming. People are getting lazy and more lazy these days and specially the ones like me don't want to spend too much money or time on my music selection. This is where things like Pandora and Slacker or even Spotify comes into picture. These things need to be integrated well into the OS itself. Phone manufacturers or service providers might have to manage their own content library but cutting the deal with companies like Amazon with huge media library should not be impossible.
Games:
Sony and Microsoft have infrastructure in place already but somehow they've failed to bring the glory to the phone platform. Multiplayer gaming with phones could be really cool.
Books:
Quite honestly, I don't see myself reading a book on my phone, no matter how big the screen is. So efforts in that direction is clearly a waste of time for me and for like minded people.
Innovation:
As said earlier, innovation is the key. People need to see their phone do something new. Controlling their thermostats for example. Goes without saying that innovation without utility isn't an innovation at all. Vlingo for example was there for quite some time, but it was Siri who made the cut. Same for the facetime. Phones need to act more like a personal assistants than just a fancy device in your pocket. This means, they need to do a value addition to your work, organize stuff for you, communicate with your cars, act as your credit cards and these are just a few ideas. Increasing the megapixels on the camera or increasing the screen size alone is not going to help anymore, there's a huge scope for innovation and that's where companies need to target.

Tuesday, September 27, 2011

"Why are you so difficult man?"


"Man, you seem to be extra-happy today!!"
"What do you mean extra-happy? I am always happy"
"Is it your birthday or something?"
"Oh C'mon. I'm always happy, did you see me sad any time? but yeah, if you keep pissing me off, I can be very bad. Try that... ha ha ha... just kidding"
"Ha ha, I know... Ok tell me, does this thing have beef or pork in it.."
"Yeah, try it, it's great !!"
"Nah, I eat only chicken!!"
"C'mon man, you can eat it everyday!!"
" In fact I eat it everyday and you know that. But I don't eat pork or beef."
"Ah... not this, not that... why are you so difficult man?" And both of us started laughing. This is one of the usual conversations between me and the chef at the Mexican food corner in the office cafeteria. But this time, this unusual word "difficult" managed to ring bells in my mind. Am I difficult? Really?

Monday, September 26, 2011

Data Processing...lol

There are 3 rules to follow when parallelizing large code bases.
Unfortunately, no one knows what these 3 three rules are.

--Gary R. Montry

Thursday, September 22, 2011

Classloader exception

Few hours ago, I had probably the most stressful 15 minutes of my life when I ran my programming assignment as a final check before submission and it did nothing but clean exited when I hit the run key. I went back to the test cases I had written for the modules and checked to see if everything worked fine. I had 100% success in there. Although, my program still won't print anything... strange!
So, as any programmer would do, I put the breakpoint at the very beginning and started debugging. Everything was still working fine, except when I came to the point where I was instantiating one of the classes, I got the classloader exception. I thought, that there probably was an issue with that class, so I rebuilt the program so the .class files get regenerated. Still no luck!
Update:
I am probably the silliest person on earth.Any beginner book will tell you that classloader exception is thrown when the class loader is unable to load the class. That is, the class doesn't exist. Which made sense in my case, because I had just refactored the code and update the other file while I did that. I still don't understand how the project was compiled though :S

Object oriented approach for user interface

Coming soon...

Friday, August 5, 2011

Coding Vs Alcoholism

There isn't much of a difference in my opinion... here's why:
1. It takes a while to develop a taste for both
2. Sooner or later, it becomes an addiction
3. Too much of both makes you dizzy
4. There's no going back from either...

\m/

Tuesday, July 26, 2011

How to generate a random string in ruby

Just ran into this while googling for random string generation in ruby. This is pretty awesome:

Also, thanks to divisionoftigers for posting it on stackoverflow here

Let me put it directly here instead:
require 'active_support/secure_random'
random_string
= ActiveSupport::SecureRandom.hex(16)

Friday, July 22, 2011

Productivity is inversely proportional to process -- In my arrogant opinion!

Experience says it and even the observations. Companies creating wonders are fairly disorganized within the teams! I've been talking to my friends whose companies are doing great and are running with minimal documentation and processes and so called systems. On the contrary, I work at a place where how you do it is more important than whether it works or not, which adds to a great extent in Quality (non)Assuarance!
I have always been very opinionated about things and with due respect to the genuine intentions behind the workflows and procedures, I think nothing would do more harm to the quality of the product than the mindless implementation of a good workflow (this is assuming that the developers are pretty darn good at what they do). It is this very blindness, mindlessness, the culture that prefers standard procedures over genuine ideas, preference to stability over adaptability that's making me go crazy! It is frustrating to test a product that in your own opinion -- sucks, it is frustrating to see raised eyebrows when something fails, it is frustrating to not being able to fix it yourself and rely on someone else who apparently wouldn't fix it anyways because apparently that person wasn't able to "reproduce" it.
I am hating being a part of the system and my threshold is almost reached.
I shall move on... hiring anyone?

Thursday, June 30, 2011

"Hassle-free" life of a software engineer

Out of nowhere, I have this urge to learn development on rails now... since everyone thinks that it's so awesome and most importantly it's free and has shitload of free reference material online. I thought this would be a good idea to "start" (literally) with web development, when I should really be packing my bag for a flight to Chicago tomorrow.
Okay, so I have "THE" Mac OS X 10.6.7 and obviously since Apple think it's so damn awesome, it shipped the laptop with ruby pre-installed... Awesome right? WRONG!!
To go by the tutorials, I want to install rails 3.0.1... which by the way, works with ruby 1.9.x. So, all I need to do now is uninstall the ruby 1.8.6 that my macbook shipped with and install ruby1.9.x. Easy right?? WRONG again. Here's why:
1. To uninstall ruby, I will have to force remove the installation folder manually -- the idea with which I've never been and probably never will be comfortable, since I have messed up such a big time in the past.
2. I know a good way, which will get me started with a clean slate. Yes, I am talking about the rvm (the ruby version manager). The problem is, rvm needs cUrl (a command line utility that lets you download stuff from command line) and cUrl website is DOWN!!
.
.
.
You might as well tie my neck to the tail of the flight tomorrow and take me to Chicago. I bet that'll be way less irritating.
This is certainly one of the situations when I feel that windows is way better!!

Update: My bad! Apparently, MacBook also comes with the curl installed... now that's awesome. Just installed rvm, but rvm notes for snow leopard do insist on updating the xcode, which is what I have to do now :(

Saturday, June 25, 2011

Ubuntu on VirtualBox

In case you're wondering why your Ubuntu installation gets stuck while you're trying to actually install it from the disk that you mounted, it's probably because you have checked the download updates installation in the beginning. Try starting over with that option unchecked to get the downloads out of your way, you can have ubuntu do that separately later when you're sleeping with your headphones on or something (like I do). If that doesn't work, use the try ubuntu option to run it from the cd-rom without installing it and then install it by double clicking the install icon on the desktop. This worked for me on both - mac and windows!
On a personal note, VmWare in my experience, is much more feature rich as compared to Oracle's VirtualBox (and I swear there's nothing more painful than calling it Oracle's VirtualBox instead of Sun's VirtualBox... but I'll get over it someday), but seriously...who buys a software these days? Also, there's a developer kit for VirtualBox... so someday you happen to crave for some opensource development, you can dive into that and I guess it's worth getting used to it now rather than later.

Monday, June 20, 2011

Murphy's Law

Found this line in Linux Kernel Module Programming Guide this morning and aptly so:

"You'll need to compile your code using various header files from the Linux kernel. Murphy's Law states that the headers that are missing are exactly the ones that you'll need for your module work."

lol

Sunday, June 19, 2011

A Note to myself - JTable.setRowSorter(null)

Never, ever use a line of code from a random forum without completely knowing what it can do to your code. I happened to use JTable.setRowSorter(null)
because I wanted to prevent users from clicking the column header and accidentally sorting the row. While I did that, I didn't know that it would actually end up disabling all the filters I might add later. Murphy's law - it did exactly the same and I wasted hours trying to figure out why aren't my filters working when the sorters are returning correct values. Google doesn't really help a lot when it comes to debugging your code. Unfortunately, I did not mention disabling the rowsorter in the changelist when I committed the changes. Thanks to my memory though, it reminded me of that as a possibility and after removing that line of code, everything is golden!

Saturday, June 18, 2011

Creating a Wizard in Java Swings

Getting started:
I was recently asked to create a windows style wizard (step by step execution) in Java.
Creating a wizard essentially means multiple panels should share the same display area. As a beginner, I had a thought that overlapping panels one over the other might work just fine. But you know what, it doesn't! With a lot of pain, you might even get it to work, but here's a thing: java has something called as CardLayout which is perfect for creating a wizard. Here's the tutorial on basic use of card layout. If you are highly impatient, lazy and have a short attention span like me or you think that you don't need to go into much detail of it, skip it.

A good design:
If you care for more than just getting the job done somehow, like I did after stupidly coding everything myself, which by the way was a bad design, now that I know of a better way, you should really think about sticking to some pattern. And by some pattern, I mean MVC which is an obvious (and as far as I know, the only) choice. Here's a very nice and straightforward article on what are your choices and considerations in terms of the architecture.

Ready to eat stuff:
I found out that OpenSwing framework, that I have used for pivot tables has good APIs for creating a wizard. It follows the pattern suggested on the Oracle/Java website mentioned previously and has a straightforward tutorial. Although, I think it would've been nice to have some more ready-made Intro panels (like licence panel) in the miscellaneous package, but you can always create your custom wizard inner panels and add it to the main wizard frame and you should be fine. If you read the Demo14 source, it's not that hard to make out what's happening in 10 minutes or so. Still, I'll brief some steps:

1. Create a client application -- This class will more likely contain the main method which eventually calls the WizardFrame class, and that's all it does.
2. WizardFrame -- This class creates the actual visible window. This class is a good place to add the inner panels on the wizard frame. By inner panels, I mean the screens that appear when you navigate
3. WizardController -- This class embeds the navigation logic of the wizard. Let's say you made a particular choice that requires 3rd screen to be displayed after the first (and not go to second screen at all), you can define that logic here.
4. Intro/First/Second Panels and so on... : These are the screens that are displayed when you navigate. These are extended from the WizardInnerPanel.
5. Execution Engine: Contains the execution logic. And mostly covers what happens when you click a cancel button or a finish button. In the demo application, finish and cancel button are referred to by the same method (getCurrentVisiblePanel.getCancelButton) which is kind of confusing at first, but 5 minutes of debugging will just do the trick.

Tip: Within 15 minutes of use, I found out that when you add WizardInnerPanel to the main wizard frame, the controls on the main wizard frame are reset. e.g. Say you have two radio buttons on the First Panel and initially none of them are selected and unless one of them is selected, you don't want user to go to next screen so you disable the Next button. However, if you've added all the necessary inner panels already, that next button might get re-enabled, if so, you might need to explicitly disable the next button in the init() function of the FirstPanel, like I did.

Thursday, June 9, 2011

Zlib buffer while installing/updating ruby gems

I struggled with Zlib error for hours now and internet world seems to be convinced with the

gem update --system

solution.
Although, the aforementioned is indeed a solution, in my case I got the Zlib error even for

gem update --system

command.

Turns out that you need to update the rubygems-update by:

gem install rubygems-update

If the above command doesn't work as it is, try fetching the rubygems-update to the local repository first by:

gem fetch rubygems-update -v 1.4.0

In my situation, even gem fetch rubygems-update -v 1.4.0
did not work, because it requires the latest build of ruby installed (1.8.7) and I had 1.8.6p26. The trick is to manually download rubygems-update gem, then go to the download directory and install the gem.
After you're done, do:

gem update --system

and proceed with whatever gems you need to install.
Hope this works for a clueless googler like me :)

Oh! While we're on it... I also had an "Application failed to initialize properly" windows error with cl.exe (Cl.exe is some Microsoft Visual Studio executable by the way.) while trying to do:

gem install win32-api -v

Apparently, this requires native C library compilation and I am thinking, because of visual studio installation, my native C library pointed to some dll accessed by cl.exe to which ruby didn't have access and so, the windows error.... As a naive windows user, I uninstalled the Visual studio and the error was gone :)
This reminds me of this girl colleague I went out for lunch with. A guy called her and asked her what to do if CPU usage is 70% even if no application is running. She told him to restart the PC and if that doesn't fix the problem... format it.
I laughed at it and she hates me now (I guess) :P

Cheers!

Vital update:
As it turns out, your life can be made much more easier if you are on a non-windows computer and you use ruby version manager instead of manual ruby installation. rvm will take care of all the version compatibilities etc that you need. RVM also needs a utility that allows users to download stuff on command line. It's called as cUrl and comes pre-installed with a Mac (and I am guessing with linux distributions too).

Another update:
This morning, a colleague came upto me with a similar problem. Manual download of rubygems 1.4.0 worked as expected, but apparently gem update --system ended up installing newer version of watir and few other gems. If there are multiple versions of same gems installed, a require statement in your ruby script might get confused with the paths and might throw errors like require_all not found or package custom_require not found. You should uninstall the unwanted ruby gem versions in such a case!

Tuesday, May 3, 2011

Multiple Table filters in Java

As mentioned in the previous post, I have been working with Java TableFilters and RowSorters for some time now and as it turns out, they are more customizable than I imagined in the beginning. Anyways, this time however, I wanted to combine these filters as in use multiple filters together. e.g. Let's say I have a table as follows:
Name Question Answer CorrectAnswer
A Q1 A1 A1
B Q1 A1 A1
C Q1 A1 A1
D Q1 A1 A1
A Q2 A3 A2 <-- wrong answer
B Q2 A2 A2
C Q2 A3 A2 <-- wrong answer

Now let's say I want to filter/sort this data to
1. Display only selected Names
2.Display only rows where Answer matches CorrectAnswer
3. Do 1 and 2 above together or individually

It makes sense to write separate filters for 1 and 2 above and then use them separately or combine them. With my architecture, I was invoking a new filter of required type whenever a related event occurs on the control. But the problem with this approach was, a new filter would be applied on the rowSorter and since the changes made by filter on the visible table are no persistant and in most of the cases you wouldn't want them to be.
So there has to be a way to combine these filters. There's obviously a way you can do that by means of an andFilter as follows:

compoundRowFilter = RowFilter.andFilter(compoundFilterList);

rowSorter.setRowFilter(compoundRowFilter);

Where compoundFilterList is a list of filters you are trying to make work together.

So, what you can do instead of the following:


protected void newFilter(String filterRegExp) {

textFilter = null;

// If current expression doesn't parse, don't update.

try {

textFilter = RowFilter.regexFilter(filterRegExp);

} catch (java.util.regex.PatternSyntaxException e) {

return;

}

rowSorter.setRowFilter(textFilter);

}

is this:

protected void newFilter(String filterRegExp) {

textFilter = null;

// If current expression doesn't parse, don't update.

try {

textFilter = RowFilter.regexFilter(filterRegExp);

} catch (java.util.regex.PatternSyntaxException e) {

return;

}

filterList.add(textFilter);

compoundRowFilter = RowFilter.andFilter(filterList);

rowSorter.setRowFilter(compoundRowFilter);

}
I got this idea from here

Now, this will work fine in most of the cases, unless, your filters are dynamic. e.g. in my case, my filter changes as I type. So, each filter will be "added" to the filterList instead of being updated. This will keep on adding filters to the table as there's no way (or at least it's very difficult to) remove a specific object from a list. Also, I could not find any method that removes the filter altogether from rowSorter.
So, I figured, I should be able update a particular filter object in the list rather than to add it. Again, I found it difficult to update it in the filterList itself. So I decided to have a fixed number of filters in an array of filters and whenever a filter updates, update in it's position in the array and construct a list out of it. Note that, null filters cannot be applied, so I had to make sure that the filter objects aren't null, before adding them to the list:
This is how I did it:

private RowFilter[] compoundFilterArray = new RowFilter [numberOfFiltersYouWant];

And later in the filter code:

protected void newFilter(String filterRegExp) {

textFilter = null;

// If current expression doesn't parse, don't update.

try {

textFilter = RowFilter.regexFilter(filterRegExp);

} catch (java.util.regex.PatternSyntaxException e) {

return;

}

this.compoundFilterArray[1] = textFilter;

applyCompoundFilter();

}


protected void newFilter(final int userAnswerColumnIndex,

final int expectedAnswerColumnIndex) {

incorrectResponseFilter = new RowFilter() {

@Override

public boolean include(Entry entry) {

String actualResponse = entry

.getStringValue(userAnswerColumnIndex);

String expectedResponse = entry

.getStringValue(expectedAnswerColumnIndex);

return expectedResponse.contains(actualResponse);

}

};

// replace the filter with new object, each time the filter is updated

this.compoundFilterArray[0] = incorrectResponseFilter;

applyCompoundFilter();

}



where:

private void applyCompoundFilter() {

ArrayList> compoundFilterList = new ArrayList>();

for(int i = 0; i< compoundFilterArray.length; i++){

if(compoundFilterArray[i] != null){

compoundFilterList.add(compoundFilterArray[i]);

}

}

compoundRowFilter = RowFilter.andFilter(compoundFilterList);

rowSorter.setRowFilter(compoundRowFilter);

}

Sunday, May 1, 2011

The Awesomeness of Java table filters

While working on a coding project related to my thesis, I ran into java table filters. Initially, I was quite hesitant to use those, merely for the obscureness bundled with them, but soon I was proven wrong. At first I thought, I'd be better off creating a database and constructing the queries as we go. Apart from the fact that I have historically hated databases, for slowness, dependencies and the time required to design them and lack of visibility in terms of wtf is exactly happening inside, I decided to give java filters a shot, simply because all I needed to do was deal with a visible table and have user play with it to his/her heart's content.
The first thing I look for when using something I have never used before is the working example, e.g. on roseindia.com or something instead of looking at the javadocs. In short, I cheat because I am impatient and I do not wish to learn the language, but instead get the work done! Recently, I have been avoiding roseindia.com because of poor coding style, but those guys are bang on in terms of how to use something, no bullshit! I don't exactly remember, but I guess there's not much about filters on roseindia.com so I settled for java tutorials example which i found here.
Example is pretty much self explanatory, in the sense that it gives you at least two key hints:
1. You have to add a DocumentListener to a text field and override all the update methods that come with the DocumentListener interface. But where this example or any other example anywhere on the internet including sun (now oracle) website fails is that these guys end up defining the listener at the exact same place where it was added on the field. This is what I am talking about:

filterText.getDocument().addDocumentListener(

new DocumentListener() {

public void changedUpdate(DocumentEvent e) {

newFilter();

}

public void insertUpdate(DocumentEvent e) {

newFilter();

}

public void removeUpdate(DocumentEvent e) {

newFilter();

}

});


And I seriously believe that this is a very bad way of doing it, because the objects and fields and methods of current class are simple inaccessible within these update method definitions, because this space actually belongs to the new class DocumentListener. In my opinion, a better way of doing it is

filterText.getDocument().addDocumentListener(new YourListenerImplementation(Object1, Object2...));


and


class YourListenerImplementation{


public YourListenerImplementation(Object1, Object2....){


}

@Override

public void changedUpdate(DocumentEvent event) {

// do stuff here

//create new filter each time

}


@Override

public void insertUpdate(DocumentEvent event) {

//do stuff here

// create new filter each time

}


@Override

public void removeUpdate(DocumentEvent event) {

// do stuff here

// create new filter each time


}


}

So, now you have a freedom to parameterize the filter. This way, filter can be reused as well.

2. Regular expression is the key:
Now comes the filter part. Against my expectations, this was very easy! All I needed to do was to make sure that I am receiving a valid regular expression from the caller. What example also fails to demonstrate is that you can actually search over the entire row instead of sticking to a particular column. Had I found that out earlier, it would have saved me writing an additional function, but better late than never! I ended up implementing a filter, which takes multiple comma separated values from a text field and lists the row that contain either of those values. And it all happens as you type. I was blown away by this feature. So here's how you can construct queries and apply filter:

// Read the text field, construct and return a regular expression:


private static final String COMMA = ",";

private static final String OR = "|";


private String getSearchRegularExpression(){

String filterText = filterControlsPanelHandlerObject.getFilterText();

return filterText.replace(COMMA, OR);

}

// call the filter in update methods

@Override

public void changedUpdate(DocumentEvent event) {

yourTableClassWithFilterMethod.newFilter(getSearchRegularExpression());

}

And here's how the filter method looks like:

private TableRowSorter rowSorter;

/*

* initialize rowsorter etc...

*/

protected void newFilter(String filterRegExp) {

RowFilter rowFilter = null;

//If current expression doesn't parse, don't update.

try {

rowFilter = RowFilter.regexFilter(filterRegExp);

} catch (java.util.regex.PatternSyntaxException e) {

return;

}

rowSorter.setRowFilter(rowFilter);

}



And the functionality is ready to be used!! Enjoy :)