10 days in Kansas

I am drawing to the end of 10 days in Lawrence, KS, my home town. I’m sad to be leaving. I love it here. That being said it will be nice to be back in NYC with the places and people I know there. I’m in a odd place of being home sick for both my homes at once.

I’ve had fun here though. I got a chance to set off my own fireworks for the first time in 5 years. That was really nice. I bought $80 worth of fireworks (mostly shells) and it was totally worth it. It was a lot of fun to have 34 good quality shells.

I went with some freinds to a big fireworks party outside of Lawrence (in Vinland) there were huge amounts of people so I had a place to legally setoff all my fireworks. And I mostly just hung out with the people I brought with me but it was a blast (no pun intended).

Tomorrow night I will be back in the big city. And it will be a relief and I will immediatly miss Lawrence. Lawrence really is my home a deep and real way. I kind of think it always will be.

Posted in Life (other than code)

Initialization order in Java classes

I just found a Java “got’cha” (there certainly are plenty aren’t there?).

When a Java object is constructed the super class constructor is called first. So far this makes sense. But this also applies to initializations that are written at the point of declaration like:

Object obj = null;

This is different then leaving out the = null in that obj is nulled after the superclass constructor runs. This is usually not a problem, but I have a situation where some of the initialization occurs in a method called from the superclass constructor (this is a dubiously good design I know). The result is that if the initialization method called from the superclass sets obj that change will be overwritten with null in the subclass constructor.

It took me several hour to find this problem. I’m going to be much more careful with calls to virtual functions in the constructor. It is considered bad form for a reason. Also this is one of the few times I have started to understand why the functional programming fan boys bash on object oriented programming. The OO methodology is flawed in a number of ways. That being said I think there is a place for it. It models the real world of things that can interact in a very nice way. I think that the actor model may turn out to be better, but that will take time.

Posted in Java, Programming Theory

Randomness and it’s creation

When I was young I knew a guy who played the lottery a lot and firmly believed that his odds were higher if he put his money on numbers that had not come up in a long time. In other word he thought random systems tend “catch up” to become balanced, but this is of course totally fails. Random systems are only balanced over an infinite sample.

Before I go any further I should note that I am not an expert on any of this stuff. I don’t know much about the theory of randomness or the methods of it’s creation. This is just my own thoughts.

A lot of people seem to have strange ideas of what “random” is. For instance, look at the Dice-O-Matic built for GamesByEmail.com. It is a mechanical dice roller and is a really fun project BTW. I don’t mean to put it down at all with this post, I just don’t really understand why people would like it is better than a computer based random number generator (true or pseudo). It was built because users of the site complained that the computer generated dice rolls where not random enough. The sources he used were standard pseudo-random number generators and also true random number generators like random.org. PRNGs are certainly not random but over the sample size used on board games it seems very unlikely to me that anyone could detect the non-randomness. And if you introduce something like random.org you have random numbers generated by such a huge system (the earths atmosphere) that they would almost certainly be more random than dice rolling on a table (especially the cheap dice that come with most games).

My suspicion is that people felt like they were getting too many low rolls. But that happens in random systems. Anything can happen; that’s what random means.

There are many ways to build table top true random number generator. Many of them much smaller than a dice rolling machine (and probably more random). For instance:

Here are instuctions for building a small device that detects the alpha emitions from a sample of americonium (from a smoke detector): Alpha Radiation Visualizer. The output of this will be very random (the radiation is a quantum process).

Here is a chaotic circuit that can be sampled to produce random numbers: Make your own True Random Number Generator 2. This TRNG is not quantum (I don’t think) but it is highly chaotic and should be a good source of random bits.

[EDIT] There is also LavaRnd. Which looks much better designed than any of the other ones I mentioned. Though I think either of the above hardware devices could be used as good input into the LavaRnd algorythms.

Posted in Hardware

Ray casting in Scala

Ray casting is an old rendering technique used in early “3D” games like Wolfenstein 3D. Today it is nothing much, but implementing it will test your knowledge of trig and binary rounding issues. The real reason I wrote it though was because I have tried to implment a raycasting engine several times before and failed. Until now! So it was a matter of geek pride to go back to this problem and defeat it.

