I've spent another Saturday night hacking on computer. My wrists won't thank me in the morning. I'll think I'll dance away the pain. This time I have no one to blame but myself. Last weekend I had to stay awake to listen to a conference call while we upgraded systems.
I seem to have caught the GWT bug. Developing Javascript in Java brings the fun back into rich thin clients! In fact, once I started writing JUnit tests external of the *.client package to validate the logic of the client code I think I crossed the line between sceptic and believer. I'll keep putting snapshots of the source I create here. The beauty of GWT is that I don't need a server to display my results - it compiles to Javascript. One caveat is that Mozilla doesn't seem to like to submit on the click.
Consider this code as Javascript:
package com.owlmountain.client;
public class MatrixTextParser {
public Matrix parse ( String values ) throws MatrixTextInvalidException {
Matrix result = null;
String[] rows = values.split("\n");
for ( int i = 0; i < rows.length; i++ ) {
String[] column = rows[i].split(" ");
if ( i == 0 ) {
result = new Matrix(rows.length, column.length);
}
for ( int j = 0; j < column.length; j++ ) {
try {
result.set(i, j, Double.parseDouble(column[j]));
}
catch ( NumberFormatException e ) {
throw new MatrixTextInvalidException(i, j, column[j], e.getMessage());
}
}
}
return result;
}}
To test it, all I have to do is create (in a package outside of *.client)
package com.owlmountain.gwt.test;
import org.apache.log4j.Logger;
import com.owlmountain.client.Matrix;
import com.owlmountain.client.MatrixTextInvalidException;
import com.owlmountain.client.MatrixTextParser;import junit.framework.TestCase;
public class MatrixTextParserTest extends TestCase {
Logger logger = Logger.getLogger(MatMultTest.class);
MatrixTextParser parser = null;
public MatrixTextParserTest(String name) {
super(name);
parser = new MatrixTextParser();
}
public void testGoodParser() throws MatrixTextInvalidException {
Matrix values = parser.parse("1 2 3\n4 5 6\n");
assertEquals(2, values.getRowDimension());
assertEquals(3, values.getColumnDimension());
logger.info(values);
}public void testBadValueParser() {
try {
parser.parse("1 2 3\n4 6 b\n");
fail("Expected exception");
}
catch (MatrixTextInvalidException e) {
assertEquals(1, e.getRow());
assertEquals(2, e.getCol());
assertEquals("b", e.getValue());
logger.info(e.getMessage());
}
}}
I converted the Jama matrix class to run on the client side by removing every reference outside of the java.lang.*. Incidentally, I saw a lot of things I didn't like about Jama, right down to simple mistakes like not implementing "toString()" on the Matrix class. I'm still trying to decide whether I can forgive them for not having a dynamic matrix class. I'll have to think about a clean way to do that. Eventually, I'd like to write some SVG graphing and analysis tools, so having a good fast matrix library is a nice start. Of course, I want a lot of the matrix processing to occur on the client, because each transformation will be small.