Java.next -- Four languages that represent the future of Java
Blogger Stuart Halloway has begun a series of posts on trends that point to the future of the Java platform. In his first post, he compares Clojure, Groovy, JRuby, and Scala -- four wildly different languages that nonetheless all play together in the JRE. Find out what unites these languages and what they can tell us about the future of Java-based development ...

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

User interfaces for object-oriented systems, Part 5: Useful stuff

Build an application that puts user-interface principles into practice

No theory is worth anything if it can't be applied effectively, so this month and next, I'll build a small but nontrivial application that demonstrates the object-oriented user-interface principles I've covered over the last few months: an RPN (Reverse Polish Notation) calculator that can work as either a tape calculator or a keypad calculator. (One of the main things I want to demonstrate is the flexibility of the UI; thus the two presentations.) Since this application is a real one, I need to do some groundwork before covering the calculator itself.

Consequently, this month's Java Toolbox takes the name of the column literally and presents the set of generic tools we'll need to build the calculator -- little tools of general utility that should be of use for more than the current application. Specifically, I'll cover the following:

Assert An implementation of an "assertion" that lets you implement pre- and postconditions.
Tester A class that aids in putting together automated unit tests.
Bit_bucket The Java equivalent of /dev/null, it throws away characters.
Std A "singleton" wrapper around standard input and output that makes it easy to access these two streams.
Log A convenient class for logging diagnostic messages.
Align A utility containing various methods for aligning text.
Scrollable_text_area A JTextArea that has scroll bars around it.


Most of those classes are unrelated to one another, and none of them is significant enough to merit a article devoted exclusively to it, so lumping them all together in one place seems sensible. We've taken a quick look at a few of the tools in the past, but a review should prove helpful. Now, on to the tools.

TEXTBOX: TEXTBOX_HEAD: Build user interfaces for object-oriented systems: Read the whole series!

:END_TEXTBOX

Assertions

Bertrand Meyer, the author of the Eiffel language, came up with the notion of pre- and postconditions, commonly called assertions. Eiffel, described in Object-Oriented Software Construction (see Resources) supports those concepts directly within the syntax of the language. Java, unfortunately, provides no such support.

The basic idea of a precondition or postcondition is that every method expects the world (the object that it's working on, the global state of the program, the arguments to the method, and so on) to be in a particular state when the method is called, and that every method will leave the world in a particular state. An assertion is a body of code that guarantees that those conditions hold and typically terminates the program with an exception toss if they do not. Assertions are typically implemented as debug-time tests and removed from the code entirely when the code goes into production. (The removal can be risky since you are modifying tested code, but on the other hand, the assertions can slow down the execution of a program noticeably.)

Resources
The complete Java Toolbox archive