I implemented the engine in as functional a way as possible. I think it came out fairly well. I did not optimize anything for performance. So I used Double values for the most part. This is very different than most implementations you see online that implement the entire algorithm with ints and use look up tables for the trig functions. One issue that I had run into before is that because parts of the algorithm are fundamentally quantized (the map cells) floating point can actually cause problems like disappearing walls or holes in walls. I was able to work around these issues through the creative use of rounding and a fudge factor (to make sure the value was on the correct side of a boundry). Regardless it works now and I’m glad I defeated the algorithm.

The project is available in github:

Posted in Scala

Inlining and optimization of methods in the JVM

I was just reminded of something that I often forget. So I thought I’d mention it here.

When the JVM (at least Sun’s HotSpot) optimizes a method it will only create one optimized version. So that if more than one type of object is passed to the function it will prevent inlineing and other optimization. This means you have to be conscious of what you allow to be passed into methods that should be optimized since the compiler will optimize for the lowerest common denominator of all input values.

This is standard for JITs. However as a side note, there are JITs that do produce multiple optimized versions for functions that are used in more than one context. One example is Psyco. Work on Psyco has stopped and new development is going into PyPy which is a fascinating (and kind of deranged) project.

Posted in JVM

Problems in the future of google streetview

I was thinking about the future of Google street view and I realized something. One of the really nice things about the street view is that the images are pretty recent. However that will change. They are spending a lot of money to capture all these images, but I doubt they will want to spend that again to recapture them in 2 years.

So: will Google street view decline in usefulness as the images age? Or will Google update them? Or will the old images still be just as useful?

I wonder if Google might keep track of construction projects and reimage area based on how much they have been changed.

PS: They get some odd pictures too: Top 10 Moments Caught on Google Maps Street View

Posted in Uncategorized

Metawidget BeanUtils auto-update binding

I have been using Metawidget (which is a really cool library by the way) on a project lately. I wanted to avoid the need for a save button by having changes automatically propagated to the beans when changes are made to the widgets. However I am using BeanUtils (from apache) which does not support binding internally.

I did try BeanBinding but it was really slow (I don’t know why and I didn’t have time to debug it). So I created an extension to the BeanUtilsBinding class (from Metawidget) that adds auto-save support. It’s a really ugly hackish implementation but it works reliably (though I think there may be corner cases where the save does not trigger when it should). It is also quite fast. It doesn’t run the save until all the messages currently in the swing queue are processed (using SwingUtilities.invokeLater) and then it only runs it once so even if a lot of changes happen at the same time only one save will be done.

The implementation is below. Feel free to use it. It is copyright Arthur Peters under the GNU GPL v3.


import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.metawidget.swing.SwingMetawidget;
import org.metawidget.swing.propertybinding.beanutils.BeanUtilsBinding;

public class BeanUtilsAutoUpdateBinding extends BeanUtilsBinding {
	@SuppressWarnings("unused")
	private static final Logger log = Logger
			.getLogger("BeanUtilsAutoUpdateBinding");

	protected static final class ListenToRecord {
		private final Component component;
		private final String propertyName;

		public ListenToRecord(Component component, String propertyName) {
			this.component = component;
			this.propertyName = propertyName;
		}

		public Component getComponent() {
			return component;
		}

		public String getPropertyName() {
			return propertyName;
		}
	}

	private List listeningTo = new ArrayList();

	private ChangeListener changeListener = new ChangeListener() {
		@Override
		public void stateChanged(ChangeEvent e) {
			doSave();
		}
	};

	private ActionListener actionListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			doSave();
		}
	};
	/*private DocumentListener documentListener = new DocumentListener() {
		@Override
		public void changedUpdate(DocumentEvent e) {
			doSave();
		}

		@Override
		public void insertUpdate(DocumentEvent e) {
			doSave();
		}

		@Override
		public void removeUpdate(DocumentEvent e) {
			doSave();
		}
	};
	*/
	private KeyListener keyListener = new KeyListener() {
		@Override
		public void keyPressed(KeyEvent e) {
			doSave();
		}

		@Override
		public void keyReleased(KeyEvent e) {
			doSave();
		}

		@Override
		public void keyTyped(KeyEvent e) {
			doSave();
		}

	};
	private MouseListener mouseListener = new MouseListener() {
		@Override
		public void mouseClicked(MouseEvent e) {
			doSave();
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// Ignore
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// Ignore
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// Ignore
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			doSave();
		}
	};
	private FocusListener focusListener = new FocusListener() {
		@Override
		public void focusGained(FocusEvent e) {
			// Ignore
		}

		@Override
		public void focusLost(FocusEvent e) {
			doSave();
		}
	};

	private boolean saveAlreadyInEventQueue;

	public BeanUtilsAutoUpdateBinding(SwingMetawidget metawidget) {
		super(metawidget);
	}

	@Override
	public void bindProperty(Component component,
			Map attributes, String path) {
		super.bindProperty(component, attributes, path);

		String valueProperty = getMetawidget().getValueProperty(component);
		// component.addPropertyChangeListener(valueProperty, this);
		attemptToCall(component, "addChangeListener", ChangeListener.class,
				changeListener);
		attemptToCall(component, "addActionListener", ActionListener.class,
				actionListener);
		// attemptToCall(component, "addDocumentListener",
		// DocumentListener.class, documentListener);
		component.addKeyListener(keyListener);
		component.addMouseListener(mouseListener);
		component.addFocusListener(focusListener);
		listeningTo.add(new ListenToRecord(component, valueProperty));
	}

	private void attemptToCall(Component component, String methodName,
			Class eventListenerCls,
			EventListener eventListener) {

		try {
			Class cls = component.getClass();
			Method method = null;
			while (method == null && cls != Object.class) {
				try {
					method = cls.getMethod(methodName, eventListenerCls);
				} catch (NoSuchMethodException e) {
					// Ignore
				}
				cls = cls.getSuperclass();
			}
			method.invoke(component, eventListener);
		} catch (Exception e) {
			// Ignore it and return. This function is an attempt.
			log.log(Level.FINE, "Failed to call " + methodName + " on "
					+ component, e);
		}
	}

	@Override
	public void unbindProperties() {
		super.unbindProperties();

		for (ListenToRecord listenToRecord : listeningTo) {
			Component component = listenToRecord.getComponent();
			// component.removePropertyChangeListener(listenToRecord.getPropertyName(),
			// this);
			component.removeKeyListener(keyListener);
			component.removeMouseListener(mouseListener);
			attemptToCall(component, "removeChangeListener",
					ChangeListener.class, changeListener);
			attemptToCall(component, "removeActionListener",
					ActionListener.class, actionListener);
			// attemptToCall(component, "removeDocumentListener",
			// DocumentListener.class, documentListener);
		}
		listeningTo.clear();
	}

	protected void doSave() {
		synchronized (this) {
			if (!saveAlreadyInEventQueue) {
				saveAlreadyInEventQueue = true;
				SwingUtilities.invokeLater(new Runnable() {
					@Override
					public void run() {
						getMetawidget().save();
						synchronized (BeanUtilsAutoUpdateBinding.this) {
							saveAlreadyInEventQueue = false;
						}
					}
				});
			}
		}
	}
}
Posted in Java, Programming

Java for Google App Engine

When Google App Engine first came out I was really surprised it didn’t support Java, because Google was (and is) really pushing GWT, so to have them release a web deployment platform that did not support their own favorite framework was odd. However they have now released a preview version of Google App Engine running Java. It doesn’t do everything I might want, for instance Lift doesn’t run on it out of the box and it doesn’t allow you to spawn your own threads (these problems are directly related). There is some work going on to make Lift work on it though (see the email thread).

Overall though I’m really excited. I have been looking for a platform that will allow me to run Java based web apps for a reasonable price and I think this is the first one that provides something that I may actually use. For a low traffic site it is free and it looks to me that the prices are pretty good even as the traffic increases.

Posted in Java, Scala

Installing and upgrading plugins in Eclipse

I have had problems with “No repository found at” errors while trying to install or update eclipse plugins. Other people have had similar problems (on the andriod forums for instance).

I found that the solution was to open eclipse on a fresh workspace (just specify an empty directory for the workspace it will create the stuff it needs). Once the install/upgrade is done I can just switch back to my normal workspace and everything is as it should be. The upgrades and installs are installation-wide so the workspace change does not effect them. For some reason the stuff in my workspace was causing problems. It doesn’t make much sense really.

Posted in Tooling

Installing Subversive on Eclipse

I ran into a problem with installing Subversive (An eclipse plugin that interfaces to Subversion). The issue is that the plugin comes in 2 pieces that are both required: the Subversive plug-in and the Subversive SVN Connectors plug-in. This was not obvious to me and I ran into this problem not once but twice.

