ListIterator for…well, iterating lists, of course

April 17th, 2008

When programming, it’s easy to mindlessly reach for the same tool repeatedly. The tool is well known and has worked in the past. Ole reliable. But there comes a time when looking beyond the familiar in favor of something more effective can pay big dividends. Maybe something that’s been overlooked simply out of habit.

Until recently, I used an Iterator to traverse Lists. But when a requirement to traverse a list backwards crossed my desk, I (finally) went looking for something better. I suppose it took a new requirement for me to open my eyes wider because right there, in the same package, is ListIterator. A ListIterator allows you to:

  1. traverse a list in either direction
  2. modify the list during iteration
  3. obtain the iterator’s current position in the list

IMO, the best feature of ListIterator is modifying the list during iteration. For example:

ListIterator<BizItem> iter = mylist.listIterator();

while (iter.hasNext()) {

BizItem next = iter.next();

if (next.isTooOld()) {

iter.remove();

// or iter.set(new BizItem());

}

}

Tame Web URLs with JSTL and Enums

January 7th, 2008

I discovered Java Enums last year. My favorite usage thus far is using them in conjunction with JSTL to construct web URLs:

<c:url value='/myapp/JustDoIt.htm' var='myHref'>
<c:param name='<%=WebParam.foo.name()%>' value='1' />
<c:param name='<%=WebParam.bar.name()%>' value='2' />
</c:url>
<a href="${myHref}>Just Do It</a>

…which results in the following HTML:

<a href="/myapp/JustDoIt.htm?foo=1&bar=2">Just Do It</a>

To retrieve the parameter values from within the controller, simply use the enum:

String foo = request.getParameter(WebParam.foo.name());
String bar = request.getParameter(WebParam.bar.name());

The best part is compliance with the DRY principle. The parameter names are uniform and not duplicated through out the web application making it easy to both search for usage and rename parameters.

Got a NoSuchMethodError? Check Your Classpath for Duplicate Classes and/or Interfaces.

January 4th, 2008

Java documentation explains NoSuchMethodError like so:

Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.

So, you’ll appreciate how surprised I was to receive such an error when I modified the signature on a service method and clearly was using the new method appropriately in my webapp. Service module compiled cleanly? Check. Service module installed in the Maven repo? Check. Webapp picking up newly installed service module from Maven repo? Check. WTF!?

And then, Eureka! I had split a bigger module containing transactional and data access objects into respective “service” and “DAO” modules in the past. Like any divorce, it got messy when the “assets” were divided between the two. An interface was moved to “service” but also remained behind in “DAO”. The webapp was compiled against a newer “service” interface, but at runtime the legacy “DAO” was resolved by the webapp via the classpath resulting in a mid-air collision. BAM!

How Many Classes are in that JAR?

January 4th, 2006

A nifty way to count how many classes are inside a .jar file:



jar tvf yourfile.jar | grep ‘\.class’ | wc -l



Of course, using any file extension in lieu of .class will do the same for others!

AspectJ’s Moving Parts

October 28th, 2005

Definitions

  • Join Point - A well-defined point in the program flow; a specific line of code in a specific class file.
  • Pointcut - Picks out certain join points and values at those points; A collection of join points across classes that meet similar criteria.
  • Advice - Code that is executed when a join point is reached; brings together a pointcut (to pick out join points) and a body of code (to run at each of those join points).
  • Aspect - The unit of modularity for crosscutting concerns. They behave somewhat like Java classes, but may also include pointcuts, advice and inter-type declarations.
  • Inter-type Declarations - declarations that cut across classes and their hierarchies; may declare members that cut across multiple classes, or change the inheritance relationship between classes.

General Goodies

  1. Pointcuts and advice dynamically affect program flow, inter-type declarations statically affects a program’s class hierarchy, and aspects encapsulate these new constructs.

Kill ‘Em All

October 13th, 2005

A very handy Linux/Unix command: killall -9 your binary



Example:

killall -9 java

…kills all java JVM processes for..say..a runaway servlet container like Tomcat.

Handling the Server Response with AJAX

September 23rd, 2005

A seemingly standard way to handle the server response when using AJAX:

http_request.onreadystatechange = nameOfTheFunction;

where nameOfTheFunction is an anonymous function that should look something like this:

if (http_request.readyState == 4) {
// good to go!
} else {
// still waiting...
}

The different types of states include:

  • 0 (uninitialized)
  • 1 (loading)
  • 2 (loaded)
  • 3 (interactive)
  • 4 (complete)

Mounting C: for Cygwin

January 5th, 2005


The Cygwin FAQ - Question and Answers



Understanding Classloaders: log4j in a J2EE Environment

August 19th, 2004


ONJava.com: Understanding Classloaders: log4j in a J2EE Environment



Excellent article! Saved me from impending doom with Weblogic.

Running a CVS server

August 10th, 2004


Running a CVS server



Instructions on how to set up anonymous, read-only access to CVS