Michael Angstadt's Homepage
programmer, teacher, artist, frontiersman


How this page works

More blog entries >>

05/15/2014 12:25 pm
Git is a version control system that has gained a lot of popularity over the past few years. It started out as a custom-designed VCS (version control system) for the Linux kernel and has since ballooned in popularity, arguably thanks to Github, a free source code hosting site that is powered by Git. Below are five facts about Git that you might not know:

1. Stashing

This command saves all the uncommitted modifications you've made to your working copy, and then reverts your working copy back to its original state. This is useful if you want to switch branches, but your changes are in an incomplete state and you don't want to commit anything yet (git will not allow you to switch branches if you have uncommitted changes).

The command to run a stash operation is git stash. When you're ready to re-apply your stashed changes, run git stash apply.

You can also create multiple stashes. To see a list of all stashes, run git stash list. By default, when you run git stash apply, it will apply the stash at the top of the list. To apply a different stash, run git stash apply stash@{2} where stash@{2} is the name of the stash as shown in the stash list.

Also note that when you apply a stash, it will remain in the stash list. To delete a stash, run git stash drop. Or, you can run git stash pop to apply a stash and then delete it.

Stashes are only stored in your local repository. They cannot be pushed to remote repositories.

For more information, see: http://git-scm.com/book/en/Git-Tools-Stashing

2. Amending commits

With most other version control systems, if you forgot to include a file in a commit, you have to make a second commit. This is annoying because it makes the commit history longer than it should be. With Git, instead of making a second commit, you can "amend" the previous commit. This will merge your commit in with the previous one.

> git commit --amend

For more information, see: http://git-scm.com/book/en/Git-Basics-Undoing-Things

3. Git is a file system

At its core, Git is actually a key/value data store. The commands that you use on a daily basis, like push and commit, are tools that are built on top of the data store. To demonstrate, I'll show you how to add and retrieve files from a Git repository, without using any of the typical Git commands.

First, initialize an empty repository:

> git init
Initialized empty Git repository in /home/michael/git-blog/.git/

Next, add a file to the repository. This command will return a SHA-1 hash, which we will need to retrieve the file again.

> echo 'file data' | git hash-object -w --stdin 

Then, retrieve the file like so:

> git cat-file -p 987721052266a93a2509c3a8ac9e8c86341d0835
file data

For more information, see: http://git-scm.com/book/en/Git-Internals-Git-Objects

4. Commit message templates

If you work in a corporate environment, your team might have a policy on how commit messages have to be formatted. Git allows you to define a file that contains the default commit message to use for all commits.

> git config --global commit.template path/to/commit-message.txt

For more information, see: http://git-scm.com/book/en/Customizing-Git-Git-Configuration

5. Ignoring files during export

Git includes a command that lets you easily generate an archive file (.zip, .tar, etc) of your project. By default, all files in your project are included, but there may be some files which you want to exclude. To do this, use the export-ignore attribute:

> echo "test/ export-ignore" > .gitattributes
> git add .gitattributes
> git commit -m "Added .gitattributes file."
> git archive -o latest.zip HEAD

For more information, see: http://git-scm.com/book/en/Customizing-Git-Git-Attributes

05/01/2014 2:53 pm

Writing GUI applications can be difficult. It's easier to understand the flow of a command-line program--you start at the top and go to the bottom. But the flow of a GUI application, with its listeners, event handlers, and callbacks, goes all over the place. Add database queries and network calls to the mix, and things get even more complicated.

Enter MVP

The Model-View-Presenter (MVP) pattern helps to manage this complexity. MVP belongs to that family of design patterns that separates the application data and logic from the way in which the information is displayed to the user. To summarize:

  • The Model is responsible for maintaining the application's raw data (typically by persisting it in a database).
  • The View is responsible for presenting the data to the user (for example, in the form of a webpage or dialog box).
  • And the Presenter is responsible for tying the model and view together. In MVP, the model and view know nothing of each other!

Presenter First

The idea with "Presenter First" is that, using the MVP pattern, you start by writing the presenter class before anything else. This forces you to think abstractly about how your dialog window is going to behave. And, in the process of writing the presenter, you naturally figure out what functionalities the model and view will need to support. So, writing the model and view becomes just a matter of implementing an interface.