The solution is to add these 2 update sites (current as of Apr 2nd, 2009):

  • http://www.polarion.org/projects/subversive/download/eclipse/2.0/update-site/
  • http://download.eclipse.org/technology/subversive/0.7/update-site/

And install the following features from them:

  • Subversive SVN Team Provider (from the http://download.eclipse.org/technology/subversive/0.7/update-site/ site)
  • Subversive SVN Connectors (from the http://www.polarion.org/projects/subversive/download/eclipse/2.0/update-site/ site)
  • SVNKit 1.2.2 Implementation (from the http://www.polarion.org/projects/subversive/download/eclipse/2.0/update-site/ site)

The error message in the eclipse logs was as follows when I failed to add the site for and install the “Subversive SVN Team Provider”. I hope this will help people find this and not have the same problems I did.

!ENTRY org.eclipse.equinox.p2.ui 4 10005 2009-04-06 15:30:28.470
!MESSAGE Cannot complete the request.  See the details.
!SUBENTRY 1 org.eclipse.equinox.p2.ui 4 10005 2009-04-06 15:30:28.470
!MESSAGE Cannot complete the request.  See the details.
!SUBENTRY 1 org.eclipse.equinox.p2.director 4 0 2009-04-06 15:30:28.470
!MESSAGE Unsatisfied dependency: [org.polarion.eclipse.team.svn.connector.feature.group 2.1.0.I20090213-1500] requiredCapability: org.eclipse.equinox.p2.iu/org.eclipse.team.svn.feature.group/[0.7.2.I20080801
-1500,1.0.0)
!SUBENTRY 1 org.eclipse.equinox.p2.director 4 0 2009-04-06 15:30:28.470
!MESSAGE Unsatisfied dependency: [org.polarion.eclipse.team.svn.connector.svnkit15.feature.group 2.1.0.I20090213-1500] requiredCapability: org.eclipse.equinox.p2.iu/org.eclipse.team.svn.feature.group/[0.7.2.I20080801-1500,1.0.0)
!SUBENTRY 1 org.eclipse.equinox.p2.director 4 0 2009-04-06 15:30:28.470
!MESSAGE Unsatisfied dependency: [org.polarion.eclipse.team.svn.connector.svnkit15 2.1.0.I20090213-1500] requiredCapability: osgi.bundle/org.eclipse.team.svn.core/0.0.0
!SUBENTRY 1 org.eclipse.equinox.p2.director 4 0 2009-04-06 15:30:28.470
!MESSAGE Unsatisfied dependency: [org.polarion.eclipse.team.svn.connector.svnkit15 2.1.0.I20090213-1500] requiredCapability: osgi.bundle/org.eclipse.team.svn.core/0.0.0
!SUBENTRY 1 org.eclipse.equinox.p2.director 4 0 2009-04-06 15:30:28.471
!MESSAGE Unsatisfied dependency: [org.polarion.eclipse.team.svn.connector.feature.group 2.1.0.I20090213-1500] requiredCapability: org.eclipse.equinox.p2.iu/org.eclipse.team.svn.feature.group/[0.7.2.I20080801-1500,1.0.0)
!SUBENTRY 1 org.eclipse.equinox.p2.director 4 0 2009-04-06 15:30:28.471
!MESSAGE Unsatisfied dependency: [org.polarion.eclipse.team.svn.connector.svnkit15.feature.group 2.1.0.I20090213-1500] requiredCapability: org.eclipse.equinox.p2.iu/org.eclipse.team.svn.feature.group/[0.7.2.I20080801-1500,1.0.0)
!SUBENTRY 1 org.eclipse.equinox.p2.director 4 0 2009-04-06 15:30:28.471
!MESSAGE Unsatisfied dependency: [org.polarion.eclipse.team.svn.connector.svnkit15.feature.group 2.1.0.I20090213-1500] requiredCapability: org.eclipse.equinox.p2.iu/org.polarion.eclipse.team.svn.connector.feature.group/0.0.0
!SUBENTRY 1 org.eclipse.equinox.p2.director 4 0 2009-04-06 15:30:28.471
!MESSAGE Unsatisfied dependency: [org.polarion.eclipse.team.svn.connector.svnkit15.feature.group 2.1.0.I20090213-1500] requiredCapability: org.eclipse.equinox.p2.iu/org.polarion.eclipse.team.svn.connector.svnkit15/[2.1.0.I20090213-1500,2.1.0.I20090213-1500]
Posted in Java, Tooling