Risk Calculator

December 7th, 2008 Craig Motlin No comments
Categories: software Tags:

Configuration (dot) files

September 7th, 2008 Craig Motlin No comments

These are files I used to configure my command line environment, plus the scripts used to generate these pages. I’m always looking to improve my setup so please email me any useful tips.

Configuration files

Meta

Categories: software Tags:

Ant Timer

September 7th, 2008 Craig Motlin No comments

Download

jar ant-timer.jar Compiled classes
src ant-timer-src.zip Source code

Additional Information

src Subversion repository
javadoc Javadoc
releases Old releases

Five minute tutorial

The Ant Timer is a plug-in that prints out diagnostic messages when ant targets and tasks take longer than the configured amount of time. You can use it with any ant build. Here is some sample output when I use it on my renamer program.

javadocs:
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source files for package com.motlin.base...
[javadoc] Loading source files for package com.motlin.base.log4j...
[javadoc] Loading source files for package com.motlin.renamer...
[javadoc] Loading source files for package com.motlin.renamer.exceptions...
[javadoc] Constructing Javadoc information...
[javadoc] Standard Doclet version 1.6.0_06
[javadoc] Building tree for all the packages and classes...
[javadoc] C:\cygwin\home\Administrator\dev\renamer\trunk\src\com\motlin\base\log4j\StdOutErrAppender.java:318: warning - Tag @link: reference not found: org.apache.log4j.net.SocketAppender
[javadoc] Building index for all the packages and classes...
[javadoc] Building index for all classes...
[javadoc] Generating C:\cygwin\home\Administrator\dev\renamer\trunk\dist\api\stylesheet.css...
[javadoc] 1 warning
XXX task   [renamer.javadocs.javadoc] took 2 seconds to run
XXX task   [renamer.null.sequential] took 2 seconds to run
XXX task   [renamer.javadocs.ac:outofdate] took 2 seconds to run
XXX target [renamer.javadocs] took 2 seconds to run

BUILD SUCCESSFUL
Total time: 2 seconds

Running ant with the custom listener is pretty simple. Here’s the command that produced the output above.

ant -listener com.motlin.ant.timer.TimerListener -lib ant-timer-1.0.jar -lib lib/joda-time-1.5.2.jar -Dtarget.timer=2 -Dtask.timer=1 -Dtimer.prefix="XXX" clean javadocs

Let’s break it down.

-listener com.motlin.ant.timer.TimerListener

This is the class name of the custom task.

-lib ant-timer-1.0.jar -lib lib/joda-time-1.5.2.jar

Unfortunately, the Ant Timer depends on joda time. It didn’t have to but I like it better than using the built in classes for working with times. You’ll need to download the joda jar separately.

-Dtarget.timer=2 -Dtask.timer=1 -Dtimer.prefix="XXX"

You can configure the behavior through these properties. The first two are the timeouts in seconds for targets and tasks. If you leave them off they default to 10 seconds and 1 second respectively. The prefix makes it easy to find the output printed by the custom listener. If you leave it off it defaults to “!!!”

Categories: software Tags:

Universal toString()

September 6th, 2008 Craig Motlin No comments

Download

jar UniversalToString.jar Compiled classes
src UniversalToString-src.zip Source code

Additional Information

src Subversion repository
javadoc Javadoc
releases Old releases

Five minute tutorial

UniversalToString is a java class that prints out the internal state of an object using reflection. The UniversalToString class builds a String by recursively traversing the contents of an object and printing out the names, types, and values of all of its fields. It is loosely based on some code in Core Java 2, Volume I – Fundamentals. The simplest way to use the UniversalToString class is in the implementation of your own classes’ toString methods. Say we have a simple Person class:

public class Person {
    private final String first;
    private final String last;
    public Person(final String first, final String last) {
        this.first = first;
        this.last = last;
    }
}

Person does not implement the toString method, so when it is printed to standard out, Object.toString is used instead. The generated String contains the type name and the address in memory where the object lives. Normally this is not very useful information.

Person john = new Person(“John”, “Smith”);
System.out.println(john);

Console output:

tostring.Person@19821f

To improve the output, we can override the toString method in the Person class.

@Override
public String toString() {
    return “Person: first [" + this.first + "] last [" + this.last + "];
}
Person: first [John] last [Smith]

Implementing the toString method like this is tedious, repetitive, and fragile. That’s where UniversalToString comes in.

@Override
public String toString() {
    return UniversalToString.print(this);
}
Person(first=John,last=Smith)

Collections

UniversalToString has special logic for arrays, collections, and maps. This example uses a list, but the output is similar for each.

Person john = new Person(“John”, “Smith”);
Person jane = new Person(“Jane”, “Doe”);
List<Person> list = new ArrayList<Person>();
list.add(john);
list.add(jane);
System.out.println(UniversalToString.print(list));
ArrayList{Person(first=John,last=Smith), Person(first=Jane,last=Doe)}

Custom formatting

Unfortunately our work isn’t done. The String created by UniversalToString isn’t particularly informative for certain objects. For example, let’s change the last example to print one Person and one Date:

Person john = new Person(“John”, “Smith”);
Date date = new Date(1206552944822l);
List<Object> list = new ArrayList<Object>();
list.add(john);
list.add(date);
System.out.println(UniversalToString.print(list));
ArrayList{Person(first=John,last=Smith), Date(fastTime=1206552944822,cdate=null)}

We can customize the formatting of UniversalToString by creating an implementation of TypedToString that handles Date objects.

public class DateToString implements TypedToString {
    private final DateFormat dateFormat;
    public DateToString() {
        this.dateFormat = new SimpleDateFormat();
        this.dateFormat.setTimeZone(TimeZone.getTimeZone(“America/New_York”));
    }
    public boolean handlesType(final Object object) {
        return object instanceof Date;
    }
    public String print(final Object object) {
        return this.dateFormat.format((Date) object);
    }
}

Now we just need to change the line that prints the list.

System.out.println(UniversalToString.print(list, new DateToString()));
ArrayList{Person(first=John,last=Smith), 3/26/08 1:35 PM}

The second argument to print is varargs TypedToString, so we can pass in as many special formatting rules as we need.

Categories: software Tags: