The battle of the container frameworks: which should you use?

We compare ObjectSpace's JGL and Sun's Collections Framework to help you pick the best option

1 2 3 4 5 Page 5
Page 5 of 5

//======================================================================== // ExpensiveConsultants (C) December 1998 JavaWorld - All Rights Reserved // -------------------- // // History: // -------- // 28/09/98: started this file from new.java //========================================================================

import java.util.Date;

import COM.objectspace.jgl.*; // for Array import COM.objectspace.jgl.algorithms.*; // for Sorting import COM.objectspace.jgl.functions.*; // for Print import COM.objectspace.jgl.predicates.*; // for UnaryAnd

public class ExpensiveConsultants {

//------------------------------------------------------------------- // main() entry point //------------------------------------------------------------------- public static void main (String[] args) {

double expensiveRate = Double.valueOf(args[0]).doubleValue();

Container teamMembers = new Array();

teamMembers.add( new Employee("Black", 40000.0) ); teamMembers.add( new Employee("Flint", 52500.0) ); teamMembers.add( new Employee("Jones", 75000.0) ); teamMembers.add( new Employee("Smith", 238000.0) );

teamMembers.add( new Consultant("Golding", 120.0) ); teamMembers.add( new Consultant("King", 200.0) ); teamMembers.add( new Consultant("Jackson", 85.0) );

UnaryPredicate allConsultants = new UPIsInstanceOf(Consultant.class);

UnaryPredicate earningMoreThan = new PaidPersonEarningMoreThan(expensiveRate);

// create complex predicate by logically "anding" simple predicates UnaryPredicate allConsultantsEarningMoreThan = new UnaryAnd(allConsultants, earningMoreThan);

// locate consultants that are too expensive Container tooExpensiveConsultants = Filtering.select(teamMembers, allConsultantsEarningMoreThan);

// display found set Applying.forEach(tooExpensiveConsultants, new Print() );

} // End of main()

} // End of Class ExpensiveConsultants

class PaidPersonEarningMoreThan implements UnaryPredicate {

protected double hourlyRate;

PaidPersonEarningMoreThan(double hourlyRate) { this.hourlyRate = hourlyRate; }

public boolean execute(Object o) { PaidPerson paidPerson = (PaidPerson) o; return ( paidPerson.hourlyRate > this.hourlyRate ); } }

//======================================================================== // UPIsInstanceOf (C) Sep 1998 Laurence VanhelsuwÈ - All Rights Reserved // -------------- // Unary predicate that returns true if argument object is an instance of // some class (specified at predicate construction). // // History: // -------- // 28-Sep-1998: started this file from new.java // // Author email: classlib@telework.demon.co.uk //========================================================================

/*************************************************************************** * Unary predicate that returns true if argument object is an instance of * some class (specified at predicate construction). * * @version 1.0 28-Sep-1998 * @author Laurence VanhelsuwÈ (classlib@telework.demon.co.uk) **************************************************************************/ class UPIsInstanceOf implements UnaryPredicate {

protected Class instanceClass;

/************************************************************************* * UPIsInstanceOf constructor. * * @param instanceClass the Class for the instanceof test. *************************************************************************/ public UPIsInstanceOf(Class instanceClass) { this.instanceClass = instanceClass; }

public boolean execute(Object anObject) { return instanceClass.isInstance(anObject); }

} // End of Class UPIsInstanceOf

And here's a solution using the Collections Framework:

//======================================================================== // ExpensiveConsultants2 (C) December 1998 JavaWorld - All Rights Reserved // --------------------- // // History: // -------- // 28/09/98: started this file from new.java // 05/10/98: Modified to use Java Collections instead of JGL //========================================================================

import java.util.*;

public class ExpensiveConsultants2 {

public static void main (String[] args) {

double expensiveRate = Double.valueOf(args[0]).doubleValue();

Collection teamMembers = new ArrayList();

teamMembers.add( new Employee("Black", 40000.0) ); teamMembers.add( new Employee("Flint", 52500.0) ); teamMembers.add( new Employee("Jones", 75000.0) ); teamMembers.add( new Employee("Smith", 238000.0) );

teamMembers.add( new Consultant("Golding", 120.0) ); teamMembers.add( new Consultant("King", 200.0) ); teamMembers.add( new Consultant("Jackson", 85.0) );

Collection tooExpensiveConsultants = new ArrayList();

for (Iterator i=teamMembers.iterator(); i.hasNext(); ) { PaidPerson worker = (PaidPerson) i.next(); if (worker instanceof Consultant && worker.hourlyRate > expensiveRate) tooExpensiveConsultants.add(worker); }

System.out.println(tooExpensiveConsultants); } }

Conclusion

Comparing ObjectSpace Inc.'s JGL and Sun's Collections Framework turns out to be like comparing apples and kiwi fruits. At first sight, the two frameworks seem to be competing for the same developers, but after a closer inspection it is clear that the two cannot be compared fairly without acknowledging first that the two frameworks have different goals.

If, like Sun's documentation states, Collections is going to homogenize Sun's own APIs (core API, extensions, etc.), then clearly Collections has to be great news, and a good thing, even to the most fanatic JGL addict. Provided Sun doesn't break its promise in this area, I'll be happy to invest my resources in adopting Collections in earnest.

At the same time, JGL's balanced "algorithms plus data structures" agenda results in it being more powerful when it comes to applying algorithms to your containers. As long as Collections marginalizes the algorithmic side of the coin, I believe JGL still has a long life ahead of it.

Laurence Vanhelsuwé is an independent software engineer. Self-taught, he dropped out of college and immediately started a professional career writing arcade games. He has worked on such diverse technologies as X.25 WAN routers, virtual reality flight simulation, Postscript, and real-time digitized video-based traffic analysis.

Learn more about this topic

  • ObjectSpace's JGL home page http://www.objectspace.com/developers/jgl/
  • Sun's Collections Framework home page http://java.sun.com/products/jdk/1.2/docs/guide/collections/index.html
  • See the JavaWorld article on JGL "Need a good set of abstract data structures? ObjectSpace's JGL packs a punch!" http://www.javaworld.com/javaworld/jw-06-1997/jw-06-jgl.html
  • See the JavaWorld article "Get started with the Java Collections Framework" http://www.javaworld.com/javaworld/jw-11-1998/jw-11-collections.html
  • See the JavaWorld article "Speed up batch file processing using generic programming and core reflection" http://www.javaworld.com/javaworld/jw-11-1998/jw-11-batch.html
  • The Java Tutorial section on Sun's Collection Framework is available at http://java.sun.com/docs/books/tutorial/collections/index.html
  • For an interview with Alexander Stepanov, see http://www.bml.ca/marine/stepanov.htm
  • David R. Musser provides a page on generic programming (Musser worked with Stepanov) http://www.cs.rpi.edu/~musser/gp/
  • Matthew Austern and Alex Stepanov did some generic programming work for JavaJAL (Java Algorithm Library) http://reality.sgi.com/austern/java/index.html
1 2 3 4 5 Page 5
Page 5 of 5