Author Archive

RPN Calc Part 3 – Undo

Posted Nov 19 2013 by with 0 Comments

One of the reasons given in the GoF book for the use of the command pattern is to support undo. By recording the commands executed by a user, and giving the commands the ability to reverse themselves, a user interface can be designed to allow users to undo mistakes they make. For this post, I’ll talk about how this is done in the undoable version of rpncalc.

Continue Reading…

RPN Calc Part 2 – Composite Commands

Posted Oct 28 2013 by with 1 Comment

If you’ve played around with the basic version of the Java calculator, you may have tried to enter multiple commands on the same prompt line:

Begin: class com.ksmpartners.rpncalc.basic.RpnCalc

> 1 2
Uncaught Exception: For input string: "1 2"
java.lang.NumberFormatException: For input string: "1 2"

This is a convenient way to enter calculator commands, but it doesn’t work for two reasons. This post discusses why it fails, and how to fix it:

Continue Reading…

RPN Calc Part 1 – A simple calculator in Java, and the Command Pattern

Posted Oct 21 2013 by with 0 Comments

The first implementation of the RPN calculator I’m going to look at is the basic Java implementation. The link takes you directly a a view of the source file for this version, but if you’d like to play with the code, you can also clone the whole project with git: https://github.com/ksmpartners/blog-rpncalc. Cloning the git project will bring down all the versions of the calculator, and also let you compile and run the software locally. I’ll discuss more about how the implementation works, after the jump:

Continue Reading…

Middle School Math, Reverse Polish Notation, and Software Design

One of the things I remember most about middle school math class is that I went through it in a perpetual state of disorganization. During one particularly bad spell, I lost two calculators within a week. The loss, and the reaction of my parents, drove me to try to fix the problem once and for all. My plan was simple: buy an expensive calculator with the hope that it’d serve as an incentive to keep track of my stuff. The next weekend, I took weeks of allowance money to the local Service Merchandise and bought a new HP-11C pocket calculator. Almost 30 years later, I still have both the calculator and a fascination for its unusual Reverse Polish Notation (RPN) user interface. Over those decades, I’ve also found out that RPN provides a good way to explore a number of fundamental ideas in the field of software design.

Continue Reading…

Logging the Boundaries of TestNG Test Methods with Log Messages

Posted Sep 5 2013 by in Java with 0 Comments

For a variety of reasons (some good, some bad) I tend to fall into the school of developers that debugs primarily with log messages and printf statements. (What can I say… It works for me.) As part of this style of working, I’ve developed a quick mechanism for instrumenting TestNG to print a log message at the beginning and ending of each test method that it executes. This makes it easier to make sense of the other log messages that get issued during a test cycle.

Continue Reading…

Clojure Closures In Java

As part of a team conversation this morning, I worked up a quick Java translation of some more-interesting-than-it-looks Clojure code. It’s a good example of how lexical closures map to objects.

The code we started out with was this:

(defn make-adder [x]
  (let [y x]
    (fn [z] (+ y z))))

(def add2 (make-adder 2))

(add2 4)

What this code does is define and return a new type of function that adds values to the constant x. In Java, it looks like this: Continue Reading…

Nicely Formatted Tabular Output in Java

Posted Aug 6 2013 by with 0 Comments

Occasionally, it’s useful to be able to print nicely formatted tables of data to a textual output stream. This is particularly the case when writing command line tools. To make the output of these tools more readable, any tables they write should have columns that line-up from row to row. The Unix tool ‘ls’ does this when it prints out long form directory listings. In this example, notice how the dates line up, even though the file size column varies in width.

drwxr-xr-x+ 1 mschaef Domain Users        0 Oct  3 09:20 docs
-rwxr-xr-x  1 mschaef Domain Users 29109013 Oct 10 13:38 file.zip
-rwxr-xr-x  1 mschaef Domain Users    77500 Oct 10 13:17 file2.zip

Continue Reading…

Why is Traditional Java I/O Uninterruptable?

Posted Jul 22 2013 by in Java with 0 Comments

One of the best things about writing code in the Java ecosystem is that so much of the underlying platform is open source. This makes it easy to get good answers to questions about how the platform actually works. To illustrate, I’ll walk through the JVM code to show why Java I/O isn’t interruptable. This will explain why threads performing Java I/O can’t be interrupted.

The core issue with interrupting a Java thread performing I/O is that the underlying system call is uninterruptable. Let’s see why that is, for a FileInputStream. Continue Reading…

Custom EHCache Sizing Engines

Posted Jul 10 2013 by in Java with 0 Comments

One of the ways that EHCache can be configured is to put a bound on the maximum amount of memory a cache can consume. To do this, EHCache needs to be able to compute the size of the cache, which implies that it can compute the size of object graphs contained within the cache. The usual way it does this is to traverse the object graph of each key/value pair in the cache, estimate the size of each component object within the graph, and then add them all up to get a total for the key/value pair.  By doing this for each cached key/value pair, EHCache can estimate the overall memory footprint of a cache and enforce a memory bound by evicting objects, if necessary. Continue Reading…

Thread Local State and its Interaction with Thread Pools

Posted Jul 2 2013 by in Java with 0 Comments

I recently blogged about solving problems with thread local storage. In that instance, thread local storage was a way to add a form of ‘after the fact’ synchronization to an interface that was initially designed to be called from a single thread.  Thread local storage is useful for this purpose, because it allows each thread to easily isolate the data it manipulates from other threads. Unfortunately, while this is the strength of Thread Local storage, it is also the weakness. In modern multi-threaded designs, threading issues are often abstracted behind thread pools and work queues. With these abstractions at work, the threads themselves become an implementation detail, and thread local variables are too low level to serve some useful scenarios.

One way to address this issue is to use some of the techniques from an earlier post on dynamic extent. The general gist of the idea is to provide Runnable‘s with way of reconstructing relevant thread local state at they time they are invoked on a pool thread. This maps well to the idea of ‘dynamic extent’, as presented in the earlier post. ‘Establishing the precondition’ is initializing the thread locals for the run, and ‘Establishing the post condition’ is restoring their original values. Here’s how it might look in code: Continue Reading…