Having a Java generic class return a type reference to its specialized class. Aka, using the self-type.

Emulating "self types" using Java Generics to simplify fluent API implementation is a very good example of how to more effectively use Java's generics and inheritance. The "self type" solution is something I would not have imagined. Further, it is motivating to see what other effects can be achieved through Java's generics. Read the article for a compete understanding of self types and a use of them. For those wishing to skip to the conclusion, if you need a generic class instance to return a typed reference to the specialized class then it is coded
public class GenericsTest {
// We have a generic type here that needs to return from its check method
// a reference to the specialized type (that is, the generic's sub-class).
static class Common<SelfType extends Common<SelfType,ElementType>,ElementType> {

protected SelfType self() {
return (SelfType) this;

public SelfType check( ElementType e ) {
// do something here that is general to all sub-classes
return self();

// ...

static class Foo extends Common<Foo,Integer> {

public Foo doFoo() {
// do something here is is specific to Foos
return this;

static class Bar extends Common<Bar,String> {
public Bar doBar() {
// do something here is is specific to Bars
return this;

public static void main( String[] args ) throws Exception {
Foo foo = new Foo();
Bar bar = new Bar();

// END

Tools needed to support a simple content organization and workflow

I like to use the file system. And I like to use the file system's default explorer: On the Mac this is the Finder, on Windows this is the Windows Explorer, and on Gnome it is/was Nautilus. I much prefer to have other tools integrate into the file system explorer than for the tool to have its own explorer. When this happens, it is then possible to create your own tools that support your workflows. (Note that in this posting I am concerned with explorer integration and not command line integration.)

Unfortunately, this has not been the norm and so we have iPhoto, iTunes, etc, having there own content organization that is independent of the file system. These content specific explorers are often created due to the weaknesses in extending the system's explorer but mostly it is because creating another explorer is just easier to both imagine and code. The up-shot is that we have organizational silos with little integration between them. And, worse, the inability to create your own specialized tools from the integrations.

This Sunday I spent far too much time tracking down tools that would allow me to use the file system to collect and organize content as follows:

1. Each content collection is a folder. I am comfortable with using the file system hierarchy for organizing and I am aware of the weaknesses of hierarchical organization. However, most collections can be organized into a primary hierarchy and augmented either by search or linking (aliases on the Mac and shortcuts on Windows).

2. Each folder will have, at least, one plain text file of dated notes. Each note is separated by a simple delimiter, for example, a row of dashes. My experience is that this one file of notes is much easier to review and keep updated than to place notes into individual files. With that said, a good tool that can hide the file-ness of the notes would be acceptable too. Until then, one file of notes and using Merlin Man's tip for adding content to it <http://bit.ly/a1kfV6> will do fine.

3. The primary hierarchy needs to be supplemented with full text search. Tagging is not enough and, moreover, tagging can be achieved via search by using specialized tokens: For example, in normal English typography a colon is always followed by a space and so the expression ":foo" distinguishes the tag "foo" from the rest of the text. Spotlight on the Mac is the system's full text searching facility. It is extendable so that other content types can be incorporated. The plugin I am looking for would index the dated notes in a file as collection of "records". (This Spotlight feature was introduced in OS X 10.6.)

The Spotlight plugin is the most specific tool that I need. While it is a Mac only indexing solution it is based on plain text content and so could be indexed easily under other operating systems. Unfortunately, as far as I can tell, and I spent a good amount of time looking, there is no plugin for indexing delimited content as records. There are plugins for content types that somewhat match, for example mailboxes, but there is no documentation on how to use them outside of their intended application. The up-shot that that for this very simple workflow I can not compose a supporting tool from existing parts. Looks like I am going to have to write my own Spotlight plugin: If anyone has skeleton code I can build upon please drop me a note.

Cat & tail

I needed to count the open and close events in an active log file. Since I needed to start the count at zero I needed to start counting at the first line of the log and continue as the log grew. I needed both cat and tail -f. Turns out that tail can do both using tail -c +0 -f events.log. And so all I needed to do was to pipe the output to the counting script. The script took several minutes to catch up from processing previous events to current events but the counts it displays are accurate.

Using gnuplot to plot a time-series in text

I needed to review a sizable time-series on a remote host. Since I work at the command-line here having the plot displayed in text would be very helpful. To do this I used gnuplot. The data has the form
2010-11-06T00:00:01  35
2010-11-06T00:05:01 38
2010-11-06T00:10:02 45
2010-11-06T00:15:01 38
2010-11-06T00:20:02 38
2010-11-06T00:25:01 38
Note the two spaces between the data columns. The command line & script to plot this data is
gnuplot <<EOH
set terminal dumb
set autoscale
set xdata time
set timefmt "%Y-%m-%dT%H:%M:%S"
set xlabel "Date & Time"
set ylabel "Count"
set title "Requests waiting on connection"
plot "data" using 1:2 with lines
which outputs

Requests waiting on connection
160 ++--+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+-++
+ + + + + + + "data" using 1:2 ****** +
| ***** |
140 ++ ****** * ++
| ** * |
120 ++ ** * ++
| * * |
| * * * |
100 ++ * * * ++
| *** * |
80 ++ *** * ++
| * ** * |
| * ** * |
60 ++ **** * ++
| * ****** * |
40 +*************** * *************************** ++
*** ******** |
+ + + + + + + + + + +
20 ++--+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+-++
00:00 02:00 04:00 06:00 08:00 10:00 12:00 14:00 16:00 18:00 20:00
Date & Time
Must easier to see the trend in the data.

I found the following helpful in figuring this out http://linuxgazette.net/126/peterson.html.

Update: Use the watch command to turn the graph generation into a dynamic visualization. For example watch -n 60 bash plotting_script will run the plotting_script every minute and update the terminal.

Update: See the bash script timeplot for a useful expression of this posting.

Vi and a little macro to step through a file of data

I needed to review a large number of data in a file each of which had a variable number of lines. Luckily, each datum started with the pattern ^"http- and so using vi I was able to quickly review the data by

1) creating a macro to find the next datum and move it's line to the top of the screen
2) use the macro to move from datum to datum
I found the following helpful in figuring this out http://www.pixelbeat.org/vim.tips.html.