Another benefit to Presenter First is that it allows you to unit test your dialog's application logic. This is because the model and view are represented as interfaces, which can be easily mocked-out in the unit tests.

To summarize, the three benefits of Presenter First are:

  1. By using MVP, the view is cleanly separated from the application data and logic. In other words, your JFrame and JDialog classes become truly "dumb"--they contain no database calls or application logic.
  2. In the process of writing the presenter, the APIs for the model and view are essentially written automatically.
  3. The application logic of your dialog is finally unit-testable!


As an example, let's create a simple login dialog. This dialog will ask the user for a username and password. If the credentials are valid, then a session token will be returned and the dialog will close. The user can also choose to have the application remember his username and password.

We start by writing the presenter class.


import java.awt.event.*;

public class LoginPresenter{
    private final ILoginView view;
    private final ILoginModel model;

    public LoginPresenter(ILoginView view, ILoginModel model){
        this.view = view;
        this.model = model;

        //invoked when the user clicks "Login"
        view.addLoginListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){

        //invoked when the user clicks "Cancel" or closes the window
        view.addCancelListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){

        //populate the dialog with its initial data

        //finally, display the dialog

    private void onLogin(){
        //get the data that the user entered
        String username = view.getUsername();
        String password = view.getPassword();
        boolean rememberMe = view.getRememberMe();

        //send the network call to log the user in
        String session = model.login(username, password);

        if (session == null){
            //credentials were bad, so show an error dialog to the user

        //persist the login credentials if "remember me" is checked
        if (rememberMe){
        } else {



    private void onCancel(){

The constructor adds event handlers which will fire with the user presses the "Login" and "Cancel" buttons. Then, it populates the view with data from the model (in this case, the saved username and password). The "onLogin()" method contains logic which determines if the login was successful or not and acts accordingly.

Now that our presenter is written, we can write the model and view interfaces, which allows the presenter class to compile.


public interface ILoginModel{
    String login(String username, String password);

    String getCachedUsername();
    void setCachedUsername(String username);
    String getCachedPassword();
    void setCachedPassword(String password);
    boolean getCachedRememberMe();
    void setCachedRememberMe(boolean rememberMe);
    String getSession();
    void setSession(String session);


import java.awt.event.*;

public interface ILoginView{
    void addLoginListener(ActionListener listener);
    void addCancelListener(ActionListener listener);

    String getUsername();
    void setUsername(String username);
    String getPassword();
    void setPassword(String password);
    boolean getRememberMe();
    void setRememberMe(boolean rememberMe);

    void onBadLogin();
    void onSuccessfulLogin();

    void display();
    void close();

Next, we write our tests! Using a stubbing framework like Mockito helps, but it's not required (you could always create your own test implementations of the model and view interfaces).


import java.awt.event.*;
import java.util.*;
import org.junit.*;
import org.mockito.invocation.*;
import org.mockito.stubbing.*;
import static org.mockito.Mockito.*;

public class LoginPresenterTest{
    public void init(){
        ILoginView view = mock(ILoginView.class);

        ILoginModel model = mock(ILoginModel.class);

        LoginPresenter presenter = new LoginPresenter(view, model);


    public void bad_login(){
        ILoginView view = mock(ILoginView.class);
        ListenerAnswer loginAnswer = new ListenerAnswer();

        ILoginModel model = mock(ILoginModel.class);
        when(model.login("user", "password")).thenReturn(null); //"null" = bad login

        LoginPresenter presenter = new LoginPresenter(view, model);

        //click "login"

        verify(model, never()).setSession(anyString());
        verify(view, never()).onSuccessfulLogin();
        verify(view, never()).close();

    public void valid_login(){
        ILoginView view = mock(ILoginView.class);
        ListenerAnswer loginAnswer = new ListenerAnswer();

        ILoginModel model = mock(ILoginModel.class);
        when(model.login("user", "password")).thenReturn("abc123"); //non-null token = good login

        LoginPresenter presenter = new LoginPresenter(view, model);

        //click "login"

        verify(view, never()).onBadLogin();

    public void rememberMe_true(){
        ILoginView view = mock(ILoginView.class);
        ListenerAnswer loginAnswer = new ListenerAnswer();

        ILoginModel model = mock(ILoginModel.class);
        when(model.login("user", "password")).thenReturn("abc123");

        LoginPresenter presenter = new LoginPresenter(view, model);

        //click "login"


    public void rememberMe_false(){
        ILoginView view = mock(ILoginView.class);
        ListenerAnswer loginAnswer = new ListenerAnswer();

        ILoginModel model = mock(ILoginModel.class);
        when(model.login("user", "password")).thenReturn("abc123");

        LoginPresenter presenter = new LoginPresenter(view, model);

        //click "login"


    public void cancel(){
        ILoginView view = mock(ILoginView.class);
        ListenerAnswer cancelAnswer = new ListenerAnswer();

        ILoginModel model = mock(ILoginModel.class);

        LoginPresenter presenter = new LoginPresenter(view, model);

        //click "cancel"

        verify(model, never()).setSession(anyString());

    private class ListenerAnswer implements Answer<Object>{
        private final List<ActionListener> listeners = new ArrayList<ActionListener>();

        public Object answer(InvocationOnMock invocation) {
            ActionListener listener = (ActionListener)invocation.getArguments()[0];
            return null;

        public void fire(){
            for (ActionListener listener : listeners){

Once our tests pass, we can write the real implementations of the model and view interfaces. Again, this is basically just a matter of creating a new class and having that class implement the interface.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.SpringLayout.*;

public class LoginViewImpl extends JFrame implements ILoginView {
    private final JButton login, cancel;
    private final JTextField username;
    private final JPasswordField password;
    private final JCheckBox rememberMe;

    public LoginViewImpl() {

        login = new JButton("Login");
        cancel = new JButton("Cancel");
        username = new JTextField();
        password = new JPasswordField();
        rememberMe = new JCheckBox("Remember me");

        JLabel title = new JLabel("Please enter your login credentials.");
        JLabel usernameLabel = new JLabel("Username:");
        JLabel passwordLabel = new JLabel("Password:");


        Container contentPane = getContentPane();
        SpringLayout layout = new SpringLayout();


        layout.putConstraint(WEST, title, 5, WEST, contentPane);
        layout.putConstraint(NORTH, title, 5, NORTH, contentPane);

        layout.putConstraint(WEST, usernameLabel, 5, WEST, contentPane);
        layout.putConstraint(NORTH, usernameLabel, 10, SOUTH, title);
        layout.putConstraint(WEST, username, 10, EAST, usernameLabel);
        layout.putConstraint(NORTH, username, 0, NORTH, usernameLabel);
        layout.putConstraint(EAST, username, 100, WEST, username);

        layout.putConstraint(WEST, passwordLabel, 5, WEST, contentPane);
        layout.putConstraint(NORTH, passwordLabel, 5, SOUTH, usernameLabel);
        layout.putConstraint(WEST, password, 0, WEST, username);
        layout.putConstraint(NORTH, password, 0, NORTH, passwordLabel);
        layout.putConstraint(EAST, password, 100, WEST, password);

        layout.putConstraint(WEST, rememberMe, 5, WEST, contentPane);
        layout.putConstraint(NORTH, rememberMe, 5, SOUTH, passwordLabel);

        layout.putConstraint(WEST, login, 5, WEST, contentPane);
        layout.putConstraint(NORTH, login, 10, SOUTH, rememberMe);
        layout.putConstraint(WEST, cancel, 5, EAST, login);
        layout.putConstraint(NORTH, cancel, 0, NORTH, login);


    public void addLoginListener(ActionListener listener) {

    public void addCancelListener(final ActionListener listener) {
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent event){

    public String getUsername() {
        return username.getText();

    public void setUsername(String username) {

    public String getPassword() {
        return new String(password.getPassword());

    public void setPassword(String password){

    public boolean getRememberMe() {
        return rememberMe.isSelected();

    public void setRememberMe(boolean rememberMe) {

    public void onBadLogin() {
        JOptionPane.showMessageDialog(this, "Invalid login credentials.");

    public void onSuccessfulLogin() {
        JOptionPane.showMessageDialog(this, "Login successful.");

    public void display() {

    public void close() {


import java.io.*;
import java.util.*;

public class LoginModelImpl implements ILoginModel{
    private final File file;
    private final Properties properties;
    private String session;

    public LoginModelImpl(File file) throws IOException{
        this.file = file;
        this.properties = new Properties();

        if (file.exists()){
            this.properties.load(new FileReader(file));

    public String login(String username, String password){
        //normally, a network or database call would be made here
        if ("test".equals(username) && "test".equals(password)){
            return "abc123";
        return null;

    public String getCachedUsername(){
        return properties.getProperty("username");

    public void setCachedUsername(String username){
        properties.setProperty("username", username);

    public String getCachedPassword(){
        return properties.getProperty("password");

    public void setCachedPassword(String password){
        properties.setProperty("password", password);

    public boolean getCachedRememberMe(){
        String value = properties.getProperty("rememberMe");
        return (value == null) ? false : Boolean.parseBoolean(value);

    public void setCachedRememberMe(boolean rememberMe){
        properties.setProperty("rememberMe", rememberMe + "");

    public String getSession(){
        return session;

    public void setSession(String session){
        this.session = session;

    private void save() {
        try {
            properties.store(new FileWriter(file), "");
        } catch (IOException e){
            throw new RuntimeException(e);

To run our program, we simply create a new instance of LoginPresenter, passing in the model and view implementations that we created above.


import java.io.*;

public class Main{
    public static void main(String args[]) throws Throwable {
        File cache = new File("cache.properties");

        ILoginModel model = new LoginModelImpl(cache);
        ILoginView view = new LoginViewImpl();
        new LoginPresenter(view, model);

And that's all there is to it!

Download the source code


09/07/2013 8:19 pm

Often times, when writing Javadocs, it helps to include source code samples along with the documentation. Typically, this is achieved by inserting the source code into a <pre> tag. This will render the code in a monospaced font when viewed in a browser.

 * <p>Represents a fruit.</p>
 * <pre>
 * //create a new fruit
 * Fruit fruit = new Fruit("banana");
 * //copy an existing fruit
 * Fruit copy = new Fruit(fruit);
 * </pre>
 * @author John Doe
public class Fruit{

But there tools out there that can add syntax highlighting to source code on a web page. Javadocs are a webpage. Why can't these syntax highlighting tools be applied to Javadocs as well?

In this blog post, I am going to show you how to add syntax highlighting to a Maven-enabled Java project. I will be using the popular Javascript-based SyntaxHighlighter library for the syntax highlighting.

1. Download SyntaxHighligher

First, download SyntaxHighlighter.

2. Create a CSS file

SyntaxHighlighter makes use of CSS styling to perform the code coloring. Luckily, the Javadoc tool allows you to specify a CSS file to customize the look and feel of your Javadoc webpage. So, we will need to create a CSS file that contains the styling that SyntaxHighlighter requires.

Navigate to the SyntaxHighlighter files that you downloaded in the previous step. In the "styles" directory, locate the "shCore.css" file and one of the "shTheme" files (such as "shThemeDefault.css", see: all the available themes). Combine these two files into a single file and give it a name of your choosing. Save this file somewhere within your project folder. The location doesn't matter, since Javadoc will end up copying the file when the Javadocs are generated. A good place is the "src/main/javadoc" folder, as this is the standard Maven location for all Javadoc-related resources.

3. Configure your POM file

Next, we will need to add some configuration settings to the project POM. In the configuration section of the "maven-javadoc-plugin" plugin, add the following: (1) the location of the CSS file that was created in the previous step, (2) <script> tags for the SyntaxHighlighter Javascript files, and (3) Javascript code to configure and initialize SyntaxHighlighter.


    <!-- (1) CSS file location -->

    <!-- (2) SyntaxHighlighter Javascript files -->
      <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript"></script>
      <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js" type="text/javascript"></script>

    (3) SyntaxHighlighter configuration and initialization
    See: http://alexgorbatchev.com/SyntaxHighlighter/manual/configuration/
      <script type="text/javascript">
        SyntaxHighlighter.defaults["auto-links"] = false;
        SyntaxHighlighter.defaults["tab-size"] = 2;


A list of available SyntaxHighlighter configuration settings can be found on the SyntaxHighlighter homepage.

4. Modify the Javadocs

Each <pre> tag in the Javadocs must be given a class="brush:java" attribute. This signals to SyntaxHighlighter that the text content should be treated as Java source code.

 * <p>Represents a fruit.</p>
 * <pre class="brush:java">
 * //create a new fruit
 * Fruit fruit = new Fruit("banana");
 * //copy an existing fruit
 * Fruit copy = new Fruit(fruit);
 * </pre>
 * @author John Doe
public class Fruit{

5. Generate the Javadocs

Instruct Maven to generate the Javadocs for the project by running the following command:

mvn javadoc:javadoc

And that's it! You should be good to go.


07/17/2013 5:33 pm

I just finished listening to an ACM webcast entitled "Changing How Programmers Think about Parallel Programming". It was presented by William Gropp from the University of Illinois at Urbana-Champaign and it was very informative!

He discussed two different types of parallel programming. Course grained parallelism is where you divide a task up into chunks and each process performs the same sequence of operations on its assigned chunk. For example, say you have to mail some letters and have multiple people at your disposal to help you. With course grained parallelism, each person would be given some number of letters to mail and would be responsible for all tasks that are involved with mailing the letter, such as folding the paper, placing it in the envelope, and applying a stamp.

Fine grained parallelism, however, is where you divide the task up by operation and each process is assigned to a specific operation. Using the letter example, this would mean that one person would be responsible for folding the paper, another for placing the paper in the envelope, and so on.

He discussed the fact that processes must often share data with each other. One technique for doing this is for each process to copy some of the data that is assigned to other processes before execution begins. He also mentioned that parallel programs are harder to debug than traditional, single-threaded programs, which is one reason why programming in a parallel fashion is so difficult!

06/20/2013 2:54 pm

Timezones are confusing to say the least. Here are four things to keep in mind when working with timezones in Java.

1. Timezones are relative to UTC

UTC, which stands for "Coordinated Universal Time", is always the same no matter where on the planet you are. The term "GMT" is often used as well. While GMT is pretty much identical to UTC, it's not exactly the same. UTC is scientifically defined, while GMT is not.

A timezone is an offset from UTC. For example, my timezone is currently 4 hours behind UTC. However, a timezone's offset may change depending on the time of year. For example, my timezone is 4 hours behind UTC for half the year (daylight savings time) and 5 hours behind UTC for the other half of the year (standard time). This is why it's preferable to represent timezones using their IDs (such as "America/New_York", described in more detail below) instead of their offsets (such as "-0400"). An offset can change depending on the time of year, but a timezone ID encapsulates these offset variations.

Fun fact: You might have noticed that the letters in the acronym "UTC" don't match up with "Coordinated Universal Time". "UTC" actually arose as a compromise between the English version "CUT" (Coordinated Universal Time), and the French version "TUC" (Temps Universel Coordonné).

2. Date objects use UTC

Whenever you call the toString() method on a java.util.Date object (for example, by printing the object to the console), it will generate a string that looks something like this:

Thu Jun 20 13:23:52 EDT 2013

This might lead you to believe that this is the exact timestamp that the object is holding. Not necessarily. Internally, Date objects store their timestamps in UTC. When you call toString(), it converts the UTC timestamp to your JVM's default timezone.

3. Timezones are clothing

Think of timezones as just different sets of clothing a timestamp can wear. It can put on a sweater, a jacket, or a tuxedo, but underneath it all, it's still a UTC timestamp. To format a Date object using a timezone of your choosing, call the setTimeZone() method on the DateFormat class.

DateFormat formatter = new SimpleDateFormat("HH:mm Z");
TimeZone timezone = TimeZone.getTimeZone("Europe/Madrid");
System.out.println(formatter.format(new Date()));
//prints: 19:50 +0200

In the example above, I'm printing the current time in Madrid, Spain (and surrounding areas). The "+0200" part describes the timezone's offset from UTC at that moment in time. It shows that the timezone is 2 hours and 0 minutes ahead of UTC. To give another example of an offset, "-0430" would mean that the timezone is 4 hours and 30 minutes behind UTC.

You might be wondering where a list of these timezone identifier strings can be found. A source that I like to use is from the PHP user manual. But the official listing can be found in what's known as the TZ database.

4. The TimeZone class is quirky

Note that if an unrecognized timezone ID is passed into the TimeZone.getTimeZone() method, the method will return an object representing the "GMT" timezone. It would be less confusing if it just returned null, but who am I to complain.

TimeZone timezone = TimeZone.getTimeZone("Bogus/Timezone");
if ("GMT".equals(timezone.getID())){
  //timezone not found!
} else {
  //timezone found
More blog entries >>

How this page works

Last Updated: 1/3/2012

My blog is actually hosted on blogger.com. The way I'm able to display my blog posts here is by parsing the blog's RSS feed. RSS feeds are used by blogs to help alert their avid readers whenever a new post is created. They are just XML files that contain data on the most recent blog posts. They include things like the title and publish date of each post, as well as the actual blog post text. I can use most of the data from my RSS feed without any trouble, but there are a few things I need to tweak in order to display everything properly.

View the source

Fixing the code samples

One tweak is fixing the code samples I often include in my posts. Blogger replaces all newlines in the blog post with <br /> tags. This is a problem because, due to the syntax highlighting library I use, the <br /> tags themselves show up in the code samples. So, I need to replace all of these tags with newline characters. However, I can't just replace all <br /> tags in the entire blog post because I only want to replace the tags that are within code samples. This means that I have to use something a little more complex than a simple search-and-replace operation:

$content = //the blog post
$contentFixed = preg_replace_callback('~(<pre\\s+class="brush:.*?">)(.*?)(</pre>)~', function($matches){
	$code = $matches[2];
	$code = str_replace('<br />', "\n", $code);
	return $matches[1] . $code . $matches[3];
}, $content);

Here, I'm using the preg_replace_callback PHP function, which will execute a function that I define every time the regular expression finds a match in the subject string. I know that each code sample is wrapped in a <pre> tag and that the tag has a class attribute whose value starts with "brush:", so I use that information to find the code samples. Then, for each match the regular expression finds, it calls my custom function, where I have it replace the <br /> tags with newlines.

Fixing the dates

Because the publish dates of each blog post in the RSS feed are relative to the UTC timezone, I also have to make sure to apply my local timezone to each date. Otherwise, the dates will not be displayed correctly (like saying that I made a post at 2am in the morning).

$dateFromRss = 'Tue, 20 Dec 2011 02:30:00 +0000';
$dateFixed = new DateTime($dateFromRss);
$dateFixed->setTimezone(new DateTimeZone('America/New_York'));

Adding Highslide support to images

One extra feature that I included is adding Highslide support to each image (Highslide is a "lightbox" library which lets you view images in special popup windows). To do this, I load the blog post into a DOM, use XPath to query for all links that have images inside of them, and then add the appropriate attributes to the link tag.

$content = //the blog post

//XML doesn't like "&nbsp;", so replace it with the proper XML equivalent
//see: http://techtrouts.com/webkit-entity-nbsp-not-defined-convert-html-entities-to-xml/
$content = str_replace("&nbsp;", "&#160;", $content);

//load the text into a DOM
//add a root tag incase there isn't one
$xml = simplexml_load_string('<div>' . $content . '</div>');

//if there's a problem loading the XML, skip the highslide stuff
if ($xml !== false){
	//get all links that contain an image
	$links = $xml->xpath('//a[img]');
	//add the highslide stuff to each link
	foreach ($links as $link){
		$link->addAttribute('class', 'highslide');
		$link->addAttribute('onclick', 'return hs.expand(this)');

	//marshal XML to a string
	$content = $xml->asXML();
	//remove the XML declaration at the top
	$content = preg_replace('~^<\\?xml.*?\\?>~', '', $content);
	//trim whitespace
	$content = trim($content);
	//remove the root tag that we added
	$content = preg_replace('~(^<div>)|(</div>$)~', '', $content);

As you can see, the blog post text has to be awkwardly manipulated in order to be read into a DOM and written back out as a string. That's why I have a lot of comments here--when I have to revisit this code in 6 months, I won't be totally confused.

Caching the RSS file

One last thing to mention is that I cache the RSS file so that my website doesn't have to contact Blogger every time someone loads this page. When the cached file gets to be more than an hour old, a fresh copy of the file is downloaded from Blogger.

Back to top