Naked Objects
By Richard Pawson and Robert Matthews

Enriching object behaviours

Manipulating collections

Collections within the Naked Objects framework are for storing a number of naked objects in a unique unsorted set. Some of the collections are also typed so that they will only accept a specific subtype of org.nakedobjects.object.NakedObject - i.e. a specific type of business object. There are three types of collections used within the framework (declared in the org.nakedobjects.object.collection package). All are based on the same abstract collection but differ in what type of object they store and the reason they are used. In order of frequency of use they are:

  • InternalCollection, a collection of associations, which is a composite part of a business object. The objects it contains are of a type specified when the collection was created. This was described in the section on one-to-many associations.
  • ArbitraryCollection, a general untyped collection of objects that the user creates for his own ad hoc use. This type of collection is created when any existing collection is copied.
  • InstanceCollection, a collection of instances of a specific type, generated directly by the persistence mechanism. For example, in the user view the pop-up menu on a class offers the option of listing all instances of that class, or finding instances that match given criteria. If matches are found they will be returned as an InstanceCollection.

The abstract superclass AbstractNakedCollection provides methods to add and remove elements from a collection. You access the collection's elements through an Enumeration, which gives access to all the objects in the collection so that you can iterate through it. There are also some utility methods to check the size and content of a collection.

  • public void add(NakedObject element) adds a new element to the collection, first calling the collection's abstract canAdd method to check whether the object can be added. The InternalCollection implements the canAdd so that only objects of the specifed type, and which are not already in the collection, can be added. The same method in the ArbitraryCollection class checks only for the existence of the object. InstanceCollection, on the other hand, vetoes any addition by the programmer.
  • public void remove(NakedObject element) removes a specific existing element. In the same way as for the add method, remove calls the canRemove method to check if the object is currently held and to determine whether the element can be removed. This is implemented generally so that all elements can be removed, except in the InstanceCollection where this is never allowed.
  • public Enumeration elements() generates an Enumeration that can be used to iterate through the collection, as below:

    Enumeration e = employees.elements();
    	Employee emp;
    	emp = (Employee)e.nextElement();
  • public boolean contains(NakedObject element) checks to see if a specific element is held by the collection, returning true if it is.
  • public boolean isEmpty() determines if the collection is empty and returns true if it is.
  • public int size() determines how many elements the collection contains.