Imagining a method which receives as a parameter a type Object instance and needs to perform some action according to its type is not difficult. In fact, there are several design patterns that, when used in combination, help solve just that. However, this post is about adding a new switch..case construct to prevent some boiler-plate caused by these patterns.
Continue reading »
Whenever a class in my model contains a collection which requires that particular care be taken with its items, there’s an internal debate regarding how to expose it to other classes. And with this, there are two major schools: one, the paranoia-based approach which doesn’t allow external code to touch the collection’s internal items and two, the trusting approach which just returns the collection for everyone to deal with.
Continue reading »
Time for some refreshing the memory with a Java language feature probably few use, and maybe for a good reason. Suppose you’re running code on elements of an array up until a certain element is found. When your code finds that element, it stops the iteration. Therefore, your code might look like the following:
for (int i = 0; i < array.length && condition(array[i]); i++) {
// your code here
}
Or, if you’d like to use the new foreach feature, like this:
Continue reading »
