Examining the Nutmeg Class Library

Smalltalk power for Java development

Doug Garrett

Doug, who is president of ANetSC Inc., can be contacted at http://www.anetsc.com.


One of the more powerful Smalltalk classes is the Collection class, which provides a variety of smart, flexible array objects to manage groups or sets of objects. While Java provides the functionality of many of the more important members of this family via its Vector, Stack, Hashtable, Dictionary, String, and StringBuffer classes (plus fixed-size arrays), Smalltalk's SortedCollection, Bag, and Set do not have comparable Java classes.

In the true spirit of object-oriented programming, the Nutmeg classes from Thought Inc. fill this void. Nutmeg is a complete implementation of a Smalltalk Collection class library. It includes classes such as Dictionary, Set, Indexed Collection, and Bag. It has features such as iterators, block execution, sort blocks external to classes (that can override a classes-compare method), DeepCopy functionality, and ThreadSafe behavior for all classes. In short, Nutmeg classes provide the functionality of the Java Collection-related classes, as well as the other Smalltalk classes missing from Sun's Java implementation. Additionally, the Nutmeg classes provide a programming interface that experienced Smalltalk developers can easily adapt to.

Figure 1 shows the class hierarchy of the Nutmeg Collection family. Although the number of classes may seem imposing at first, the major classes are those under the Collection class. Table 1 describes some of the different features and attributes of each of these classes. (A complete list of the 200 or so Nutmeg fields and methods is available at http://www.thoughtinc.com.)

One problem with Java's Collection-related classes is that, in many cases, they do not allow you to create subclasses to add your own functionality. Also, when you are allowed to create subclasses, the subclasses do not allow you to override class methods. For example, if you wanted to create a class called Table, which would contain a list of rows for a relational table, you could create a subclass of the Java Vector class. However, if you wanted to override the default addElement() method for the Vector class to edit a row before adding it to your table, you'd be out of luck. The Java Vector class defines the addElement() method as final-you can neither override it nor disable it. The alternative is to create an entirely new Table class based on the Java Object class, then reimplement those methods from the Vector class that you want to keep. Say you want to retain elements() to get an enumeration containing all of the rows in your table, or contains() to see if your table contains a particular row: In the latter case, you have lost much of the value of the object-oriented paradigm. Some classes, such as String and StringBuffer, do not even allow you to create subclasses.

Evaluating the Nutmeg Classes

My company, ANetSC Inc. (Another Net Software Company) recently evaluated Nutmeg for use in the development of a commercially available development tool, using the following criteria:

The tool we're creating at ANetSC is designed to allow companies to easily and quickly develop secure GUI business applications for Intranets and the Internet. To this end, we're using the Collection-class family primarily on the client to allow coordination and data sharing between applets. Since this part of the application occupies little of the total response time compared to the network and DBMS response time, performance is not a major issue-although it cannot be completely ignored.

Figure 2 is the main screen of a program that displays a table, allows changes on selected columns within the table, and resorts the table by clicking on a column heading. (The source code for this program is available electronically.) It's also available at http://www.anetsc.com.) This program, which makes extensive use of the OrderedCollection class and defines a Table class that is a subclass of the Nutmeg SortedCollection class, illustrates some basic functions of the Nutmeg classes. Although we could have used Java's Vector class, having a familiar set of methods was a big help in developing the example program. When it came to creating the Table class, the only alternative to using Nutmeg's SortedCollection class would have been creating a class of our own. This new class could not be a subclass of Vector since it would need to disallow some of the methods already in the Vector class. For example, it does not make any sense to have the Vector class method insertElementAt(Object, int) for a SortedCollection. Another major reason we did not use the Vector class was that, as mentioned earlier, the full-blown version of the Table class should allow overrides to the methods which add new elements. (This is not shown in the example program.)

Functionality

If a class won't do what you need, what good is it? We were looking for classes that would provide a sorted Collection as well as more-robust Collection classes that would support mass-update functions such as "add all the rows in this Collection to Collection xyz" and "delete all the rows in Collection abc from Collection def." We also needed the ability to create subclasses of all the Collection classes and to override superclass methods as needed. Nutmeg delivered on these requirements.

In addition to these capabilities, Nutmeg has some of the other Smalltalk classes not currently implemented in Java. These include

Nutmeg also provides a DoBlock class to implement the functionality of a Smalltalk do:[...] construct. Because Java does not allow the passing of code blocks or pointers to methods, Nutmeg includes this as a separate object, making it somewhat awkward to use if you need many blocks. Wisely, Nutmeg includes an alternative, allowing you to use the standard Java Enumeration interface to process all the objects within a collection. One complaint I have with this approach is that, to create an Enumeration, you must use new Enumeration(collection) instead of collection.elements(), which would be more consistent with the Java Vector and Hashtable classes.

Ease of Use

When it comes to software development, the name of the game today is time-to-market. Since there are not a lot of experienced Java developers out there, if you are developing a new Java-based application, a big factor in time-to-market will be your learning curve. With the object-oriented paradigm, much of the time spent in learning a language is not in learning the syntax of the language, but in learning about the classes that come with the language. Most good programmers will be able to pick up Java's syntax in a day or so. The hard part comes in learning to use all of objects that come with Java. If you are experienced with Smalltalk (as we are), you'll be able to get up to speed quickly by using the Nutmeg classes-which provide all the familiar Collection class methods and functionality.

On the other hand, if you are not experienced with Smalltalk, you may find the number of classes and methods contained in Nutmeg to be overwhelming. By and large, a good C++ programmer will be used to dealing with much more complexity than Nutmeg's Collection class and should have little trouble learning to use the classes. The javadoc-generated documentation that comes with Nutmeg will make learning the tool even easier. The built-in links allow you to easily traverse the class hierarchy to find what you're looking for.

If you are new to object-oriented programming and Java, you may have trouble learning Nutmeg at the same time. The javadoc-generated documentation is complete, but a bit overwhelming in size.

A second factor in time-to-market is reusability. The Nutmeg classes provide you with much more flexibility in creating subclasses of the Collection family. This, in turn, means less time spent reinventing the wheel and reimplementing methods that are similar to existing ones.

If you are converting an application from Smalltalk, you should definitely take a look at Nutmeg. If your programs make extensive use of the Smalltalk Collection class family (as most Smalltalk programs do), you'll find Nutmeg invaluable in converting them to Java.

Quality

I'm almost embarrassed to have listed this criterion third. After all, if a product doesn't work, it doesn't matter how much functionality it contains or how fast you were able to rush it to market, right? Well, almost. There's "quality," then there's "Quality!" You can expect to hit a few snags when developing any Java application. The question is will it be in the compiler, browser, or third-party objects? I hit all three in developing the example program presented in this article. If you are looking for a 100-percent-bulletproof app, you probably shouldn't even be considering Java, the Internet, or even C++ for that matter.

By and large, I experienced few problems with the Nutmeg classes. Since the Nutmeg classes will be in wider use than similar classes written on our own, I expect that, in the long term, they will contain fewer bugs than house-developed code. Those bugs that we did find were fixed quickly, and a bug fix was distributed while I was writing this article.

One other item to consider is the difficulty in testing threaded code. By its nature, bugs that are related to threads may show up only sporadically and will be extremely difficult to recreate. Just having a product that has been tested by outside companies and has users beyond your company helps improve the chances that the product will work as expected.

Performance

Figure 3 shows the main screen of a sample run of a program that performs an add, a lookup, and a delete of a thousand objects from the Java Vector and Hashtable classes, and also from the Nutmeg OrderedCollection and Dictionary classes. Although Java does not have a SortedCollection class, the same test was run for Nutmeg's SortedCollection class, loading the elements in both ascending (ordered) sequence and descending sequence; see Table 2. Just to make things interesting, we ran the same tests with and without Symantec's just-in-time compiler. The example is not meant to be a thorough and complete performance comparison between the classes, but it does represent much of how ANetSC plans to use the classes and gives you some idea of the performance you can expect.

As Table 2 shows, although the SortedCollection class is not as fast as the Java Hashtable class in performing a lookup by key, it provides reasonable performance while still allowing sequential processing in a predefined order. One caveat: Be careful if you are loading objects in the presorted order. As the timings show, this takes much longer than loading the objects in reverse sequence. One other weakness in the Nutmeg classes appears to be in the NDictionary class, which performed significantly slower than Java's Hashtable class. Thought Inc. is aware of both of the problems mentioned and may have fixed them by the time you read this article.

Conclusion

Overall, the Nutmeg classes are a valuable addition to your programming toolkit, especially if you need a SortedCollection class, are converting Smalltalk programs to Java, or are experienced in Smalltalk and want a more-familiar set of Collection classes. The DoBlock class is a bit on the awkward side, so you may want to forgo using it in favor of the CollectionEnumerator class (it's very easy to use and it fits better with Java architecture). At $49.00 for a noncommercial binary license, it's well worth the savings in time and effort (commercial licenses start at $495.00).

Table 1: Nutmeg Collection family class features. *SortedCollection class uses a binary search in performing a lookup of a particular object.

                     Fixed     Access     Access     Duplicates     Elements     Special
Class                Size      by Index   by Key     Allowed        Ordered      Characteristics

FixedSizeCollection  Y         Y          N          Y              N     
SortedCollection     N         Y          N*         Y              Y            Sequence based on 
                                                                                   Sort key
                                                                                  
OrderedCollection    N         Y          N          Y              Y     
Collection           N         Y          N          Y              N     
IndexedCollection    N         Y          N          Y              N     
Narray               N         Y          N          Y              N     
NDictionary          N         N          Y          N              N            Lookup of 
                                                                                   value by key

Bag                  N         N          N          Y              N            Keeps a count 
                                                                                   of duplicates 

Set                  N         N          N          N              N            Duplicates not 
                                                                                   allowed

Table 2: Nutmeg performance times in seconds on 1000 test cases. Tests were run using Symantec's Cafe Applet Viewer on a Pentium/133 with 16 MB of RAM. *SortedCollection loaded in ascending order. **SortedCollection loaded in descending order.



                              Time to     Time to     Delete     Delete      Total      JIT Times
                              Add         Find        Asc        Dsc                      Faster

Array          Cafe           0.00        0.69        0.69        0.69         2.08
               Cafe/JIT       0.00        0.09        0.09        0.09         0.28        7.4

Vector         Cafe           0.02        10.63       0.11        10.77        21.53
               Cafe/JIT       0.02        2.11        0.04        2.10         4.26        5.1

Ordered        Cafe           0.06        10.64       0.11        10.75        21.55
Collection     Cafe/JIT       0.02        2.11        0.05        2.13         4.30        5.0

Hashtable      Cafe           0.07        0.09        0.11        0.06         0.33
               Cafe/JIT       0.02        0.04        0.02        0.04         0.11        3.0

NDictionary    Cafe           3.00        2.78        0.05        3.31         9.15
               Cafe/JIT       0.49        0.50        0.03        0.66         1.68        5.4
               
Sorted         Cafe           18.40       1.28        0.22        1.21         21.11
Collection*    Cafe/JIT       3.32        0.16        0.06        0.16         3.70        5.7

Sorted         Cafe           1.92        1.32        0.16        1.27         4.67
Collection**   Cafe/JIT       0.33        0.16        0.06        0.16         0.71        6.5





Figure 1: Nutmeg Collection classes.

Figure 2: Program that displays a table, allows changes on selected columns within the table, and resorts the table by clicking on a column heading.

Figure 3: Program that performs an add, a lookup, and a delete of a thousand objects from the Java Vector and Hashtable classes, and also from the Nutmeg OrderedCollection and Dictionary classes. Note that results are not meant to correspond to Table 2.

For More Information

Thought Inc.

2222 Leavenworth Street, Suite 304

San Francisco, CA 94133

415-928-4229

http://www.thoughtinc.com