Saturday, February 26, 2005

Iterators in Java

Coming from a background in C# for the past couple of years, I am so glad to see the Java SDK add constructs that I have used in the past.

In a personal project that I am working on, I had created a couple of enumeration classes.  The classes had a lookup() method that I used to allow me to assign a particular enumeration label to a variable by looking up a particular id of one of the enumeration constants.  To facilitate the lookup of a particular enumeration value, there was a private array of all the available enumerations in the particular class.  The lookup() method was pretty straight forward...


//.. lookup procedure expects an id pass in
for ( int i = 0; i < available.length; i++ ) {
    if ( available[i].getId() == id ) {
        return available[i];
    }
    return null;
}
I decided at work yesterday to come home and setup the latest milestone release of Eclipse so that I could play around with JDK 5.0.  After getting that working, I refactored my enumeration classes to be actual enum classes now that Java 5 has the new enum keyword.  With that refactoring, I was able to make use of the new for-each style of iteration that Java 5 has added.  So, my lookup() method on the enumeration classes now look like this...

//.. lookup procedure still expects an id passed in
MyEnumType[] available = MyEnumType.values();
for ( MyEnumType e : available ) {
    if ( e.getId() == id ) {
        return e;
    }
    return null;
}
I have been used to this style of construct in C# which has the for-each style of iterations for a collection.  For a new Java developer, this method might be a little harder to understand.  There is not the explicit notion of having checking each object in a particular collection like you have in C# where you say “for each ...”, but I feel like this is a little cleaner code than the style where you use the index property, which could always lead to problems itself.

Of course, Java had the older style of iterations where you could get an iterator and then calling hasNext() on each loop iteration, but it is a bit more to type and it is nice to have the compiler itself be a little smarter in situations like this where I am looping over a collection and checking every object in the collection.

Technorati Tags: , , ,

Wednesday, February 16, 2005

Using INSERT..SELECT with MySQL

I have been working on a small application at home lately which is going to use MySQL for data persistence.  I was messing around with my tables tonight and got into a situation where I wanted to add data into one table using the last inserted value from the auto_increment primary key field in another table.

According to the documentation for MySQL, I should be able to use LAST_INSERT_ID(), but I couldn't figure out exactly how to get it to pull the last inserted ID from a different table.

So, I then tried the following code...

INSERT INTO table2 (id, field1, field2) SELECT NULL, 4, id FROM table1 t WHERE t.id IS NULL;
According to some information that I found in a comment posted on the documentation for the INSERT..SELECT command on the MySQL documentation site, this should give me the last inserted ID from the first table.  Nope... this results in no errors and no rows being added to the table.  Very weird!

Finally, I came up with the following...

INSERT INTO table2 (id, field1, field2) SELECT NULL, 4, MAX(id) from table1;
So far, this seems to give me the results that I am after.

Technorati Tags:

Tuesday, February 15, 2005

Adding a new DataRow to a DataTable

This is one little item that I never can seem to remember, because I just do not use it that often, so I thought I would commit it to my blog.

When creating a new DataRow that you want to subsequently add to a DataTable, you generally create the columns that you need for the rows and then you add data values. The code for this looks something like this...

    DataTable ErrorTable = new DataTable();
    ErrorTable.Columns.Add(new DataColumn("Fault Code Namespace", typeof(string)));
    ErrorTable.Columns.Add(new DataColumn("Fault Code Name", typeof(string)));
    ErrorTable.Columns.Add(new DataColumn("SOAP Actor that threw Exception", typeof(string)));
    ErrorTable.Columns.Add(new DataColumn("Error Message", typeof(string)));
    ErrorTable.Columns.Add(new DataColumn("Detail", typeof(string)));
    DataRow row = ErrorTable.NewRow();
    row["Fault Code Namespace"] = error.Code.Namespace;
    row["Fault Code Name"] = error.Code.Name;
    row["SOAP Actor that threw Exception"] = error.Actor;
    row["Error Message"] = error.Message;
    row["Detail"] = HttpUtility.HtmlEncode(error.Detail.OuterXml);


There is a simpler way to write the code above which sets up the data values and adds the DataRow to the DataTable. That code can be written in one line like this...

    ErrorTable.Rows.Add( new object[] {error.Code.Namespace, error.Code.Name, error.Actor,
    error.Message, HttpUtility.HtmlEncode(error.Detail.OuterXml)} );


The code is definitely not more readable and is probably more of a headache for someone to come along later and maintain, but it is easier to type the first time around. :)

Technorati Tags: , , ,