Categories
Coding

Commons::Net Update

I have just started working on a new branch of Commons::Net which will be a Tiger (Java 5.0) based implementation. This will not require huge changes, but will clear the decks for some of the following features:

  • Removal of all extra dependencies (currently Jakarta ORO is the regular expression implementation used in Commons::Net. It has done and still does a fine job, and the JDK has only matched its functionality in release 5.0. However, the less extra dependencies, the better).
  • JCE-based secure cipher and channel implementations without having to include the extra JSSE jar.
  • A cleanup of some of the more gnarly threading and I/O code. Some of the concurrency classes may come in useful here. I was originally thinking that NIO may be a good option to look at here, however now, I am less convinced. I think the extra complexity may be too much for the potential gains, which would be small, if any. I believe that NIO is a great candidate for a certain class of server applications that may from time to time require a large amount of resources and the ability to handle severe load gracefully. A good case in point would be an application like Azureus (and they do indeed use NIO heavily). For a small synchronous-I/O client-side library like Commons::Net, it would probably just complicate the code with no real performance gains.
  • Some nice syntactic sugar like generics/compile-time type checking and enhanced for/iterator loops.
  • Miscellaneous items, such as this code which FindBugs located for me:


    public DatagramPacket getDatagramPacket()
    {
    if (dp == null)
    synchronized(this) {
    if (dp == null) {
    dp = new DatagramPacket(buf, buf.length);
    dp.setPort(NTP_PORT);
    }
    }
    return dp;
    }

    Previous to JDK 5, this double-checked lock idiom would not be guaranteed to work. However, by marking the dp variable as volatile, the new Java memory model will enforce the correctness of this block of code.

I have also migrated the existing Maven 1 build setup to a Maven 2-based project. Apart from some minor annoyances (like the alpha-state Plexus plugins not being available where they should be), it was quick and easy to get going. I have always liked Maven, and Maven 2 looks like good improvement. It’s under quite heavy development at the moment (as the Apache SVN logs show), but at least the documentation for version 2 is a big improvement over version 1. In particular, the JIRA changelog report is very neat. I haven’t scratched the surface yet of what Maven 2 is capable of, but I’m hoping to have a good look at it over the next month or so.

Categories
Coding

Java 5 Enums and Priority Queues

Consider the case where you retrieve a list of many objects which have a status code attached. There may be many different status codes, and you need to fish through the list of codes and display the object differently, based on the priority. So for instance, an object may have many tasks attached, each of which has an associated priority. You need to display the object in a
certain manner depending on the outstanding task with the highest priority.

A combination of enums and PriorityQueues make this easy. Firstly, we can create an enum to represent the status codes for an outstanding item:




package uk.co.researchkitchen.tiger;

public enum Status {
  HIGH("red"),
  MEDIUM("amber"),
  LOW("green");
  
  private String color;
  
  public String getColor() { return color; }
  
  Status(String color) {
    this.color = color;
  }
}

Next, we generate a random list of Status instances and add them to a PriorityQueue instance:



package uk.co.researchkitchen.tiger;

import java.util.PriorityQueue;
import java.util.Random;

public class SortStatuses {
  
  public static void main(String[] args) {
    PriorityQueue<Status> queue = new PriorityQueue<Status>();
    
    Status statuses[] new Status[] { Status.HIGH, Status.MEDIUM, Status.LOW};
    Random generator = new Random();
    
    // Pretend list of 100 items, each with a different status code
    for (int i = 0; i < 100; ++i) {
      queue.addstatusesgenerator.nextInt(3) ] );
    }
    
    /*for (Status status : queue) {
      System.out.println(status);
    }*/
    
    System.out.println(queue.peek());
  }

}

There are a couple of features that make this interesting:

  • Java’s Priority Queue implementation sorts by the results of compareTo(). Hence, your candidate objects must implement Comparable, and you need to write boilerplate comparison code. Except…
  • …When you use an enum. Enums implement Comparable directly, and their comparison order is based on the order of declaration of the enumeration instances. Thus, you just need to declare your enumeration in order of priority (highest first), and let the queue handle the rest.
Categories
Coding

FireBug – The Best Firefox Plugin for Developers?

We have been doing quite a lot of Ajax-related work on our project recently. Writing rich-UI applications with a lot of client-side Javascript and Ajax-type functionality can be a chore, due to the lack of comprehensive debugging tools. However, there are some excellent Firefox plugins that make things easier, such as the Web Developer extension, TamperData (a more sophisticated version of the earlier Live HTTP Headers extension), and the sadly underdeveloped Venkman.

However, the newest and best extension is undoubtedly FireBug, which is like a one-stop shop for Web development tools. FireBug 0.4 has recently been released, and now has integrated JavaScript debugging support, which can be triggered by inserting a debugger; statement into your JavaScript code at the point where you wish to trigger a breakpoint. FireBug also has a very useful “Inspector” tool, which can be triggered by a CTRL-SHIFT-C keystroke, upon which any element can be selected, in order to display its source, DOM properties, and CSS styles.

Another useful feature is the console, which allows you to enter arbitrary JavaScript commands within the context of the current page, so if you are using Prototype, your $() etc. commands will work. One very useful feature in FireBug that I haven’t seen anywhere else is its support for Ajax request snooping. You can view each individual request (and response) within the FireBug UI. This extension has already been very useful, from debugging some awkward JavaScript, to debugging hard-to-find conflicts due to CSS precedence rules. I would recommend trying it.