This function makes use of Douglas Crockford’s functions available from his Remedial Javascript page. If you haven’t read his series of articles on Javascript, you’re missing out. Also, check out the Yahoo User Interface team’s website and the YUI blog for more Javascript info and tips.
Crockford’s isEmpty function:
function isEmpty(o) {
var i, v;
if (typeOf(o) === 'object') {
for (i in o) {
v = o[i];
if (v !== undefined && typeOf(v) !== 'function') {
return false;
}
}
}
return true;
}
As a question to the readers, how should the isEmpty function treat object properties that are undefined or functions? Currently, the function ignores undefined properties and functions. Should it count them as properties?
My getProperties function looks much like the isEmpty function and uses the same method for accessing an object’s properties. Rather than return true or false based on the existence of properties, this function returns an array of all of the property names for the given object. A trivial modification of the isEmpty function, but useful none the less.
function getProperties(obj) {
var i, v;
var count = 0;
var props = [];
if (typeof(obj) === 'object') {
for (i in obj) {
v = obj[i];
if (v !== undefined && typeof(v) !== 'function') {
props[count] = i;
count++;
}
}
}
return props;
};
Templating in Java: What to use?
April 9, 2008
I’m looking into using a templating engine for formatting output from a Java component. From what I can tell, the top 3 templating engines for Java are Apache Velocity, Jxp, and FreeMarker.
Right away, Velocity stands out given the reputation of the Apache Foundation. Looking at Jxp, though, it’s appealing to be able to use standard Java code with the JSP-like syntax. If we can believe the FreeMarker site comparing it to Velocity, FreeMarker has better support for number and date manipulations (among other things), which could be particularly important for my uses.
I haven’t tried any of these engines out, yet. Rest assured, though, that I will try them and post my findings.
Profiling with Netbeans: Do You Have a Memory Leak?
February 23, 2008
This article on Netbeans.org discusses using the profiler bundled with the Netbeans IDE to detect memory leaks. By monitoring the surviving generations metric, one can supposedly catch a leak. I believe that this article ignores a certain situation.
New Hard Drive Woes for the Dell Dimension
February 11, 2008
As described in my earlier post, I managed to get my hand-me-down Dell Dimension 8400 desktop working after a little tinkering and a lot of reading. I installed Ubuntu 7.10 and was up and running for a good week.
But, alas, my poor computer was destined for yet another crippling problem. One evening as I read the news, the cpu usage maxed out, and the computer was unresponsive. I believe Firefox was the only application open. Regardless, it was under a very light load.
After turning the computer off, I waited a few seconds, and then pushed the power button. Instead of booting from my Ubuntu partition, Grub presented me with a command line. I tried manually booting from the hard drive with no luck.
ConcurrentHashMap to the Rescue
February 2, 2008
It was only a matter of time before I needed a Thread-safe Map. Looking at the synchronization wrappers for the Java Collections framework, I couldn’t help but be disappointed. The wrapper classes provide external synchronization on the entire Collection. That means that any read or update method is locking — one thing in at a time. What a performance bottleneck!
Read the rest of this entry »
Flashing Orange Light on Dell Dimension 8400
January 27, 2008
After reading all sorts of problem descriptions and solutions on various forums, I’ve finally fixed my hand-me-down Dell Dimension 8400 desktop box.
When I first got it, I couldn’t even get it to turn on. After pressing the power button, it gave a few beeps, started an orange blinking with the power light, and refused to do anything else. Some people with the same problem had to replace the motherboard, the power supply, or the memory. Some still don’t have a working computer.
As for me, I unplugged all of the devices from the power supply, removed the memory sticks, and then put it all back together. Something along the way did the trick — I suspect it was removing and re-inserting the memory, but I’m not certain.
Now my box is up and running with Ubuntu 7.10!Leave a comment if you’ve had a similar problem with your Dell — I know there are a lot of you guys out there. Maybe you can post some other solutions and ideas here to help out…
Update: According to the Dell manual, there are 4 lights on the back of the tower labeled A, B, C, and D. If the A and B lights are orange, but the C and D lights are green, it could mean that the memory isn’t recognized and that you need to take out the memory sticks and put them back in again. This sounds like it could have been my problem.
Hashtable or HashMap?
January 17, 2008
Working on some Java code, I recently found myself in need of a hash-type data structure. The project I’m working on frequently uses a home-grown hash table implementation, but I wanted to transition to the more recently developed Java Collection classes. Since J2SE 1.5.0, the data structures in the Collections framework have been updated to use Generics, which are cleaner and provide safety against casting errors.
Without giving it much thought, I started using the java.util.Hashtable implementation. But soon I stumbled upon the java.util.HashMap class. Not knowing the differences between the two, I read through the API documentation to compare both implementations.
As it turns out, there are some important differences. First, Hashtable is synchronized, meaning that it can be used concurrently by multiple processes/threads without the need for synchronization outside of the object. HashMap, on the other hand, is not synchronized. To use a HashMap object in a concurrent programming environment, you’d need synchronization external to the object.
Secondly, Hashtable does not allow null keys or values, whereas HashMap does. This was important for my project because the existing code required the ability to use null as a value. To change that behavior would be outside the scope of my task.
Finally, HashMap allows values to be safely removed during iteration, but Hashtable does not.
After comparing the two Collections classes, I chose to use HashMap mainly due to the fact that it allows null values. Since the existing source code requires null values in some cases, this ability was a necessity. The fact that HashMap does not provide synchronization is insignificant for my project — there is no concurrent access to these data structures. Although concurrency is planned in the future, there already exists a need for synchronization at a higher level.
Regardless of the differences between Hashtable and HashMap, I feel much better about using either one than the in-house hash table implementation developed circa jdk 1.0. At least I won’t have to worry about casting.
Update: After reading a little more about the Collections framework, I’ve discovered that there is a way to get synchronized versions of the unsynchronized classes. Java provides a set of synchronization wrappers capable of producing thread-safe versions of any Collection. Pretty cool!
Kayak Travel Search Engine
December 13, 2007
A colleague recently told me about Kayak. It’s a tool for searching flights, hotels, rental cars, and cruises, but I’ve only used it to lookup up and compare flights. Kayak’s pretty useful for finding the right flight; you can choose the number of stops, which airlines you’d like to consider, and what online travel agencies you’d like to search.
Some of its more interesting features are charts that show the historical prices for flights between the cities you picked. There’s a chart for the past 90 days and a chart for every day of the month you’ve chosen to travel.
On top of its handy features, Kayak has an interface that’s both useful, easy on the eyes, and easy to navigate. Plus, the site uses advertisements sparingly. Most travel sites I’ve seen are teaming with ads, slow to load, and visually confusing. Nice job, Kayak.
Simple Leftover Thanksgiving Turkey Recipe
November 24, 2007
This comes courtesy of my soon-to-be mother-in-law:
1/2 to 1 lb. cooked turkey
2 cups gravy
1 lb. campanelle pasta
1/2 to 1 cup grated parmesan cheese
Chop turkey into small pieces. Boil the pasta, drain, return to pot. Heat gravy in separate container. Add turkey and gravy to pasta. Salt and pepper to taste. Spoon the mixture into a large casserole dish and top with parmesan cheese. Bake at 375 degrees for 20-30 minutes.
This simple recipe is a great way to use the leftover Thanksgiving turkey’s flavor in a new setting, and it’s quick and easy to prepare. Give it a try and tell me what you think!
