Wednesday, November 23, 2005

Concise Java Collection Syntax with varargs

Here's some utility methods that makes declaring and initializing Collections in Java 5 more concise. Note that this does not make sense to use for Collections that contain a lot of items. Here's the utility methods.


public static <T> List<T> newArrayList(T... elems) {
List<t> newList = new ArrayList<T>(elems.length);
fillCollection(newList, elems);
return newList;
}

public static <T> List<T> newLinkedList(T... elems) {
List<T> newList = new LinkedList<T>(elems.length);
fillCollection(newList, elems);
return newList;
}

private static <T> void fillCollection(Collection<T> coll, T... elems) {
for (T elem : elems) {
coll.add(elem);
}
}

...



So code that looked like this before:


List<String> programmingLanguages = new ArrayList<String>(3);
programmingLanguages.add("Java");
programmingLanguages.add("C++");
programmingLanguages.add("Ruby");




Now looks like this:


List<String> programmingLanguages = CollectionUtils.newList(
"Java", "C++", "Ruby");

Sunday, August 21, 2005

Cygwin tips

One feature that I could not live without when developing on Windows is the "Open Command Window Here" Windows Explorer context menu. I also use Cygwin a lot when developing on Windows and wanted this same functionality for opening a Cygwin window.

I found this blog entry discussing how this can be done. I ended up tweaking this solution a bit so that I could open the rxvt terminal window rather than the bash shell in a DOS window. Here's what I came up with:

[HKEY_CLASSES_ROOT\Directory\shell\bash]
@="Open Bash Shell Here"

[HKEY_CLASSES_ROOT\Directory\shell\bash\command]
@="c:\\cygwin\\bin\\rxvt.exe -e bash --login -I -c 'cd \"`cygpath \"$*\"`\";bash' bash %L"

[HKEY_CLASSES_ROOT\Drive\shell\bash]
@="Open Bash Shell Here"

[HKEY_CLASSES_ROOT\Drive\shell\bash\command]
@="c:\\cygwin\\bin\\rxvt.exe -e bash --login -I -c 'cd \"`cygpath \"$*\"`\";bash' bash %L"

You will need to copy and paste this to a .reg file and import it into your Window's registry. You also need the rxvt package for Cygwin.

Another Cygwin tip I came across the other day that is useful for Ant users is adding the following line to your .bash_rc file:
export ANT_ARGS='-logger org.apache.tools.ant.listener.AnsiColorLogger'

This will add color to the output from Ant running in terminals that support ANSI color codes.

Thursday, March 03, 2005

Translets

At my current client I have been working on and off on a legacy Java web application utilizing a Servlets->XML->XSLT architecture. The users were complaining that the application seemed sluggish. After profiling the application, I found that our bottleneck was in the XML->XSLT transformation process. I started looking around for some XSL performance tips and came across Xalan's XSLTC. XSLTC is a compiler and runtime processor for XSLT. The compiler compiles a stylesheet to a Translet and the runtime processor applies the translet to an XML document to perform the transformation. Translets can be accessed through the JAXP API, so I did not have to modify much of the existing source code.

The bulk of the work was streamlining the use of XSLTC into our build process. There is not an Ant task available and the command line interface is lacking. I implemented some utilities wrapping the XSLTC process, including a good command line interface and dependency checking for stylesheet compilation. The reason for the dependency checking is to prevent from having to compile all approx. 400 stylesheets in the application during each compilation phase. Although there are nearly 400 stylesheets in the app the bulk of the XSL is contained in a common collection of 30-40 stylesheets that are imported by locale and customer specific stylesheets.

Using the XSLTC compiler and runtime processor showed an approx. 3X performance gain in stylesheet transformations. Our previous implementation utilized the JAXP API to create and cache Templates objects for each stylesheet in the app.

Monday, December 13, 2004

How Many Days Ago in Ruby

So Weiqi wondered How Many Days Ago was that. Then Eric posted a Java solution.

Since I have been attempting to learn Ruby, I figured I would attempt to implement a solution in Ruby. I would like to preface this post by stating that I am a Ruby novice and I'm sure there is a better way to do this. Here's the "How Many Days Ago Was That" in Ruby.



require "parsedate"
require "date"

def usage()
puts "Usage: daysago.rb date"
exit 1
end

def computeDaysBetween(from, to)
from, to = to, from if (from > to) # Swaps the two variables
daysAgo = 0
from.upto(to) {|day|daysAgo+=1}
return daysAgo
end

from = Date.parse(ARGV.shift || usage())
puts "It has been #{computeDaysBetween(from, Date.today)} since #{from}"


Thursday, December 09, 2004

Tapestry Presentation from St. Louis JUG

I presented on Tapestry tonight at the St. Louis Java Users Group. Here's a link to the presentation in pdf format.